PadCodedocs

Web APIs

Get and send data with the Call API step: its settings, reading the answer, handling failures with Try, and a sign-in example.

A web API is a service your app talks to over the internet: it signs people in, sends the weather, lists products. The Call API step sends it a request and saves the answer in a variable, where formulas and views can read it.

Call API's settings: Url https://dummyjson.com/auth/login, Method POST, Query, and an Authorization header of Bearer {accessToken}.

#Before you start

  • The address must start with https://. PadCode refuses plain http, and so does iPadOS.
  • Make a variable for the answer. An Anything variable fits: it takes whatever shape the answer has. See Variables.
  • Try it in Preview. The canvas doesn't run steps, so data from an API shows up in Preview, not while you design.

#Add a Call API step

  1. Select the view that starts it, like a Button, and open the Inspector's Actions tab.
  2. Pick when it runs, like When Tapped. To load data as a screen opens, pick When Loading.
  3. Tap Add action and choose Call API, under Web & APIs.
  4. Tap the step to open its settings.

To call the same API from several places, like a Sign in button and the keyboard's Return key, put the steps in a reusable action and start it with Run Action.

#Settings

SettingWhat it does
UrlThe whole web address, like https://dummyjson.com/products. A note under the box says so if it doesn't start with https://. Braces work here: https://dummyjson.com/products/{productID}.
MethodGET reads (where it starts), POST creates, PUT and PATCH change, DELETE removes. The API's own documentation says which to use.
QueryValues added to the end of the address, like ?limit=30. Tap Add field, name it (limit) and give it a value (30).
HeadersExtra lines sent with the request, like Authorization. Add them like Query's. A header's value can be text with braces, like Bearer {accessToken}, or a secret picked with ⚡︎.
BodyWhat to send, as JSON. Usually for POST, PUT and PATCH. See Send a body.
IntoThe variable that keeps the answer. Pick one, or New Value to make one.
TimeoutHow many seconds to wait before giving up. It starts at 30.

Every field in Query, Headers and Body has a ⚡︎ button, to link it to a variable or a formula. Tap ⊖ to remove a field.

#Send a body

Most APIs want the body as named fields, like a username and a password.

  1. Tap the {} button next to Body. A field named field1 appears.
  2. Tap field1 and type the name the API expects, like username.
  3. Give it a value: type it, or tap ⚡︎ beside the name to link a variable or a formula.
  4. For each other field, tap Add field and pick its type: Text, Decimal Number, True / False, Date & Time, Color, Web Address, List of Text or Record.

Text fields take braces too: Hi {name}. Removing the last field puts back the plain box.

#Read the answer

When the API answers with JSON, as most do, the variable you picked in Into holds it as records and lists. Read it with formulas:

If the answer isRead it with
{ "accessToken": "eyJ…", "firstName": "Emily" } in sessionsession.accessToken, session.firstName
{ "products": [ { "title": "Phone" }, … ] } in catalogcatalog.products[0].title, count(catalog.products)
  • Show a value: put it in braces in text, like Hi, {session.firstName}, or link a setting with ⚡︎ → Formula….
  • Keep a part: add a Set Value after the Call API, with To linked to a formula like session.accessToken.
  • Show a list: link a Repeater's Data with ⚡︎ → Formula… → catalog.products, then show each product with item.title.

An answer that isn't JSON is kept as text.

#Handle failures with Try

A call can fail: no internet, a wrong address, a wrong password. PadCode counts any answer whose status isn't between 200 and 299 as a failure. When a Call API fails, the steps after it don't run, and the app shows what went wrong in a red message at the top of the screen.

To decide what happens instead, put the Call API inside a Try:

  1. Add a Try step.
  2. Under its Do, tap Add action and add the Call API, plus the steps that use its answer.
  3. Under its Catch, add the steps to run if any of them fails, like a Set Value that keeps a message to show.
A Try step open: Do holds a Call API to dummyjson.com/auth/login, then Set Value steps that keep parts of the answer.

Inside Catch, error says what went wrong. The ⚡︎ menu lists its parts under From this action:

NameWhat it holds
error.messageWhat went wrong, in words, like "The server answered with error 401."
error.statusThe status the server answered with, like 401.
error.bodyThe server's answer. Many APIs explain the problem there, like error.body.message.

error.status and error.body only have values when the server answered. When the call didn't get that far, like with no internet, they're null and error.message explains. So a good message to show is error.body.message ?? error.message.

#See what happened

In Preview, open the debugger. Each Call API adds a line with its method, address and status, like POST https://dummyjson.com/auth/login → 200, with the answer under it. When one fails, a red line says why, and a Try adds "Try caught it" before its Catch steps run.

The debugger explains common statuses:

StatusWhat the debugger says
400The server didn't understand the request: check the body and query.
401The server doesn't know who's asking: check the headers, like Authorization, and their secrets.
403The server won't allow it with these credentials.
404There's nothing at that address: check the URL.
429Too many requests: wait a moment, then try again.
500 and upThe server had a problem of its own. Try again later.
Any otherThe server refused the request.

Other messages you may see:

MessageWhat to do
APIs have to use an https:// address.Change Url to start with https://.
The secret “name” has no value yet. Add it under Data & Logic → Secrets.Give the secret a value. See Secrets.

#Example: sign in to DummyJSON

DummyJSON is a free practice API with test users. One of them is emilys, with the password emilyspass. This example signs in, keeps the token, and loads the profile with it.

1. Add the variables in Data & Logic:

VariableTypeStarting value
usernameTextemilys
passwordTextemilyspass
signingInTrue / FalseOff
signInErrorText(empty)
sessionAnything
accessTokenText(empty)
meAnything

Link a Text Field's Text to username and a Password Field's Text to password, so people can type their own.

2. Make a reusable action named signIn, with these steps:

  1. Set Value: signingIn to On.
  2. Set Value: signInError to empty.
  3. Try, and in its Do:

    1. Call API: Url https://dummyjson.com/auth/login, Method POST. Body fields: username linked to the formula trim(username), password linked to the variable password, and expiresInMins set to 30. Into session.
    2. Set Value: accessToken to the formula session.accessToken.
    3. Go to Screen: the profile screen.

    In its Catch:

    1. Set Value: signInError to the formula error.body.message ?? error.message.
  4. Set Value: signingIn to Off.
The signIn reusable action: Set Value, Set Value, Try and Set Value, in order.

3. Start it. Give the Sign in button a When Tapped step: Run Action → signIn. Its Title can say what's happening: {signingIn ? 'Signing in…' : 'Sign in'}. Under the button, add a Text showing {signInError}, with Show when signInError != ''.

4. Load the profile. On the profile screen, add a When Loading Call API: Url https://dummyjson.com/auth/me, a header named Authorization with the value Bearer {accessToken}, and Into me. Then show Hi, {me.firstName}.

Run it in Preview. Change the password to see the Catch steps at work, and open the debugger to see each request and its answer.

#See also