Call API
Sends a request to a web API and saves the answer. JSON answers become values formulas can read: forecast.current.temperature.
Category: Web & APIs · Can fail (wrap it in Try to handle that) · Takes time: the next step waits for it
#When to use it
Use it to get or send data over the web: the weather, signing in, saving to your own server.
#Example
Signing in to an API:
- Url:
https://dummyjson.com/auth/login - Method: POST
- Body: tap the {} button (Use named fields, like a JSON body), then add
usernameandpasswordfields, each linked with ⚡︎ to what was typed - Into: session
#Good to know
- Url has to start with https://, or the step fails.
- Body is sent as JSON.
- A header can be a secret: pick it with ⚡︎ under Secrets. It's read only when the request is sent, and it never shows on screen or in the debugger. A secret with no value yet makes the step fail.
- An answer with an error status is a failure. Catch it with Try;
error.statusanderror.bodysay why. - Preview's debugger shows the address, the status and the answer.
- Put it in When Loading to load data when a screen opens.
#Add it
Select a view, open the Inspector's Actions tab, pick when it runs, tap Add action and choose Call API (under Web & APIs). Tap the step to open its settings.
#Settings
| Setting | What it does | Kind | Default |
|---|---|---|---|
| Url | The whole web address, like https://dummyjson.com/products. It must start with https://. | Text | Required |
| Api | An API saved in an older version of the app, which adds its base address and headers. New apps put the whole address in URL. | API | Optional |
| Method | GET reads, POST creates, PUT/PATCH change, DELETE removes. | Choice: GET, POST, PUT, PATCH, DELETE | GET |
| Query | Values added to the address: ?city=Lahore. | Fields | Optional |
| Headers | Extra lines sent with the request, like Authorization. Pick a secret with ⚡︎ for an API key. | Fields | Optional |
| Body | What to send, as JSON. | Any value | Optional |
| Into | Where to save the answer. | Variable | Optional |
| Timeout | Seconds to wait before giving up. | Number | 30 |
#What it gives back
The answer's body: JSON becomes a value, anything else text. A status that isn't 2xx is a failure (catch it with Try); error.status and error.body say why.