How to Send SMS to Mobile Number in Laravel 12 Using Twilio API

In this post How to Send SMS to Mobile Number in Laravel 12 Using Twilio API, you’ll learn how to send text messages or SMS to mobile number using the Twilio API in a Laravel 12 application.

Twilio is a powerful cloud-based communications platform that enables developers to integrate features like SMS, voice calls, email, WhatsApp and video into their applications with ease.

Twilio provides robust APIs that simplify the process of adding communication capabilities to your software.

We’ll walk through a straightforward example where we install the twilio/sdk package via Composer and use Twilio’s API credentials (Account SID, Auth Token) and twilio phone number to send a text message to a specific mobile number.

Just follow the step-by-step instructions below to get everything set up and working in your Laravel project.

Prerequisites

To follow this tutorial, you will need the following:

  • A basic understanding of Laravel and how it works
  • Laravel already installed on your local development environment
  • Composer installed globally on your system
  • An active Twilio account with access to your Account SID, Auth Token, and a Twilio phone number

Step for How to Send SMS to Mobile Number in Laravel 12 Using Twilio API

Step 1: Install Laravel 12

Step 2: Create Twilio Account

Step 3: Install twilio/sdk Package

Step 4: Create Controller

Step 5: Create Route

Step 6: Run Laravel App

🛠️ Step 1: Install Laravel 12

Firstly, we need to create a new Laravel project,if you already installed you can skip. If you don’t have it installed,  run this command in your terminal to create a fresh laravel project:

composer create-project laravel/laravel laravel-12-send-sms-using-twilio

🧑 Step 2: Create Twilio Account

Once Laravel is successfully installed, create an account on Twilio.com. After registering account on twilio, verify your mobile number to access your Account  and get twilio SID, Auth Token, and Twilio phone number. Below, I’ve shared some images to help guide you through the process.

How to Send SMS to Mobile Number in Laravel 12 Using Twilio API
verify your mobile number

Follow These Steps to Set Up Your Twilio Account:

Create an Account inside Twilio

After verifying your phone number, you will see a next screen create an account. Click on “Create Account” and fill in the required details. Once the account is created, you will be redirected to the Twilio Console.

How to Send SMS to Mobile Number in Laravel 12 Using Twilio API
create account

Twilio Console Setup

In the Twilio Console, you will get your Account SID and Auth Token.

how-to-send-sms-to-mobile-number-in-laravel-12-using-twilio-api

Check Active Twilio Number

Click on the sidebar : Phone Numbers > Manage > Active Numbers

How to Send SMS to Mobile Number in Laravel 12 Using Twilio API
Twilio Active Number

If you don’t see any number, click the “Buy a Number” link on the page, or navigate through the sidebar.

Buy a Number

Twilio provides a free trial balance of $15.50. Use it to buy any number available in the list.

This number will be used as your Twilio sender number.

How to Send SMS to Mobile Number in Laravel 12 Using Twilio API
Buy Twilio Number

Get Your Credentials

From your Twilio Console Dashboard, you will get the following details:

  • Account SID
  • Auth Token
  • Twilio Phone Number

Step 3: Install twilio Package

Now change your working directory to laravel-12-send-sms-using-twilio and install the Twilio SDK via Composer:

cd laravel-12-send-sms-using-twilio
composer require twilio/sdk

The next step is to open the .env file located at the root of the project directory and add the Account SID, Auth Token, and Phone Number.

TWILIO_SID="INSERT YOUR TWILIO SID HERE"
TWILIO_AUTH_TOKEN="INSERT YOUR TWILIO TOKEN HERE"
TWILIO_NUMBER_FROM="INSERT YOUR TWILIO NUMBER IN [E.164] FORMAT eg.+14155552671"

🧠 Step 4: Create Controller

Type the command given below to create a controller called SendSMSController:

php artisan make:controller SendSMSController

Once the controller is created, copy and paste the code below into it. This will allow you to handle the SMS sending functionality.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Exception;
use Twilio\Rest\Client;

class SendSMSController extends Controller
{
      public function index()
    {
        $receiverNumber = "RECEIVER_NUMBER";
        $message = "🚀 New Laravel Tutorial: Custom Login with Remember Me 👉 https://itstuffsolutiotions.io";
  
        try {
  
            $account_sid = getenv ("TWILIO_SID");
            $auth_token = getenv("TWILIO_AUTH_TOKEN");
            $twilio_number = getenv("TWILIO_NUMBER_FROM");
  
            $client = new Client($account_sid, $auth_token);
            $client->messages->create($receiverNumber, [
                'from' => $twilio_number, 
                'body' => $message]);
  
            dd('SMS Sent Successfully.');
  
        } catch (Exception $e) {
            dd("Error: ". $e->getMessage());
        }
    }
}

🧑 Step 5: Create Route

In this step, we will create a route to send SMS. Open the web.php file and add the code below.

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SendSMSController;
  
Route::get('sendSMS', [SendSMSController::class, 'index']);

🚀 Step 6: Run Laravel App

Open the terminal / command line and type the command given below to start the development server.

php artisan serve

Next, open your browser and enter the URL provided below to see the result.

http://127.0.0.1:8000/sendSMS
How to Send SMS to Mobile Number in Laravel 12 Using Twilio API
Send SMS Successfully

SMS Preview on Mobile

How to Send SMS to Mobile Number in Laravel 12 Using Twilio API
sms preview on mobile

Download Project Full Source Code

You can download the full source code for this project directly from our GitHub repository: https://github.com/itstuffsolutions/laravel-laravel-12-send-sms-using-twilio