We will be using a third-party package like “twilio/sdk.” Twilio provides a WhatsApp API that allows you to send messages and media to WhatsApp users programmatically. Below, we will walk through the steps to integrate the Twilio WhatsApp API into a Laravel application.
Step 1: Set up a Twilio Account
First, you need to sign up for a Twilio account (if you don’t have one already) at https://www.twilio.com/try-twilio. After creating an account, you’ll get your Account SID and Auth Token, which are essential for API authentication.
Step 2: Create a Laravel Project
If you haven’t already set up a Laravel project, you can do so using Composer:
composer create-project --prefer-dist laravel/laravel whatsapp-api-example
Step 3: Install Twilio SDK Package
Install the Twilio SDK package via Composer:
composer require twilio/sdk
Step 4: Configuration
In your Laravel project, open the .env
file and add the following Twilio credentials:
TWILIO_SID=your_twilio_account_sid
TWILIO_AUTH_TOKEN=your_twilio_auth_token
TWILIO_WHATSAPP_NUMBER=your_twilio_whatsapp_number
Step 5: Create a Route
In routes/web.php
, create a route to trigger the WhatsApp message sending:
use App\Http\Controllers\WhatsAppController;
Route::get('/send-whatsapp', [WhatsAppController::class, 'sendWhatsAppMessage']);
Step 6: Create a Controller
Create a new controller WhatsAppController
using the following command:
php artisan make:controller WhatsAppController
Open the generated WhatsAppController.php
and add the code for sending a WhatsApp message:
namespace App\Http\Controllers;
use Twilio\Rest\Client;
class WhatsAppController extends Controller
{
public function sendWhatsAppMessage()
{
$twilioSid = config('app.twilio_sid');
$twilioToken = config('app.twilio_auth_token');
$twilioWhatsAppNumber = config('app.twilio_whatsapp_number');
$recipientNumber = 'RECIPIENT_PHONE_NUMBER'; // Replace with the recipient's phone number in WhatsApp format (e.g., "whatsapp:+1234567890")
$message = "Hello from Twilio WhatsApp API in Laravel! 🚀";
$twilio = new Client($twilioSid, $twilioToken);
try {
$twilio->messages->create(
$recipientNumber,
[
"from" => $twilioWhatsAppNumber,
"body" => $message,
]
);
return response()->json(['message' => 'WhatsApp message sent successfully']);
} catch (\Exception $e) {
return response()->json(['error' => $e->getMessage()], 500);
}
}
}
Step 7: Testing the Integration
Now you can test the integration by accessing the /send-whatsapp
route in your browser. It will trigger the sendWhatsAppMessage
method in the WhatsAppController
, and a WhatsApp message with the specified content will be sent to the recipient number.
Summary
- We create a new
WhatsAppController
that extends the base LaravelController
. - In the
sendWhatsAppMessage
method, we retrieve the Twilio credentials from the.env
file using theconfig
function. - We create a new instance of the Twilio
Client
using the credentials. - Inside a
try-catch
block, we use the Twilio client to send the WhatsApp message to the recipient number specified in the$recipientNumber
variable. - If the message is sent successfully, we return a JSON response indicating success; otherwise, we return an error message in the case of an exception.
Remember to replace 'RECIPIENT_PHONE_NUMBER'
with the actual phone number of the recipient you want to send the WhatsApp message to, formatted in WhatsApp format (e.g., "whatsapp:+1234567890").
That’s it! You have successfully integrated the WhatsApp API into your Laravel application using the Twilio SDK. Now you can leverage the power of WhatsApp to engage with your users programmatically.