StableGuide
Call an external API
Goal: show live data from another service in your app and send data to it. The example uses a weather service with an API key, but the steps are the same for any REST API.
Call an external API
Goal: show live data from another service in your app and send data to it. The example uses a weather service with an API key, but the steps are the same for any REST API.
Data needed
- The API's base URL and the parameter it expects (here a city).
- Its API key, stored as a secret named
WEATHER_API_KEYin Settings → Environment & secrets for each environment you use (Development for the builder, Production for the live app).
Step 1 — define the API source
- Open APIs and click New API → Blank GET request. Name it Weather.
- Request: method
GET, URLhttps://api.example-weather.com/v1/current?city={{params.city}}. The{{params.city}}placeholder is filled in by whatever calls the API. - Authentication: type API key header, header name
X-API-Key, secretWEATHER_API_KEY. The badges show in which environments the secret is set. - Response path:
dataif the useful part of the response is nested, otherwise leave empty. - Allow calling from the app stays on so pages and buttons can use it (turn it off for APIs that only workflows should call).
- In Test request, enter
city = Paris, choose Development and click Send test request. On success, click Save fields for the data picker so the response fields appear by name when binding.
Step 2 — show the data on a page
- Drop a Text component on the page. In the Data tab choose API → Weather.
- Bind the text's content to the response field (for example API › Weather.temperature).
- For lists (search results, products), drop a Table or Cards, set the Data source to API and the Items path to the list in the response (for example
items).
Step 3 — call it from a button
- On a button, Actions → onClick → Add action → Call API (group Data). Choose GET Weather.
- Parameters: add
city= Form › city (or App state › selectedCity). - Save result as:
weather. - Add Show message with
It's {{results.weather.temperature}}° in {{results.weather.city}}.
Example
onClick
1. Call API Weather city = Form › city → weather
2. Show message "It's {{results.weather.temperature}}° in {{results.weather.city}}"Step 4 — call it from a workflow
In a workflow, add a Call API step, choose the API, fill its parameters (city = {{record.city}}) and set Save result as weather. Later steps use Action results › weather.temperature. Unlike the action, the workflow step fails the run when the API answers with an error status.
Resulting behaviour
- Every request is made by the server. The browser never sees the URL, headers or the key; it receives only the (mapped) response.
- The key is read from the secret for the current environment: Development in the builder preview, Staging on the staging app, Production on the live app.
- Responses larger than 2 MB are rejected; requests time out after the API's Timeout (default 10 s).
Variations
- POST with a JSON body: change the method to
POSTand write the body as JSON with placeholders, e.g.{"email": "{{params.email}}"}.Content-Type: application/jsonis added automatically. - Pagination or search: add query parameters such as
page = {{params.page}}and pass them from page state. - Keep an API private to workflows: switch off Allow calling from the app; buttons then get "This API can only be used from workflows."
- Read a whole table from an API instead: connect a REST data source and map an entity to it (read-only). See External data sources.
Common mistakes
- The key typed directly into a header value. Use
{{secret.WEATHER_API_KEY}}or the Authentication section; plain header values are stored in the project definition. - Secret set for Development only: the live app fails with "API "Weather" needs the secret WEATHERAPIKEY in production."
- Calling an API from a component binding that no visible component actually uses: only APIs referenced by a component's data binding may be called by the published app.
- Private or internal hosts (
localhost,10.x,192.168.x): blocked by the safety guard on production servers.