In Zenvia Customer Cloud, the API Call feature allows users to include an API within their chatbot. An Application Programming Interface (API) is a way for one program to interact with another, hence API calls are the means by which they interact. Thus, it's possible to use an API call to send data to your CRM, query customer data in databases, or even send external communications.
How to Access
The API Call content is available in the Constructor and to access it, simply click on the three dots of the block or anywhere within it.
This will open the Block Details modal, then follow these instructions:
- Click on Add content > Conversation actions > API Call.
- In Method, specify the type of your API, whether it's: POST, GET, DELETE, PUT, or PATCH.
- Provide the URL, including a link to this API.
- In Header, include the respective code, editing the code will be similar to any common editor. For example, it could be Authorization, Application, etc.
- In Body, include the code. Remember that this will depend on the API. For example, if we have a JSON application, in the body, the code will be JSON.
After configuring the API Call in your chatbot, if you want to save and use its response as a variable in the flow, you need to create a new block to access this information.
Why create a new block?
The execution of a block in Zenvia Customer Cloud follows this order:
Sends the message.
Receives messages.
Saves variables.
Executes actions (receives the API response).
Validates destinations.
Therefore, the variables will only be available after the block has been fully executed. If you try to use them within the same block where the API Call was configured, they will not exist yet.
Step-by-step to create the block and use the variables
In the Bot Builder, click on Add block.
Give the block a name, such as ReturnAPI.
Configure the message using the returned variables:
3.1: In the new block, add the message you want to send to the customer. To access the data returned by the API Call, use the default variable: <? $resposta_api.body ?>
.
3.2: If you want to access a specific value within the returned data structure, just follow the hierarchy.
For example: suppose the API response contains the following path: body > events > extra1
. To display the value of extra1
in the message, use: $resposta_api.body.events.extra1
This variable will automatically be filled with the value returned by the API.
Important: The content of the variable <? $resposta_api.body ?>
remains available in the flow until a new API Call is made. In other words, whenever a new call is made, this variable will be updated with the most recent response.
In the bot flow, connect the block where the API Call is configured to this new block. This way, after execution and variable saving, the bot will automatically proceed to the block where the variables can be used.
If the API response includes an array of objects, as in the example below, where you want to access the communication channel of the first event, the code to display this information would be:
"resposta_api": {
"body": {
"events": [
{
"extra1": null,
"extra2": null,
"channel": "widget",
"eventName": "new_dialog",
"assistantId": 142,
"environment": "dev"
}
]
},
"code": 200,
}
In this example, events[0]
refers to the first object within the events
array, and channel
is the field you want to access and display in the chatbot conversation.