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:
| Key | Description |
|---|---|
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 |
TO_NUMBER | Replace with the number you are sending to. E.g. |
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.
Run your code
Save this file to your machine and run it:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
Add the following to build.gradle:
Create a file named SendWhatsappAuthenticationTemplate and add the following code to the main method:
Write the code
Add the following to the main method of the SendWhatsappAuthenticationTemplate file:
Run your code
We can use the application plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:
Run the following gradle command to execute your application, replacing com.vonage.quickstart.kt.messages.whatsapp with the package containing SendWhatsappAuthenticationTemplate:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
Add the following to build.gradle:
Create a file named SendWhatsappAuthenticationTemplate and add the following code to the main method:
Write the code
Add the following to the main method of the SendWhatsappAuthenticationTemplate file:
Run your code
We can use the application plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:
Run the following gradle command to execute your application, replacing com.vonage.quickstart.messages.whatsapp with the package containing SendWhatsappAuthenticationTemplate:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
Create a file named send-authentication-template.php and add the following code:
Run your code
Save this file to your machine and run it:
Prerequisites
If you do not have an application you can create one. Make sure you also configure your webhooks.
Run your code
Save this file to your machine and run it:
Try it out
When you run the code a WhatsApp Authentication Template is sent to the destination number.