I'm always excited to take on new projects and collaborate with innovative minds.

Address

PP Trade Centre, 110 & 111 1st Floor, Netaji Subhash Place

Social Links

Web Development

📞 How to Set Up Twilio in Laravel: Step-by-Step Guide

Need to send SMS or make voice calls in your Laravel app? Twilio makes it super easy! In this guide, we’ll walk through how to integrate Twilio into a Laravel project step-by-step.

📞 How to Set Up Twilio in Laravel: Step-by-Step Guide

🛠️ Step 1: Install a Fresh Laravel Project

If you haven’t already created a Laravel project, start with this:

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

Already have a Laravel project? Skip to the next step.

 

📦 Step 2: Install Twilio SDK via Composer

Twilio provides an official PHP SDK. Install it like this:

composer require twilio/sdk

 

🔑 Step 3: Create a Twilio Account and Get API Credentials

  1. Go to https://www.twilio.com/

  2. Sign up / log in.

  3. Go to Console Dashboard and note the following:

    • Account SID

    • Auth Token

    • Twilio Phone Number

 

🔐 Step 4: Add Twilio Credentials to .env

Open your .env file and add:

 

TWILIO_SID=your_account_sid
TWILIO_AUTH_TOKEN=your_auth_token
TWILIO_NUMBER=+1234567890

Replace the placeholders with your actual Twilio credentials.

 

🧠 Step 5: Create a Twilio Service Class (Optional but Recommended)

Create a dedicated service class for Twilio.

 

php artisan make:service TwilioService

If make:service doesn’t exist, just create a new file manually:

app/Services/TwilioService.php

<?php

namespace App\Services;

use Twilio\Rest\Client;

class TwilioService
{
    protected $twilio;

    public function __construct()
    {
        $this->twilio = new Client(
            config('services.twilio.sid'),
            config('services.twilio.token')
        );
    }

    public function sendSMS($to, $message)
    {
        return $this->twilio->messages->create($to, [
            'from' => config('services.twilio.from'),
            'body' => $message
        ]);
    }
}

 

🧩 Step 6: Configure config/services.php

Add your Twilio settings:

// config/services.php

'twilio' => [
    'sid' => env('TWILIO_SID'),
    'token' => env('TWILIO_AUTH_TOKEN'),
    'from' => env('TWILIO_NUMBER'),
],

 

📬 Step 7: Use the Twilio Service in Your Controller

Example usage:

app/Http/Controllers/SMSController.php

<?php

namespace App\Http\Controllers;

use App\Services\TwilioService;

class SMSController extends Controller
{
    protected $twilio;

    public function __construct(TwilioService $twilio)
    {
        $this->twilio = $twilio;
    }

    public function send()
    {
        $this->twilio->sendSMS('+11234567890', 'Hello from Laravel + Twilio!');
        return response()->json(['message' => 'SMS sent!']);
    }
}

Add a route:

// routes/web.php

Route::get('/send-sms', [\App\Http\Controllers\SMSController::class, 'send']);

✅ Step 8: Test the Integration

Now visit:

http://your-localhost/send-sms

You should receive an SMS shortly 🎉

 

2 min read
Apr 16, 2025
By Aditya Kumar
Share

Related posts

Apr 28, 2025 • 4 min read
How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu

A LAMP stack is a set of open-source software that works together to h...

Apr 16, 2025 • 4 min read
💳 How to Set Up Stripe Payment Gateway in Laravel: Step-by-Step Guide

Integrating Stripe with Laravel allows you to securely accept payments...

Apr 16, 2025 • 3 min read
What's New in Laravel 12? Full Feature Breakdown & Upgrade Guide

Laravel 12 is finally here, and it brings a wave of exciting new featu...