Send a Text Template Message through WhatsApp
POST and GET Method
This API allows you to create and send WhatsApp Message Templates.
Template messages are predefined messages that are used for standard communication and are designed to be reusable.
To send a Text Template Message through WhatsApp, perform the following steps:
- Before you get started, Create an API Key. To view the API Key and the SID, see View API Key and SID.
- Pre-requisites
The following are the pre-requisites for using Kaleyra WhatsApp API’s:
- A WhatsApp business number.
- An associated profile with the business number.
- A WhatsApp verified and approved profile.
For more details, read what WhatsApp has to say about creating a business account.
Read Message Templates for any questions you may have about creating templates.
- Send a Text Template Message API request.
Base URL
https://<api_domain>/v2/<SID>
API request to send a Text Template Message through WhatsApp
To send a Text Template Message through WhatsApp, use the /messages
endpoint with the following request method:
Request Format - POST Method
The following is the request format to send a text template message through WhatsApp for POST method:
curl -X POST "https://api.kaleyra.io/v1/<sid>/messages" \
-H "api-key: <api_key>" \
-d "from=<from_number>" \
-d "to=<to_number>" \
-d "type=<message_type>" \
-d "channel=<channel_name>" \
-d "template_name=<template_name>" \
-d "params=<template_parameters>" \
-d "lang_code=<language_code>" \
-d "callback_url=<callback_url>"
Sample Request Format -POST Method
The following is the sample request format to send a text template message through WhatsApp for POST Method:
curl -X POST "https://api.kaleyra.io/v1/HXXXXXXX071US/messages" \
-H "api-key: Axxxxxxxxxxxxxxxxxxxxxxxxxxxx3" \
-d "from=+91XXXXXXXXXX" \
-d "to=+91XXXXXXXXXX" \
-d "type=template" \
-d "channel=whatsapp" \
-d "template_name=booking_confirm" \
-d "params=param1,param2,param3" \
-d "lang_code=en" \
-d "callback_url=https%3A%2F%2Ftrees.free.beeceptor.com"
Request Format - GET Method
The following is the request format to send a text template message through WhatsApp for GET method:
curl -X GET "https://api.kaleyra.io/v1/<sid>/messages?channel=<channel_name>&to=<to_number>&from=<from_number>&type=<message_type>&template_name=<template_name>¶ms=<template_parameters>&lang_code=<language_code>&callback_url=<callback_url>" \
--header "api-key: <api-key>"
Sample Request Format
The following is the sample request format to send a text template message through WhatsApp for GET Method:
curl -X GET "https://api.kaleyra.io/v1/HXXXXXXX071US/messages?channel=whatsapp&to=91XXXXXXXXXX&from=91XXXXXXXXXX&type=template&template_name=booking_confirm¶ms=test&lang_code=en&callback_url=https%3A%2F%2Ftrees.free.beeceptor.com" \
--header "api-key: Axxxxxxxxxxxxxxxxxxxxxxxxxxxx3"
Parameter and Data Types
The following is the list of parameters and data types supported for sending a Text Template Message:
Parameter | Data Type | Description | Examples | Mandatory? |
---|---|---|---|---|
sid | String | Account SID (Security Identifier). Generated by Kaleyra.io while creating an API key. | HXXXXXXX071US | Yes |
api_key | String | Your API Key generated by Kaleyra.io. | Axxxxxxxxxxxxxxxxxxxxxxxxxxxx3 | Yes |
content_type | String | Indicates the format of the content the API will be processing. | application/json | Yes |
from_number | String | The number registered with WhatsApp business from which the message is to be sent. Ensure that the country code is prefixed to the number. (E164 format). | +91XXXXXXXXXX | Yes |
to_number | String | A valid WhatsApp number of the recipient. Ensure that the country code is prefixed to the number. (E164 format). Note: You can add multiple recipients, separate each number using the comma (,) delimiter. | +91XXXXXXXXXX | Yes |
message_type | String | The type of message to be sent. | template | Yes |
channel_name | String | The channel on which message must be sent. | Yes | |
template_name | String or Integer | Name of the WhatsApp template that was created in the Kaleyra.io portal. This name must be approved by WhatsApp before you start using this API. | booking_confirm | Yes |
template_parameters | String | The values that you will pass when you want to send a message using the template. Since the values are dynamic, it must be passed through the key. Multiple parameters are allowed that must be separated with a comma. For a template that says: Hi your booking id is for date the params to be passed would be , <id_number>, . | "", "", "" | Yes |
language_code | String | The language code of the language that the template will allow. 'en' is the code for English. For a comprehensive list of supported languages, read this | en | Yes |
callback_url | String | The URL that Kaleyra.io should send information to when your number receives a response. This URL can be accessed publicly. Click here for an example. | https%3A%2F%2Ftrees.free.beeceptor.com | No |
Sample Success Response
The following success message appears with the status 200 Accepted:
{
"id": "1b4XXXX8-61bf-4XX3-9aXX-f130XXXX80a5",
"type": "template",
"body": null,
"createdDateTime": "2020-01-29 05:30:39+00:00",
"totalCount": 1,
"data": [
{
"message_id": "1b4XXXX0-61bf-46d3-9a1d-f130XXXXf0a5:0",
"recipient": "1202XXXXXXX"
}
],
"error": {}
}
Error Codes
The below table provides information about the error codes you would receive when executing the WhatsApp Template Message API.
Error Code | Parameter | Description |
---|---|---|
E13008 | from_invalid | Invalid or in-correct input of the From number. |
E13009 | to_invalid | Invalid or in-correct input of the TO number. |
E13004 | template_params | The Template params are not matching. |
E13005 | template_name_missmatch | Invalid template. |
Updated about 1 month ago