• Send-SMS-using-Laravel-Plivo-copy

SMS messaging is one of the  most popular way of notifying user about otp, promotional offers and many other updates. In one of my project, there was requirement of sending otp to user’s phone device  and I had to develop web service for web and mobile apps to consume it for sending sms and that is when I stumbled upon Twilio and how by using it with Laravel will give you the result with maximum efficiency and minimum amount of hard work.

In just a matter of few minutes and few lines of code you will be ready to send your first text message. To send a message, just make an HTTP POST to Twilio with the body of the message and the phone number you want to send it to.

Here we will cover the installation and integration of twilio in our project and then use it’s API to send SMS or OTP to a user. Follow the below given steps and then you are good to go. But,before going into the coding part, first let us learn about what twilio is and how does it work?


It is a cloud communication platform as a service. It allows software developer to programmatically make and receive phone calls and send and receive text messages using its web service APIs.This enables businesses to provide the right communications experience for their customers.

Behind Twilio API is a Super Network, a software layer that connects and optimizes communications networks around the world. This is what allows your users to reliably call and message anyone anywhere.Twilio uses Amazon Web Services to host telephony infrastructure and provide connectivity between HTTP and the public switched telephone network (PSTN) through its APIs.

With Twilio, you can reach customers in the ways they prefer, and engage with them effectively using context related to that interaction. As customer experience can increasingly make or break your brand, programmable communications has become more crucial than ever to the success of businesses today.

Sending an SMS or MMS is one of the most common tasks performed on the Twilio Platform. It requires AccountSID and AuthToken, they can be generated by registrting at Twilio.

Getting a Twilio SMS Account:
Here is a link to create an account on twilio if you don’t have one already.

After registration click on Account, there you will be able to see authsid and authtoken. You have assigned a sender mobile number which can be found at Twilio, which is used to send Text Messages or MMS and ShortCodes etc.


Sample Snapshot:



When you send an SMS/MMS from your web app using Twilio it works like this:


As you can see sending an SMS is pretty straight forward, and all of this interaction is triggered with just those few lines of code which we will see later.


  • Download package form
  • OR to install with composer run the following command in your terminal
       composer require lakshmaji/twilio
       Composer dump-autoload
       Composer update


First, you need to add the service provider. Open app/config/app.php, and add a new item to the providers array.


Then, add a Facade for more convenient usage. In app/config/app.php add the following line to the aliases array:

'Twilio' => Lakshmaji\Twilio\Facade\Twilio::class,

Again do composer update again

After completing the above two steps, now open you terminal and create a controller called TwilioTestController. In this controller class we write an example code to send an otp to a user mobile.

Run the below command to create the controller class:

php artisan make:controller  TwilioTestController

Now, go to your TwilioTestController class and write down the below code.

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Twilio; 

class TwilioTest extends Controller
  public function testMesssage()

    // initialize message array 
    $message_array = array(
        'sender_id'     => 'TWILIO_USER_ID',
        'sender_secret' => 'TWILIO_USER_PASSWORD',
        'receiver_mobile' => '99999999999',
        'otp'     =>'325456',
        'sender' => 'TWILIO_SOURCE_MOBILE_NUMBER'

    // This will send OTP only
    $sms_response = Twilio::message($message_array,$op="otp only", false, true,  false ); // otp

    return response()->json($sms_response,200);

After writing the above code in your controller, go to route folder of your project and give the following route in your api.php file.

Route:: post(‘send_sms’,’TwilioTestController@testMessage’);

when you hit the above api, you will get the following Response:

Response Data:

   "account_sid": "AC6779d4499d13507f580f37e112a486a2",
   "api_version": "2010-04-01",
   "body": "325456",
   "num_segments": "1",
   "num_media": "1",
   "date_created": "Wed, 18 Aug 2010 20:01:40 +0000",
   "date_sent": null,
   "date_updated": "Wed, 18 Aug 2010 20:01:40 +0000",
   "direction": "outbound-api",
   "error_code": null,
   "error_message": null,
   "from": "+14158141829",
   "price": null,
   "sid": "MM90c6fc909d8504d45ecdb3a3d5b3556e",
   "status": "queued",
   "to": "+15558675310",
   "uri": "/2010-0401/Accounts/AC6779d4499d13507f580f37e112a486a2/Messages/MM90c6fc909d8504d45ecdb3a3d5b3556e.json"

NOTE:Trial accounts cannot send messages to unverified numbers; verify +9199XXX23867 at, or purchase a Twilio number to send messages to unverified numbers.

By developing and consuming api we desicussed above, now you are able to add SMS capabilities to any of your web/mobile apps in minutes.Well I hope this taste of the Twilio API has left you excited to learn even more. To know more about twilio and it’s features visit It is fully stocked with examples to help you dive in.