Table of Contents

Send WhatsApp Messages with Landbot API

Elisa Updated by Elisa

It's possible to send Message Templates to your WhatsApp contacts using our API!

Let's look in detail how to do it!

Requirements to send WhatsApp Message Templates

1. The WhatsApp Business Channel

You need to have your official WhatsApp channel, for now, it doesn't work in WhatsApp Testing channels.

2. The user's Opt-in

You need to previously have collected the user opt-in to send proactive messages to your WhatsApp contacts, please click here for more information.

3. An Approved WhatsApp Message Templates

The Message WhatsApp has to be validated previously by Meta, please click here for more information.

The API method to Send a template

Here you can find the url to the Dev Docs documentation: Link

API Requests Authorization

Landbot Platform API requires the following header for the authorization:

Header

Field

Description

Authorization

Default value:Β Token :agent_token

Content-Type

Default value:Β application/json

Find agent_token (API Token)

You can find your :agent_token (API Token) in https://app.landbot.io/gui/settings/account

Bear in mind that it is unique per teammate, and will behave according to the permissions granted to that teammate

Find Template ID

On the Message Templates section of your WhatsApp channel you will need to filter by Active templates

And you will see a column with the specific ID of the desired templates:

Find Template Language code

Currently the best way to find the template_language code is using the list method of the templates: Link to method

curl --location --request GET 'https://api.landbot.io/v1/channels/whatsapp/templates/' \--header 'Content-Type: application/json' 
\--header 'Authorization: Token XXXXXXXXX'

Find customer id

The customer id is the basic identifier, as templates need to be sent one by one, you will need to specify the customer_id

There are several methods you can use to find the customer id of a WhatsApp user that has done the opt-in:

1. Opt-in request response

If you use the end point for the opt-in of customers, in the response of the request, it will return the id value, that is the customer_id

2. Via API using phone as a filter

curl --location --request GET 'https://api.landbot.io/v1/customers/?search_by=phone&search={{phone number}}&opt_in=true' \--header 'Content-Type: application/json' 
\--header 'Authorization: Token XXXXXXXXX'

3. In the bot from the Analyze section under the column Customer Id

4. From the Inbox section in the top right panel

Request Examples

Here are different examples with the required code you need to send different models of Templates

1. Message Media Template Picture (with parameter) and Body (with parameters)

cURL snippet

curl --location --request POST 'https://api.landbot.io/v1/customers/:customerID/send_template/' \
--header 'Content-Type: application/json' \
--header 'Authorization: Token XXXXXXXXX' \
--data-raw '{
"template_id": 1082,
"template_params": {
"header": { "url":"https://upload.wikimedia.org/wikipedia/commons/6/6e/Mona_Lisa_bw_square.jpeg"
},
"body": {
"params": ["Variable one", "Variable two"]
}, "buttons":[]
},
"template_language": "en"
}'

2. Message Media Template Header (with parameters) and Body (with parameters)

cURL snippet

curl --location --request POST 'https://api.landbot.io/v1/customers/:customerID/send_template/' \
--header 'Content-Type: application/json' \
--header 'Authorization: Token XXXXXXXXX' \
--data-raw '{
"template_id": 1137,
"template_params": {
"header": { "params": ["variable one header"] },
"body": { "params": ["variable one body"] },
"buttons":[]
},
"template_language": "en"
}'

3. Message Media Template Header and Body (with parameters)

cURL snippet

curl --location --request POST 'https://api.landbot.io/v1/customers/:customerID/send_template/' \
--header 'Content-Type: application/json' \
--header 'Authorization: Token XXXXXXXXX' \
--data-raw '{
"template_id": 1074,
"template_params": {
"header": {
"params": []
},
"body": {
"params": ["Variable one", "Variable two","Variable three"]
},
"buttons":[]
},
"template_language": "en"
}
}'

4. Message Media Template with Buttons (Quick reply cannot use parameters)

