docs/versioned_docs/version-2.24.0/data-sources/restapi.md
ToolJet can establish a connection with any available REST API endpoint and create queries to interact with it.
To establish a connection with the REST API data source, you can either click on the Add new button located on the query panel or navigate to the Data Sources page through the ToolJet dashboard.
ToolJet requires the following to connect to a REST API data source:
Once you have connected to the REST API data source, follow these steps to write queries and interact with a REST API in the ToolJet application:
+Add button to open the list of available Data Sources.Preview to view the data returned from the query or click Run to execute the query.:::tip Query results can be transformed using the Transformations feature. :::
<div style={{textAlign: 'center'}}> </div>ToolJet supports the REST HTTP methods GET, POST, PUT, PATCH, and DELETE. You can select the method from the dropdown menu.
<div style={{textAlign: 'center'}}> </div>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.
<div style={{textAlign: 'center'}}> </div>The plugin will send a JSON formatted body by default. If a file object from a FilePicker widget is set as a value, the body is automatically converted to be sent as a multipart/form-data request.
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.
[
{
"id": 1,
"title": "Fjallraven - Foldsack No. 1 Backpack, Fits 15 Laptops",
"price": 109.95,
"description": "Your perfect pack for everyday use and walks in the forest. Stash your laptop (up to 15 inches) in the padded sleeve, your everyday",
"category": "men's clothing",
"image": "https://fakestoreapi.com/img/81fPKd-2AYL._AC_SL1500_.jpg",
"rating": {
"rate": 3.9,
"count": 120
}
},
{
"id": 2,
"title": "Mens Casual Premium Slim Fit T-Shirts ",
"price": 22.3,
"description": "Slim-fitting style, contrast raglan long sleeve, three-button henley placket, light weight & soft fabric for breathable and comfortable wearing. And Solid stitched shirts with round neck made for durability and a great fit for casual fashion wear and diehard baseball fans. The Henley style round neckline includes a three-button placket.",
"category": "men's clothing",
"image": "https://fakestoreapi.com/img/71-3HjGNDUL._AC_SY879._SX._UX._SY._UY_.jpg",
"rating": {
"rate": 4.1,
"count": 259
}
},
{
"id": 3,
"title": "Mens Cotton Jacket",
"price": 55.99,
"description": "great outerwear jackets for Spring/Autumn/Winter, suitable for many occasions, such as working, hiking, camping, mountain/rock climbing, cycling, traveling or other outdoors. Good gift choice for you or your family member. A warm hearted love to Father, husband or son in this thanksgiving or Christmas Day.",
"category": "men's clothing",
"image": "https://fakestoreapi.com/img/71li-ujtlUL._AC_UX679_.jpg",
"rating": {
"rate": 4.7,
"count": 500
}
},
{
"id": 4,
"title": "Mens Casual Slim Fit",
"price": 15.99,
"description": "The color could be slightly different between on the screen and in practice. / Please note that body builds vary by person, therefore, detailed size information should be reviewed below on the product description.",
"category": "men's clothing",
"image": "https://fakestoreapi.com/img/71YXzeOuslL._AC_UY879_.jpg",
"rating": {
"rate": 2.1,
"count": 430
}
}
]
The JSON response can be easily loaded on the components like table and listview using {{queries.<queryname>.data}}
You can also use JS methods like map to load data on components like dropdown using {{queries.restapi1.data.map(i => i.title)}}
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/3mMzS8AAAKxUlEQVR4nO2cC1vbthrHLVmWJUuW3dlywWYkIRAorG1K6SgjgV5g3a3jbOf7f5fzygngcCvbeobN8/7a8rQl7eO/Jf3fiyV7HoIgCIIgCIIgCIIgCIIgCIIgCIIgCIIgCIIgCIIgCIIgCIIgCIIgCIIgCIL8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a7jOdK/OzzRJPw3aeV54V75QWft4abSTVkbao+7zNZ2no+8gJeb/PuTKc0efLfnVcrw4F7Z8lsb/O1z8AYlpHMD9ZOKtoN/2zC+/1qULjn2zaZr6trGmAdynz3qCq4O5Lxb1/hP8Zt/QLTSO56O9dKuHsyPLX1C8v+zWv7OoBpBvVLulwmessA/Xb4mvJT1xZPu6eQu/d0uQNB1rvlAL17jlGcZn337jk+exLTrZlKvfmL5O78DKUBP//44+TRCkMQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEEQBEGQr8n/ABGyzAUL7/lcAAAAAElFTkSuQmCC
Read the guide on loading base64 data
<div style={{textAlign: 'center'}}> </div>ToolJet’s REST API data source supports OAuth 2.0 as the authentication type.
Before setting up the REST API data source in ToolJet, we need to configure the Google Cloud Platform to gather the API keys required for the authorization access.
Google Cloud Platform provides access to more than 350 APIs and Services that can allow us to access data from our Google account and its services. Let's create an OAuth application that can be given permission to use our Google profile data such as Name and Profile picture.
https://app.tooljet.com/oauth2/authorize (if you’re using ToolJet cloud)http://localhost:8082/oauth2/authorize (if you’re using ToolJet locally)Let's follow the steps to authorize ToolJet to access your Google profile data:
:::info
You can rename the data source by clicking on its default name restapi
:::
In the Base URL field, enter the base URL https://www.googleapis.com/oauth2/v1/userinfo; the base URL specifies the network address of the API service.
Select Authentication type as OAuth 2.0
Keep the default values for Grant Type, Add Access Token To, and Header Prefix i.e. Authorization Code, Request Header, and Bearer respectively.
Enter Access Token URL: https://oauth2.googleapis.com/token; this token allows users to verify their identity, and in return, receive a unique access token.
Enter the Client ID and Client Secret that we generated from the Google Console.
In the Scope field, enter https://www.googleapis.com/auth/userinfo.profile; Scope is a mechanism in OAuth 2.0 to limit an application's access to a user's account. Check the scopes available for Google OAuth2 API here.
Enter Authorization URL: https://accounts.google.com/o/oauth2/v2/auth; the Authorization URL requests authorization from the user and redirects to retrieve an authorization code from identity server.
Create three Custom Authentication Parameters:
| Params | Description |
|---|---|
| response_type | code ( code refers to the Authorization Code) |
| client_id | Client ID |
| redirect_uri | http://localhost:8082/oauth2/authorize if using ToolJet locally or enter this https://app.tooljet.com/oauth2/authorize if using ToolJet Cloud. |
Keep the default selection for Client Authentication and Save the data source.
Let’s create a query to make a GET request to the URL, it will pop a new window and ask the user to authenticate against the API.
GET and enabe the Run query on application load?ToolJet’s REST API data source supports Bearer Token as the authentication type. Bearer Token is a security token that is issued by the authentication server to the client. The client then uses the token to access the protected resources hosted by the resource server.
:::info
You can rename the data source by clicking on its default name restapi
:::
In the Base URL field, enter the base URL. The base URL specifies the network address of the API service. For example, http://localhost:3001/api/bearer-auth
Enter the Headers if required. Headers are key-value pairs to include as headers with REST API requests.
Select Authentication type as Bearer from the dropdown.
Enter the Token in the field. The token is a security token that is issued by the authentication server to the client. The client then uses the token to access the protected resources hosted by the resource server.
<div style={{textAlign: 'center'}}> </div>Now you have option to select the SSL Certificate if required. SSL certificate is used to verify the server certificate. By default, it is set to None. You can provide the CA Certificate or Client Certificate from the dropdown.
server.crt file and paste it in the CA Cert field. server.crt file is the certificate file that is used to verify the server certificate.client.key, client.crt, and server.crt files are the certificate files that are used to authenticate with the server. Copy the content of client.key file and paste it in the Client Key field. Copy the content of client.crt file and paste it in the Client Cert field. Copy the content of server.crt file and paste it in the CA Cert field.Once you have configured the REST API data source, click on the Save button.
Create a query to make a GET request to the URL, and it will return a success message if the token is valid.