PadCodedocs

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 username and password fields, each linked with ⚡︎ to what was typed
  • Into: session
A Call API step open: Url, Method POST, Query, and an Authorization header that reads Bearer {accessToken}.

#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.status and error.body say 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

SettingWhat it doesKindDefault
UrlThe whole web address, like https://dummyjson.com/products. It must start with https://.TextRequired
ApiAn API saved in an older version of the app, which adds its base address and headers. New apps put the whole address in URL.APIOptional
MethodGET reads, POST creates, PUT/PATCH change, DELETE removes.Choice: GET, POST, PUT, PATCH, DELETEGET
QueryValues added to the address: ?city=Lahore.FieldsOptional
HeadersExtra lines sent with the request, like Authorization. Pick a secret with ⚡︎ for an API key.FieldsOptional
BodyWhat to send, as JSON.Any valueOptional
IntoWhere to save the answer.VariableOptional
TimeoutSeconds to wait before giving up.Number30

#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.

#See also