Sms Verification Using Twilio Api on Laravel 5.6

In this short tutorial our objective is SMS VERIFCATION USING TWILIO API USING LARAVEL 5.6 where I will create RestApi for that, this blog is divided into:
1- twilio account setup
2- laravel project setup
3- database migration
4- Sms Routes, Model & Controller
5- Testing

1- twilio account setup

I am using twillio trial account. Once you create an account on www.twilio.com. Then Go to https://www.twilio.com/console/phone-numbers/incoming and get a free number after that in trial twilio account you have to verify receiver number, for that go to https://www.twilio.com/console/phone-numbers/verified and add a your number and you will receive code on your number, just verify it.
Now from twilio you need three things
1- Account SID
2- Auth Token
3- Sender Number.

To get all these detail from single page, just go to https://www.twilio.com/console/sms/getting-started/build

You can see some thing like this.

Here in my case

AC30e9b8a072f1c8f7e7b38082d131797a is my ACCOUNT SID
Preview AuthToken to get it. Also you can get sender number. In above screen, it is +17049819803.
Now we all get what we need from twilio.

2- laravel project setup

I am going to use my prebuild laravel project and I assume you know how to create new laravel project. Once you created a new laravel project. Go to your file directory

add the twilio sdk (using composer).

Install guzzle for Twilio Api Call

Add these configuration settings of Twilio in your .env file.

Now add below code in config/app.php to access the twilio configuration variables globally.

Here we are done with laravel installation setup. Now we are moving to database migration.

3- Database Migration

Simply create a new migration (your db must connected with laravel app)

In migration table

migrate it.

4- Sms Routes, Model & Controller

First I am going to make a model.

And my here I will create two method in this model, store and updateModel.

Now I will define two api routes, one for sending sms and second for verification.

Now add below routes in routes/api.php

Now create a controller with name SmsController and create three methods, store, sendSms and verifyNymber. This is the most technical part of this tutorial.

Create a controller

Now add libraries at top of controller

For good coding practice, I define two variables

and in constructor

store method is look like this.

sendSms Method is look like this

VerifyContact Method

Complete Controller Code

5- Testing

I am using postman for testing.

Testing send-sms route, and here is post request and response

Received sms message on my number

Lastly just verify it, for that we need to use verify-user route, here I receive CODE: 8037. let test it and use wrong code and see what response we get from api.

Now test on original code

BINGOO it is working :D

I hope you learn something from this tutorial, feel free to comment below if you have any query.

To make the project successfully within the set timeframe and budget, if you can’t do it all alone, you don’t need to worry. Psmorfia would love to help you in the process and make your project complete and successful as per your requirements.

Reach us to get more information and help you make your project a success.
or email us at
info@pmsorfia.com

For a free consultation, book a call with us

For a free consultation, book a call with us

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

SYNERZIP TEST AUTOMATION FRAMEWORK

Blue Team: System Live Analysis [Part 2]- Windows: Rules and Tools

Life and Death of an Object — Chapter 9

Server Side Swift & Concurrency: two sides of the same coin

Natural Language API & Google Docs

6 Critical JIRA Metrics

Designing a serverless pipeline for moving data to S3

Compliance teams need a digital assistant.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Zilay

Zilay

More from Medium

Laravel 8 Autocomplete Search from Database

Running Laravel Queue:work in cPanel

How to Install JWT Auth in Laravel Project

implementing custom auth guard in laravel