cURL snippet

curl --location --request POST 'https://api.landbot.io/v1/customers/:customerID/send_template/' \
--header 'Content-Type: application/json' \
--header 'Authorization: Token XXXXXXXXX' \
--data-raw '{
"template_id": 1200,
"template_params": {
"header": {
"params": []
},
"body": {
"params": []
},
"buttons":[]
},
"template_language": "en"
}'

5. Message Media Template with Header (with parameter), Body (with parameters) and Button (with Parameters)

cURL snippet

curl --location --request POST 'https://api.landbot.io/v1/customers/:customerID/send_template/' \
--header 'Content-Type: application/json' \
--header 'Authorization: Token XXXXXXXXX' \
--data-raw '{
"template_id": 1243,
"template_params": {
"header": {
"params": ["variable one"]
},
"body": {
"params": ["variable one","variable two"]
},
"buttons":[]
},
"template_language": "en"
}'

6. Message Media Template with Body (with parameters) and Click to Action (Button with Parameter)

cURL snippet

curl --location --request POST 'https://api.landbot.io/v1/customers/:customerID/send_template/' \
--header 'Content-Type: application/json' \
--header 'Authorization: Token XXXXXXXXX' \
--data-raw '{
"template_id": 1725,
"template_params": {
"header": {
"params": []
},
"body": {
"params": ["variable one"]
},
"buttons":[ {
"params": ["variable_one"]
} ]
},
"template_language": "en"
}'

7. Message Media Template with Header (document) and Body (with parameter)

cURL snippet

curl --location --request POST 'https://api.landbot.io/v1/customers/:customerID/send_template/' \
--header 'Content-Type: application/json' \
--header 'Authorization: Token XXXXXXXXX' \
--data-raw '{
"template_id": 1726,
"template_params": {
"header": { "url": "https://www.domain.com/file.pdf",
"filename": "Nameofthefile"
},
"body": {
"params": ["variable one","variable two"]
},
"buttons":[]
},
"template_language": "en"
}'

Default Agent Assignation, and how to assign it to a bot via API

By default, once a Message Template has been sent via API, as described before, the chat will be assigned to the Agent which was used the API token

There are 2 possible approaches that you can use to redirect the user to an automated conversation, right after the first request (Send WhatsApp Message)

Unassign

It will "unassign" the chat from the agent, and after the first interaction from the user. It will be redirected to the first message, of the bot linked to the WhatsApp Channel

Link to docs: https://dev.landbot.io/api/index.html#api-Customers-PutHttpsApiLandbotIoV1CustomersCustomer_idUnassign

cURL snippet

curl --location --request PUT 'https://api.landbot.io/v1/customers/:customerID/unassign/' \
--header 'Content-Type: application/json' \
--header 'Authorization: Token XXXXXXXXX' \
--data-raw ''

Assign to bot (and to a specific block)

If you want to assign the user to a specific bot and a specific block, instead of the default first block in of the linked bot to the WhatsApp Channel

Link to docs: https://dev.landbot.io/api/index.html#api-Customers-PutHttpsApiLandbotIoV1CustomersCustomer_idAssign_botBot_id

cURL snippet

curl --location --request PUT 'https://api.landbot.io/v1/customers/:customer_id/assign_bot/:bot_id/' \
--header 'Content-Type: application/json' \
--header 'Authorization: Token XXXXXXXXX' \
--data-raw '{
"launch": false,
"node": "node_id"
}'

Find :bot_id

To find the bot id, go to the bot inside the app, and the number in the url, is the bot id

Find :block_id

To retrieve the block id, in the bot builder, select the block where you want the bot to use, click on the three dots of the block and select Copy block id, will display. Once you click it will set the number in your clipboard

Error Logs

On the Logs section of your WhatsApp channel you'll be able too se If there was any issue generated when sending the Message Templates on the 'Errors' sections

How did we do?

WhatsApp's Message Templates

Audience block

Contact