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.
#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
- Select the view that starts it, like a Button, and open the Inspector's Actions tab.
- Pick when it runs, like When Tapped. To load data as a screen opens, pick When Loading.
- Tap Add action and choose Call API, under Web & APIs.
- 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
| Setting | What it does |
|---|---|
| Url | The 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}. |
| Method | GET reads (where it starts), POST creates, PUT and PATCH change, DELETE removes. The API's own documentation says which to use. |
| Query | Values added to the end of the address, like ?limit=30. Tap Add field, name it (limit) and give it a value (30). |
| Headers | Extra 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 ⚡︎. |
| Body | What to send, as JSON. Usually for POST, PUT and PATCH. See Send a body. |
| Into | The variable that keeps the answer. Pick one, or New Value to make one. |
| Timeout | How 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.
- Tap the {} button next to Body. A field named
field1appears. - Tap
field1and type the name the API expects, likeusername. - Give it a value: type it, or tap ⚡︎ beside the name to link a variable or a formula.
- 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 is | Read it with |
|---|---|
{ "accessToken": "eyJ…", "firstName": "Emily" } in session | session.accessToken, session.firstName |
{ "products": [ { "title": "Phone" }, … ] } in catalog | catalog.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 withitem.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:
- Add a Try step.
- Under its Do, tap Add action and add the Call API, plus the steps that use its answer.
- Under its Catch, add the steps to run if any of them fails, like a Set Value that keeps a message to show.
Inside Catch, error says what went wrong. The ⚡︎ menu lists its parts under From this action:
| Name | What it holds |
|---|---|
error.message | What went wrong, in words, like "The server answered with error 401." |
error.status | The status the server answered with, like 401. |
error.body | The 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:
| Status | What the debugger says |
|---|---|
| 400 | The server didn't understand the request: check the body and query. |
| 401 | The server doesn't know who's asking: check the headers, like Authorization, and their secrets. |
| 403 | The server won't allow it with these credentials. |
| 404 | There's nothing at that address: check the URL. |
| 429 | Too many requests: wait a moment, then try again. |
| 500 and up | The server had a problem of its own. Try again later. |
| Any other | The server refused the request. |
Other messages you may see:
| Message | What 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:
| Variable | Type | Starting value |
|---|---|---|
username | Text | emilys |
password | Text | emilyspass |
signingIn | True / False | Off |
signInError | Text | (empty) |
session | Anything | |
accessToken | Text | (empty) |
me | Anything |
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:
- Set Value:
signingInto On. - Set Value:
signInErrorto empty. Try, and in its Do:
- Call API: Url
https://dummyjson.com/auth/login, Method POST. Body fields:usernamelinked to the formulatrim(username),passwordlinked to the variablepassword, andexpiresInMinsset to 30. Intosession. - Set Value:
accessTokento the formulasession.accessToken. - Go to Screen: the profile screen.
In its Catch:
- Set Value:
signInErrorto the formulaerror.body.message ?? error.message.
- Call API: Url
- Set Value:
signingInto Off.
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.