Sending data to Airtable (POST)

Data gathered inside of bots is often sent to an external database. An easy way of doing this is by ingrating with Airtable. Airtable is a tool that allows you to create a spreadsheet that you can talk to with an API.

In this tutorial we will set up an integration with Airtable. Because of all the code it looks quite technical, but in fact it's pretty easy.

If you're new to using variables in Chatlayer.ai, follow this tutorial first.

  • Start by building a short flow that you use to gather some data about your user, for example:

  • This flow adds the variables "customerType" and "firstName" to the session of the user.

  • Set up an Airtable that has a column for each variable you want to save. Rows will be added for each customer.

In this tutorial, we will be using this Airtable. Feel free to reuse it!

  • At the point in the flow where you want to send the data to Airtable, add an Action containing a Code plugin.

  • In our example, we want to send the customer's type and first name. Furthermore, we want to jump to another bot dialog as soon as the data has been sent. Start by adding these parameters at the top of your code editor.

  • Then add the code below to the plugin and save your Action dialog.

const body = {
"records": [{
"fields": {
"First Name": args.firstName,
"Customer Type": args.customerType
}
}]
}
const airtableResult = await fetch('https://api.airtable.com/v0/(yourAppId)/(yourTableName)', {
method: 'POST',
body: JSON.stringify(body),
headers: {
'Authorization': 'Bearer (insert token here)',
'Content-Type': 'application/json'
}
}).then(r => r.json())
ChatlayerResponseBuilder()
.setNextDialogState(args.nextDialogState)
.send()

Remember to get the right app id, table name and bearer token for your Airtable. You can find it here.

  • Et voilà! Now, every time a user goes through your flow, their data will be sent to Airtable, and they will continue on to the Success bot dialog.

  • If there are any errors with your connection to Airtable, you can find them in the Error logs tab in Chatlayer.ai.