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:

  1. Before you get started, Create an API Key. To view the API Key and the SID, see View API Key and SID.
  2. 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.

  1. 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>&params=<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&params=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:

ParameterData TypeDescriptionExamplesMandatory?
sidStringAccount SID (Security Identifier). Generated by Kaleyra.io while creating an API key.HXXXXXXX071USYes
api_keyStringYour API Key generated by Kaleyra.io.Axxxxxxxxxxxxxxxxxxxxxxxxxxxx3Yes
content_typeStringIndicates the format of the content the API will be processing.application/jsonYes
from_numberStringThe 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).+91XXXXXXXXXXYes
to_numberStringA 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.
+91XXXXXXXXXXYes
message_typeStringThe type of message to be sent.templateYes
channel_nameStringThe channel on which message must be sent.whatsappYes
template_nameString or IntegerName 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_confirmYes
template_parametersStringThe 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_codeStringThe 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
enYes
callback_urlStringThe 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.comNo

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 CodeParameterDescription
E13008from_invalidInvalid or in-correct input of the From number.
E13009to_invalidInvalid or in-correct input of the TO number.
E13004template_paramsThe Template params are not matching.
E13005template_name_missmatchInvalid template.