Authenticate users with a WhatsApp template message
This tutorial will show you how to send a simple authorization message using Infobip WhatsApp API templates.
What is a WhatsApp template message?
As opposed to regular messages that need to be user-initiated and can only happen within a 24-hour window, template messages can be sent and delivered at any time. Check out our Product Documentation for more details.
Prerequisites
- Infobip account (opens in a new tab). If you do not have one, create a free trial account (opens in a new tab).
- Infobip API key with the
whatsapp:message:send
scope. Find out more about scopes in API Authorization. - Your favorite HTTP client. In this example, we use
curl
. You can also choose an official Infobip SDK in your preferred programming language. - Sender. If in a free trial period, you can use Infobip test sender, 447860099299. If you want to configure a custom sender, request a new number or an alphanumeric sender through your Infobip account (opens in a new tab) or use the Infobip Numbers API (opens in a new tab).
- Destination - a phone number to which the message will be sent. In a free trial period, you can only send messages to a verified phone number.
Summary of the steps
- Create and register WhatsApp template.
- Send a WhatsApp template message.
Create a WhatsApp template
Use the Create a WhatsApp template (opens in a new tab) endpoint to create your template.
curl -L -g 'https://{baseUrl}/whatsapp/2/senders/{sender}/templates' \
-H 'Authorization: {authorization}' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-d '{
"name": "authentication_template_with_copy_code_button",
"language": "en",
"category": "AUTHENTICATION",
"allowCategoryChange": false,
"structure": {
"body": {
"addSecurityRecommendation": true
},
"footer": {
"codeExpirationMinutes": 5
},
"buttons": [
{
"text": "Copy code",
"otpType": "COPY_CODE"
}
]
}
}'
Key points:
- Once you create a template, Infobip sends it for the WhatsApp validation process. You can edit the template, following the editing rules (opens in a new tab).
- For
category
, use theAUTHENTICATION
value. This will adapt further field options in the API docs. - For
buttons
, useCOPY_CODE
. This will adapt further field options in the API docs.
Register a WhatsApp template
Infobip takes care of your template registration. During the process, you can edit your template following the editing rules (opens in a new tab). You can also set up a webhook (opens in a new tab) to get the updates about your template registration process.
Send a WhatsApp template message
Once registered, use the template to contact users for authentication.
Key points:
- Use the international number formatting when adding a number to the
to
field. For example, for British numbers, the value will look like this:44754322576
. - For
buttons
, useURL
. This is to adhere to Meta's convention for button types.
curl -L -g 'https://{baseUrl}/whatsapp/1/message/template' \
-H 'Authorization: {authorization}' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-d '{
"messages": [
{
"from": "441134960000",
"to": "441134960001",
"messageId": "a28dd97c-1ffb-4fcf-99f1-0b557ed381da",
"content": {
"templateName": "template_name",
"templateData": {
"body": {
"placeholders": [
"Authentication Code Value"
]
},
"buttons": [
{
"type": "URL",
"parameter": "Authentication Code Value"
}
]
},
"language": "en_GB"
},
"callbackData": "Callback data",
"notifyUrl": "https://www.example.com/whatsapp",
"urlOptions": {
"shortenUrl": true,
"trackClicks": true,
"trackingUrl": "https://example.com/click-report",
"removeProtocol": true,
"customDomain": "example.com"
}
}
]
}'
Track message delivery
Once you send the request, you should receive a WhatsApp message on the handset you've specified in the to
field and see a 200 OK
response.
{
"to": "441134960001",
"messageCount": 1,
"messageId": "a28dd97c-1ffb-4fcf-99f1-0b557ed381da",
"status": {
"groupId": 1,
"groupName": "PENDING",
"id": 7,
"name": "PENDING_ENROUTE",
"description": "Message sent to next instance"
}
}
The response informs you that the message has successfully been queued within the Infobip platform and will be then sent to Meta depending on throughput.
If you want to know whether it's been delivered to the recipient, use the Delivery Report (opens in a new tab) feature.
Troubleshoot and analyze your message
For troubleshooting and analytics, Use the auto-generated messageId
to view the message and its details.