ApiResponse - Liquid Object Reference
On this page
Description
An ApiResponse holds the result of an outbound HTTP request made with the api tag. Inside an {% api %}...{% endapi %} block that specifies a url, the response is exposed as the response object once the request completes, so you can branch on the status code and read the returned data.
The response object is only populated for synchronous requests (async: false). With async: true the request is dispatched fire-and-forget and no response is returned to the template.
Attributes
| Attribute | Type | Description |
|---|---|---|
status |
Number | The HTTP status code of the response (e.g. 200, 404, 500). It compares numerically, so response.status >= 200 and response.status < 300 tests for success. |
body |
Map | The HTTP response body. When the endpoint returns JSON it is parsed automatically, so nested fields are reachable directly (response.body.access_token, response.body.data). Use the serialize filter to turn a Map or List back into a JSON string for storage or output. |
headers |
Map | The HTTP response headers, keyed by header name. |
Examples
Read a successful JSON response
```liquid
{% liquid new Map headers = ‘{ “Accept”: “application/json” }’ assign result = ‘’ api url: ‘https://api.example.com/v1/status’, method: ‘GET’, headers: headers, async: false if response.status >= 200 and response.status < 300 assign result = response.body.message endif endapi %} {{ result }} ```
Handle an error status
Always check status before trusting body, and capture the raw body when a call fails so the error is visible while developing:
```liquid
{% liquid api url: ‘https://api.example.com/v1/orders’, method: ‘GET’, headers: headers, async: false if response.status >= 200 and response.status < 300 assign orders = response.body.orders else assign error = response.body | serialize debug status: response.status, error: error endif endapi %} ```
Read a response header
```liquid
{% api url: ‘https://api.example.com/v1/status’, method: ‘GET’, async: false %} {% assign request_id = response.headers[‘x-request-id’] %} {% endapi %} ```
See the api tag reference for how to issue the request, set headers, and send a request body.
Was this article helpful?
Thanks for your feedback! It helps us improve our docs.