Configure your API integration
A Chatlayer API step is available in the Action blocks to enable you to create bot messages based on user-specific information and other external data, and to redirect your users to different flows based on your own business logic.
You can use this solution on any platform that supports receiving and responding to HTTP requests.
The API step sends a request to your backend server.
Send an API request
To configure your API step:

Click on API.
Configure your parameters. See below for more details.
You can only define a request body when your request method is POST or DELETE.

Under the Query tab, add query parameters and/or a body payload by defining key value combinations. Each key can has 3 possible value types:
text: static text.
variable: a user session variable. The value of the variable will be stored as value for the key. Dot and array notation are supported, for example:
users[0].firstnamedialogstate: select a dialog state from the dropdown. The dialog state ID will be stored as value for the key. This ID can be used to redirect the user to a certain dialog state based on your business logic when sending back the API response.

The Headers tab is where you configure your headers.

Body is where you can define a request body request in all HTTPS methods.
Under the Body tab, add query parameters and/or a body payload by defining key value combinations. Each key can has 3 possible value types:
text: static text.
variable: a user session variable. The value of the variable will be stored as value for the key. Dot and array notation are supported, for example:
users[0].firstnamedialogstate: select a dialog state from the dropdown. The dialog state ID will be stored as value for the key. This ID can be used to redirect the user to a certain dialog state based on your business logic when sending back the API response.
In this example, representing a money transfer, we send five keys in the body payload of an HTTPS POST request to our API endpoint https://chatlayer-integration-demo.glitch.me/transaction.

The amount key will have the value of user session variable
transfer_amount(ex: 500).The destination key will have the value of user session variable
transfer_destination(ex: Elon Musk).The accountType key will have the value of user session variable
card_type(ex: savings_account).The transactionSuccess key will have the dialog state identifier for the ‘successful transaction’ dialog state. This identifier can be used in the response of this API request to redirect the user to a new dialog state.
The transactionNoMoney key will have the dialog state identifier for the ‘unsuccessful transaction’ dialog state. This identifier can be used in the response of this API request to redirect the user to a new dialog state.
The test key will have a value of ‘5’.
This will result in the following body payload:
{
"amount": 500,
"destination": "Elon Musk",
"accountType": "savings_account",
"transactionSuccess": "successful transaction",
"transactionNoMoney": "unsuccessful transaction",
"test": 5
}
Configuration is where, if you have an API configuration in Settings>API, you will see them here.
Listen for an API response
You do not need to configure the API plugin to listen for a response. This is done automatically and the API step will listen for what your API returns.
Make sure you include the correct content type in the header: content-type: application/json;
Example
This example demonstrates one API endpoint for transferring an amount of money from an account type (regular or savings) to someone. We will redirected the user to a certain dialog state based on the transaction result.
app.post('/transaction', function (req, res) {
let nextDialogstate;
const { amount, destination, accountType, transactionSuccess, transactionNoMoney = req.body;
// get account type (regular - savings account)
const account = account_synonyms[accountType];
if( accounts[account].amount + accounts[accounthlimit — amount < ) {
nextDialogstate = transactionNoMoney
} else {
// transfer amount
accounts[account].amount —= amount;
nextDialogstate = transactionSuccess;
}
res.json({
action: {
nextDialogstate,
},
session: {
namespace: 'account',
data: {
limit: accounts[account].limit,
amount: accounts[account].amount
}
},
})
});We receive the body payload object as defined in the Chatlayer API step. If the user doesn’t have a sufficient amount of money on his account we set the next dialog state to ‘transactionNoMoney’. Else we subtract the desired amount and set the next dialog state to ‘transactionSuccess'.
As a response for the request we send the next dialogs state to redirect the user to that state and we save the amount of money and the limit of his account in his session data under the namespace account. This data can be used in that next dialog state.

As an alternative solution you could also send that chat message as a response of the API plugin requests by using the messages key.
Last updated
Was this helpful?



