How to integrate your chatbot with a database
In this article we will show you how you can use your chatbot to extract data and send that data to a database by using APIs. We will do that by showing you how to:
- Extract data or user input
- Set up a database
- Send data to a database (POST)
In addition to pushing data to a database, it is also possible to retrieve data from a database (GET). Together, these two are a strong combination for providing automated personalized customer journeys.
Extracting data and user input
Before we start filling our database, we need to extract some data or user input to send to our database. Within the Flow.ai platform there are 2 common places where to find data.
1. User input
The first place to extract data is by using user input. User input can contain valuable information such as an e-mail address, phone number, or name. Our article How to capture user input shows you how to extract that information as a parameter.
2. Payload
Second, you can find a lot of data within the payload. When integrating with messaging channels, such as WhatsApp or Facebook Messenger, some data is already provided by that channel. You can already extract that from the channels metadata without asking for a name or phone number.
Setting up a database
In this example, we’ll be working with a database from Restdb. If you’re using a different database, don’t worry, the concept will be similar.
After creating an account you can set-up your database. In the example below, you will see the database ‘userdata’ that contains 3 fields: name, phone, and email.

Sending data to a database (POST)
To send data from your Flow.ai chatbot to the RestDB database, we’ll be working with APIs. If you’re using RestDB you can access developers mode by clicking the gear/settings icon in the top right. Then head over to API docs -> Javascript. From there, you can copy and paste the code from RestDB to an action in Flow.ai.

The next step is to alter the code in Flow.ai’s Action, so that we can actually send the extracted data to our database. Therefore, we’ll create 3 variables. We check if they exist and if so, we’ll fill those variables. New to actions and coding? Have a look at code best practices
async payload => {
var fullName = "-"
var phoneNumber = "-"
var email = "-"
// Check if name exists
if("fullName" in payload.user.profile) {
fullName = payload.user.profile.fullName
}
// Check if phoneNumber
if("phoneNumber" in payload.user.profile) {
phoneNumber = payload.user.profile.phoneNumber
}
// Check if the param "email" exists
if(Array.isArray(payload.params.email)) {
email = payload.params.email[0].value
}
// API (POST) to send data to database
var request = require("request");
var options = { method: 'POST',
url: 'https://integratedb-74ce.restdb.io/rest/userdata',
headers:
{ 'cache-control': 'no-cache',
'x-apikey': 'YOUR SECRET API KEY',
'content-type': 'application/json' },
body: {
name: `${fullName}`,
phone: `${phoneNumber}` ,
email: `${email}`
},
json: true };
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
}
Our code is now completed and we can add it to our Flow and test it by using the try it out function or by using WhatsApp testing.

Our database is now filled with 3 values:
- The name of the user (acquired by payload)
- The phone number of the user (acquired by payload)
- The email address of the user (acquired by user input)

Retrieving data from a database (GET)
When data is stored in a database you can also use that data to improve the customer experience. Read more about retrieving data from a database.