Integrate Sendwo with your website or app using the new Sendwo API. This allows you to send WhatsApp messages, including OTPs, directly from your platform. This article provides a step-by-step guide on sending WhatsApp OTP messages via the API.
To get your API key, go to the Sendwo dashboard, click on your user account, and select "API Developer." Then, click the "Generate API Key" button.
User Account > API Developer > Generate API Key
WhatsApp offers two APIs: the Send API and the Subscriber/Contact API. This example demonstrates sending a WhatsApp message using the Send API, specifically using a PHP script to deliver an OTP.
For business-initiated messages or messages sent outside the 24-hour customer service window, a pre-approved message template is required. While direct messaging is possible with users active in a 24-hour chat window, template messages can be sent to any user, even those added manually as subscribers.
To create a message template, go to your WhatsApp account's Message Template section within the WhatsApp Bot Manager.
WhatsApp > Bot Manager > Select Bot Account > Message Template
We'll start by creating a variable for our message template. This is necessary because each user will receive a unique OTP. To create the variable, click the "Create" button in the Template variables section.
Give a name for the variable and click on the save button. Now, from the message template settings click on the “Create” button to create a template.
Once the previous step is finished, a template creation form will appear. Provide a name and locale for the template. Select the appropriate category, and since this is an OTP template, choose "OTP" as the type. Adding a header is optional; if desired, select the "Header" type.
Compose the message in the body, including the generated OTP variable by selecting it from the provided list. A footer is also optional. Since this template doesn't require buttons, leave the footer setting as "None." Finally, click the "Save" button.
The template should be in the list after it has been created.
Click the check status button to discover if the template has been approved or not. They must immediately approve or disapprove.
Yes, the template was created and approved successfully.
To generate an API endpoint for sending a template message, navigate to "Generate API End-point : Send Template Message (GET)". Select the desired WhatsApp account (using your WhatsApp phone number) and choose the "OTP message template" from the available templates. This will use the template you recently created.
As soon we select the Message template it will ask us to put a name for it. Just put OTP on the field and click on the “Get API end-point”.
It will generate the API End-point. We will use this code in the PHP script to send OTP messages to WhatsApp users.
In the PHP script, we will put this generated code into a variable. Then we need to write a few lines of code.
$url="https://sendwo.com/api/v1/whatsapp/send/template?apiToken=1|bF90hdFU4Mm2uFxIFDbvvEIcxYqweyJuwrO25yEM&phoneNumberID=100761702762631&botTemplateID=712&templateVariable-OTP-1=OTP-VALUE&sendToPhoneNumber=SEND-TO-PHONE-NUMBER";
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
echo $response=curl_exec($ch);
?>
We need to change the OPT-VALUE and SEND-TO-PHONE-NUMBER from the API end-point to variables. Just like below.
The final code will look like this after updating the values with variables.
$otp= random_int(10000, 99999);
$send_number="8801722977459";
$url="https://sendwo.com/api/v1/whatsapp/send/template?apiToken=1|bF90hdFU4Mm2uFxIFDbvvEIcxYqweyJuwrO25yEM&phoneNumberID=100761702762631&botTemplateID=712&templateVariable-OTP-1={$otp}&sendToPhoneNumber={$send_number}";
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
echo $response=curl_exec($ch);
?>
[ Use must use your generated API End-point ]
If we just run this code, the message should be delivered to the WhatsApp number.
So, this is how we can utilize the WhatsApp API of Sendwo to deliver OTP messages to WhatsApp numbers.