ToolJet/docs/versioned_docs/version-3.16.0-LTS/data-sources/restapi/querying-rest-api.md

148 lines
9.8 KiB
Markdown
Raw Normal View History

2025-08-04 06:49:49 +00:00
---
id: querying-rest-api
title: Querying REST API
---
## Creating Queries
Once you have connected to the REST API data source, you can easily write queries and interact with the REST API in the ToolJet application. Follow these steps to get started:
1. Click on the **+ Add** button in the query manager at the bottom panel of the editor.
2. Select **REST API** from the Data Source section.
3. Enter the required query parameters.
4. Click **Preview** to view the data returned from the query or click **Run** to execute the query.
:::tip
2025-08-08 05:22:02 +00:00
You can also transform the query results using the **[Transformations](/docs/app-builder/custom-code/transform-data)** feature.
2025-08-04 06:49:49 +00:00
:::
ToolJet supports the following REST HTTP methods:
- **GET**
- **POST**
- **PUT**
- **PATCH**
- **DELETE**
<img className="screenshot-full img-full" src="/img/datasource-reference/rest-api/listops-query.png" alt="REST API query operations" />
2025-08-04 06:49:49 +00:00
:::info
To receive the string `"undefined"` instead of an actual `undefined` value, it must be explicitly handled in the query manager. <br/>
Example: `"address": "{{components.table1.selectedRow?.address ?? 'undefined'}}"`
:::
### Additional header
Whenever a request is made to the REST API, a **tj-x-forwarded-for** header is added to the request, the value of the header will be the IP address of the user who is logged in to the ToolJet application. This header can be used to identify the user who is making the request to the REST API.
<img className="screenshot-full img-full" src="/img/datasource-reference/rest-api/request-header.png" alt="REST API - additional header querying"/>
2025-08-04 06:49:49 +00:00
## Request/Content Types
REST API sends a **JSON** formatted body by default. If you want to send a different type of body, you can enter the appropriate headers in the **Headers** section.
For example, to send a **multipart/form-data** body, you can add the following header:
```javascript
Content-Type: multipart/form-data;
```
<img style={{ marginBottom:'15px' }} className="screenshot-full img-full" src="/img/datasource-reference/rest-api/form-headers.png" alt="REST API query headers" />
2025-08-04 06:49:49 +00:00
2026-02-12 07:24:36 +00:00
<img className="screenshot-full img-full" src="/img/datasource-reference/rest-api/form-body.png" alt="REST API query body" />
2025-08-04 06:49:49 +00:00
<br/><br/>
:::info Handling OAuth Token via REST API
To obtain an OAuth token via REST API, add the following custom header: <br/>
`Content-Type: application/x-www-form-urlencoded`
:::
## Response Types and Handling
REST APIs can return data in a variety of formats, including **JSON** and **Base64**. JSON is a common format used for data exchange in REST APIs, while Base64 is often used for encoding binary data, such as images or video, within a JSON response.
When the response **content-type** is **image**, the response will be a **base64** string.
<details id="tj-dropdown">
<summary>**Example JSON response**</summary>
```json
[
{
"id": "3",
"name": "Apple iPhone 12 Pro Max",
"data": {
"color": "Cloudy White",
"capacity GB": 512
2025-08-04 06:49:49 +00:00
}
},
{
"id": "5",
"name": "Apple iPhone 13",
"data": {
"color": "Midnight",
"capacity GB": 256
2025-08-04 06:49:49 +00:00
}
},
{
"id": "10",
"name": "Apple iPad Pro",
"data": {
"color": "Space Gray",
"capacity GB": 128
2025-08-04 06:49:49 +00:00
}
}
]
```
The JSON response can be easily loaded on the components like **table** and **listview** using `{{queries.<queryname>.data}}`
</details>
You can also use JS methods like **map** to load data on components like **dropdown** using **`{{queries.restapi1.data.map(i => i.title)}}`**
2026-02-12 07:24:36 +00:00
<img className="screenshot-full img-full" src="/img/datasource-reference/rest-api/dropdown-query.png" alt="REST API dropdown component mapping " />
2025-08-04 06:49:49 +00:00
<details id="tj-dropdown">
<summary>**Example base64 response**</summary>
```base64
iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAA/FBMVEVAYt79/f1AYt/9/f79/ftAY9s/Y93v/P89ZNv8/v38/f/9/vj9/vr+/Pz//P49ZNw8ZddUb86QpMlCYOX1//9AYeI6XdaXp+C1x+nL2fj+/vU2WMZVb8iPnsU3Xt00WNY7ZtU0WMuJncs8W8JDY801W986V9BTacRleMF+kNClt+CsvuFtiNYvVMRcedaZq9Lb5/eCl9K8zOJJWcqlr9xdb8C6w+w7XsCmvt3S5fs5ac1whs7l8v/6//B9j8wvVLrO2+o+Y7t6kMODotxPbL0+WOLf3/aesdVmfbvL5PentOmDl99RbtdMXcGOnNqTqdp+luIyVrLr7Pq/2/3mMzS8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Y/VVmW8NNOKPSC3ieoAN0zFqV1tDQsAtci9LIb1mGdD+RKHmxuQCLUmU5pMFrSsdsuJPNwaWsEhYVLvPk1p6lzmpgwOdmrYAFCRSK6InHwzm3Dk2R8tjIKkoSvur6iaE7TVVBI3HEnyOv+3DsZ9hPIzGlmk9a7jOdK/OzzRJPw3aeV54V75QWft4abSTVkbao+7zNZ2no+8gJeb/PuTKc0efLfnVcrw4F7Z8lsb/O1z8AYlpHMD9ZOKtoN/2zC+/1qULjn2zaZr6trGmAdynz3qCq4O5Lxb1/hP8Zt/QLTSO56O9dKuHsyPLX1C8v+zWv7OoBpBvVLulwmessA/Xb4mvJT1xZPu6eQu/d0uQNB1rvlAL17jlGcZn337jk+exLTrZlKvfmL5O78DKUBP//44+TRCkMQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEGQr8n/ABGyzAUL7/lcAAAAAEl
```
</details>
Read the guide on **[loading base64 data](/docs/how-to/loading-image-pdf-from-db)**.
<img className="screenshot-full" src="/img/datasource-reference/rest-api/image-comp-query.png" alt=" REST API image component mapping" />
2025-08-04 06:49:49 +00:00
## Retry on Network Errors
ToolJet provides an option to automatically retry REST API requests in case of certain network errors or specific HTTP status codes. By default, this feature is enabled and will retry the request up to 3 times in case of failure. This feature can be toggled on or off at both the data source level and the individual query level. When enabled, retries will occur for the following scenarios:
1. Specific HTTP status codes: 408, 413, 429, 500, 502, 503, 504, 521, 522, 524.
2. Network errors:
- **ETIMEDOUT**: One of the timeout limits was reached.
- **ECONNRESET**: Connection was forcibly closed by a peer.
- **EADDRINUSE**: Could not bind to any free port.
- **ECONNREFUSED**: Connection was refused by the server.
- **EPIPE**: The remote side of the stream being written has been closed.
- **ENOTFOUND**: Couldn't resolve the hostname to an IP address.
- **ENETUNREACH**: No internet connection.
- **EAI_AGAIN**: DNS lookup timed out.
You can configure this feature at two levels:
### Data Source Level
In the REST API data source configuration, you'll find a toggle for **Retry on network errors** This sets the default behavior for all queries using this data source.
<img className="screenshot-full img-full" src="/img/datasource-reference/rest-api/rest-api-data-source.png" alt="ToolJet - Data source - REST API" />
2025-08-04 06:49:49 +00:00
### Query Level
In the query builder for each REST API query, you'll find a similar toggle for for **Retry on network errors** under the **Settings** tab. This sets the behavior for that specific query.
<img className="screenshot-full img-full" src="/img/datasource-reference/rest-api/query-level.png" alt="ToolJet - Data source - REST API" />
2025-08-04 06:49:49 +00:00
:::info
If the data source-level configuration is enabled but a specific query has it disabled, the query-level setting takes precedence.
:::