How to Get Data from API Using send_request
The send_request action allows getting data from external APIs and processing the response in a scenario. This is useful for getting exchange rates, weather, order information from CRM, and other data.
How Does Data Retrieval Work?
- The
send_requestaction sends an HTTP request to the specified URL - Receives a response in JSON format
- Processes the response through
response_mappingor saves it in state - Data becomes available in the scenario through placeholders
Processing JSON Object in Response
Example 1: Getting Day of Week
Configuration:
{
"url": "http://worldclockapi.com/api/json/est/now",
"method": "GET",
"response_mapping": {
"dayOfTheWeek": "today"
}
}
Explanation:
response_mappingspecifies which placeholder to write the response parameter to- Parameter from API
dayOfTheWeekwill be available in the constructor as{{today}}
Usage in scenario:
After executing the send_request action with this configuration, you can use {{today}} in subsequent scenario blocks to display the day of week.
Processing Array of Objects in Response
Example 2: Getting Exchange Rates
Configuration:
{
"url": "https://api.privatbank.ua/p24api/pubinfo?json&exchange&coursid=5",
"method": "GET",
"response_mapping": {
"list_obj": {
"text": "{{ccy}}/{{base_ccy}}",
"value": "{{buy}}"
}
},
"save_mapped_list_obj": "currency_mapped_obj"
}
Configuration explanation:
list_obj— indicates that we are processing an array of objectstext— text that will be displayed (can contain placeholders from API response)value— value that will be saved when selected (can contain placeholders from API response)save_mapped_list_obj— variable name where the processed array will be saved
Example API response:
[
{
"ccy": "USD",
"base_ccy": "UAH",
"buy": "36.56860"
},
{
"ccy": "EUR",
"base_ccy": "UAH",
"buy": "40.20000"
}
]
Processing result:
After processing, currency_mapped_obj will contain:
[
{
"text": "USD/UAH",
"value": "36.56860"
},
{
"text": "EUR/UAH",
"value": "40.20000"
}
]
Usage in "List" block:
In the "List" block, specify variable {{currency_mapped_obj}} as the data source. The block will receive exchange rate data and display them as buttons.
Using selection result:
After the user selects a currency, you can use the result in the scenario, for example, to display the currency rate.
Saving Unprocessed Request Result
If you add the save_responce configuration parameter to the send_request block, the request execution result will be written to state in the variable:
{
"url": "https://api.example.com/data",
"method": "GET",
"save_responce": "currency_arr"
}
After executing the request, the entire JSON response will be available in the {{currency_arr}} variable for further processing through action jail or other mechanisms.
Error Handling
If the API request ended with an error:
- Action returns
"not_ok" response_mappingis not executedsave_responceis not saved
In the scenario, you can handle the "not_ok" event for:
- Retry
- Sending a message to the user
- Using a backup data source
Limitations
response_mappingworks only with successful responses (HTTP 200)- Maximum response size is limited by system settings
- Complex nested structures may require additional processing through action jail