Twilio Integration In Laravel 12 Step By Step Guide With Example

image
image
image
image
image
image
image
image
twilio integration in laravel 12 | Step by step guide with example

twilio integration in laravel 12 | Step by step guide with example

Sending SMS through your Laravel app using Twilio is easier than you think. In this blog, we'll show you how to send SMS using Twilio's REST API with just CURL—no third-party SDK or package required.

Perfect for developers who want full control, fewer dependencies, and clean code.


Before Starting The Integration, You have to create an account first from here - https://login.twilio.com/u/signup


When you log into your account, you dashboard will looks like the below one, from that page you need to collect your Twilio phone number, sid, and key



What You’ll Learn

  1. Setting up a new Laravel project
  2. Creating a controller
  3. Configuring environment variables
  4. Making API calls with CURL
  5. Handling errors
  6. SEO-friendly source code

Use Cases

  1. OTP verification
  2. Order confirmations
  3. Alerts & notifications
  4. Customer engagement

Step 1: Create a New Laravel Project

Open your terminal and run:

composer create-project laravel/laravel twilio-sms-app
cd twilio-sms-app


Step 2: Create the Controller

Use Laravel Artisan to generate a controller:

php artisan make:controller TwilioController

This creates:

app/Http/Controllers/TwilioController.php


Step 3: Add Twilio Credentials to .env

ACCOUNT_SID=your_twilio_account_sid
AUTH_TOKEN=your_twilio_auth_token

You can find these in your Twilio Console: https://www.twilio.com/console


Step 4: Send SMS Using CURL in Laravel

Paste the following code in TwilioController.php:

<?php

namespace App\Http\Controllers;

class TwilioController extends Controller
{
public function sendSms()
{
$sid = env('ACCOUNT_SID');
$token = env('AUTH_TOKEN');
$from = '+13412173669'; // Your Twilio number
$to = '+18777804236'; // Receiver's number
$body = 'hello CodeHunger';

$url = "https://api.twilio.com/2010-04-01/Accounts/{$sid}/Messages.json";

$data = http_build_query([
'To' => $to,
'From' => $from,
'Body' => $body,
]);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_USERPWD, "{$sid}:{$token}");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

$response = curl_exec($ch);

if (curl_errno($ch)) {
$error_msg = curl_error($ch);
curl_close($ch);
return "Curl error: " . $error_msg;
}

curl_close($ch);

echo $response;
return;
}
}


Step 5: Add a Route

Edit routes/web.php:

php
CopyEdit
use App\Http\Controllers\TwilioController;

Route::get('/send-sms', [TwilioController::class, 'sendSms']);

Now visit http://127.0.0.1:8000/send-sms in your browser.


Sample API Response

{
"account_sid": "ACce120430949973fb3c95fb537ded897f",
"api_version": "2010-04-01",
"body": "Sent from your Twilio trial account - hello",
"date_created": "Wed, 04 Jun 2025 10:54:53 +0000",
"date_sent": null,
"date_updated": "Wed, 04 Jun 2025 10:54:53 +0000",
"direction": "outbound-api",
"error_code": null,
"error_message": null,
"from": "+13412173669",
"messaging_service_sid": null,
"num_media": "0",
"num_segments": "1",
"price": null,
"price_unit": "USD",
"sid": "SMd2e461ac08b112c7815eb5e59762bfbc",
"status": "queued",
"subresource_uris": {
"media": "/2010-04-01/Accounts/ACce120430949973fb3c95fb537ded897f/Messages/SMd2e461ac08b112c7815eb5e59762bfbc/Media.json"
},
"to": "+18777804236",
"uri": "/2010-04-01/Accounts/ACce120430949973fb3c95fb537ded897f/Messages/SMd2e461ac08b112c7815eb5e59762bfbc.json"
}


Test It Locally

Make sure your .env is loaded correctly. You can test with:

php artisan serve


when you will hit the below URL , you can see the below success json as like the image

http://127.0.0.1:8000/send-test-sms





Common Issues and Fixes

ErrorReasonSolution
Curl error: 401Wrong SID/Auth TokenDouble-check .env values
From number is invalidUnverified or inactive Twilio numberUse a verified number from your dashboard
No message sentRecipient number not verified (trial account)Verify in Twilio dashboard


Bonus Tips

Dynamic Message Example

$body = "Hello $user->name, your OTP is $otp";

Perfect for sending OTPs or custom messages.


GitHub Repository

Find full source code here:

Twilio SMS Integration in Laravel


Final Thoughts

Integrating Twilio directly with CURL in Laravel gives you:

  1. Lightweight performance
  2. Full control
  3. Fewer dependencies
  4. Better debugging

This approach is great for those who want to avoid SDK bloat or build something custom.

Let us know your thoughts—and don’t forget to star the GitHub Repo if you found this helpful.