WhatsApp is considered to be one of the top messaging apps globally, with over 1.5 billion users and over 60 billion messages sent per day. It has proven to be secure and reliable over the years and now with recent surveys, online businesses are using it as a communication medium to reach their customers other than the traditional way of sending SMS. In this tutorial, I’m going to take you through how to integrate WhatsApp Messaging into your application using the Twilio API for WhatsApp messaging. Here’s a short 30-second video on how you can achieve this after setting up.
This tutorial assumes you have basic knowledge of PHP and Unix/Linux commands. If not, don't be discouraged. I will explain every concept in detail.
Ensure you have the following installed in your local development environment.
Open your terminal and run the following commands:
mkdir demo-app cd demo-app touch twilioWhatsAppMessaging.php .env
The above commands create a folder named
demo-app in which the
.env files are created inside it.
Create a WhatsApp Twilio Project
After you have created and verified your account on Twilio, you will be redirected to the dashboard where you need to create your project. Under the Products tab, select Programmable SMS then click “Continue”. Next, you are required to key in your project name. I have named mine “Demo WhatsApp Messaging App”. To test your app, Twilio loads your account with a trial balance that you can use to purchase a number with Voice, SMS and MMS capabilities.
In the Programmable SMS dashboard, select “WhatsApp Beta” to activate your Sandbox.
You will be redirected to the dashboard above where you will be required to connect to your sandbox by sending a WhatsApp message through your device. In my case, I’m required to send
join space-known to +14155238886.
We are now all set to start sending WhatsApp messages! In your preferred IDE, open our project and add the following lines of code to start sending WhatsApp messages to
<?php require __DIR__ . "/vendor/autoload.php"; use Twilio\Rest\Client; $dotenv = Dotenv\Dotenv::create(__DIR__); $dotenv->load(); $twilioSid = getenv('TWILIO_SID'); $twilioToken = getenv('TWILIO_TOKEN'); $twilio = new Client($twilioSid, $twilioToken); $message = $twilio->messages ->create( "whatsapp:+254712345678", array( "body" => "Greetings from Twilio :-)", "from" => "whatsapp:+14155238886" ) );
Be sure to replace
+254713456789 with your phone number that will receive the message
To install the official Twilio SDK and PHP Dotenv package and enable us to send WhatsApp messages and retrieve the environment variables respectively run:
$ composer require twilio/sdk vlucas/phpdotenv
Test Sending A WhatsApp Message
Head over to the Twilio dashboard to obtain the Twilio SID and Twilio Token and update the values in your
To run our application, type
php twilioWhatsAppMessaging.php in your terminal. Voila! We’ve sent our first WhatsApp message.
Conclusion & Video
In this tutorial, we have learned how to send WhatsApp messages using the Twilio API for WhatsApp messaging. Scale your businesses and connect more with your customers where they are most; online! I have created a video version of this tutorial for you to watch.