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 Setting up a new Laravel project Creating a controller Configuring environment variables Making API calls with CURL Handling errors SEO-friendly source codeUse Cases OTP verification Order confirmations Alerts & notifications Customer engagementStep 1: Create a New Laravel Project Open your terminal and run:
Plain Bash C++ C# CSS Diff HTML/XML Java JavaScript Markdown PHP Python Ruby SQL composer create-project laravel/laravel twilio-sms-app
cd twilio-sms-app
Step 2: Create the Controller Use Laravel Artisan to generate a controller:
Plain Bash C++ C# CSS Diff HTML/XML Java JavaScript Markdown PHP Python Ruby SQL php artisan make:controller TwilioController
This creates:
app/Http/Controllers/TwilioController.php
Step 3: Add Twilio Credentials to .env
Plain Bash C++ C# CSS Diff HTML/XML Java JavaScript Markdown PHP Python Ruby SQL 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
:
Plain Bash C++ C# CSS Diff HTML/XML Java JavaScript Markdown PHP Python Ruby SQL <?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
:
Plain Bash C++ C# CSS Diff HTML/XML Java JavaScript Markdown PHP Python Ruby SQL 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 Plain Bash C++ C# CSS Diff HTML/XML Java JavaScript Markdown PHP Python Ruby SQL {
"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:
Plain Bash C++ C# CSS Diff HTML/XML Java JavaScript Markdown PHP Python Ruby SQL php artisan serve
when you will hit the below URL , you can see the below success json as like the image
Plain Bash C++ C# CSS Diff HTML/XML Java JavaScript Markdown PHP Python Ruby SQL http://127.0.0.1:8000/send-test-sms
Common Issues and Fixes ErrorReasonSolution Curl error: 401 Wrong SID/Auth Token Double-check .env
values From number is invalid Unverified or inactive Twilio number Use a verified number from your dashboard No message sent Recipient number not verified (trial account) Verify in Twilio dashboard
Bonus Tips Dynamic Message Example Plain Bash C++ C# CSS Diff HTML/XML Java JavaScript Markdown PHP Python Ruby SQL $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:
Lightweight performance Full control Fewer dependencies Better debuggingThis 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.
VIDEO