Send a WhatsApp Authentication Template

In this code snippet you learn how to send a WhatsApp Authentication Template using the Messages API.

WhatsApp recently updated the rules for using Authentication templates, which must now include a COPY_CODE or ONE_TAP button. Since it contains a button, an Authentication Template is considered an interactive template. When sending interactive templates via the Vonage Messages API, they must be sent with a message_type of custom rather than template.

To template is sent using the Messages custom object. The custom object takes a partial section of the original WhatsApp API request and sends it directly through to WhatsApp.

WhatsApp Media Message Templates consist of a Body, Footer, and Buttons structure.

Note: In case you are trying to send an Auth template with One-Tap button and if you do not perform a handshake before sending the message, or the message fails an eligibility check, the delivered message will display a copy code button instead of a one-tap button.

Example

Find the description for all variables used in each code snippet below:

KeyDescription
VONAGE_APPLICATION_ID

The Vonage Application ID.

VONAGE_APPLICATION_PRIVATE_KEY_PATH

Private key path.

WHATSAPP_NUMBER

The WhatsApp number that has been allocated to you by Vonage. For sandbox testing the number is 14157386102.

VONAGE_NUMBER

Refer to WHATSAPP_NUMBER above.

TO_NUMBER

Replace with the number you are sending to. E.g. 447700900001

WHATSAPP_AUTH_TEMPLATE_NAME

The name of the Authentication template created in your WhatsApp Business Account.

OTP

A One-Time Password

NOTE: Don't use a leading + or 00 when entering a phone number, start with the country code, for example, 447700900000.

Prerequisites

If you do not have an application you can create one. Make sure you also configure your webhooks.

Write the code

Add the following to send-authentication-template.sh:

View full source

Run your code

Save this file to your machine and run it:

bash send-authentication-template.sh

Try it out

When you run the code a WhatsApp Authentication Template is sent to the destination number.

Further information