How to Send Voice OTP on a Phone Call in PHP Using Laravel and Plivo

How to Send Voice OTP on a Phone Call in PHP Using Laravel and Plivo

You can authenticate a phone number by delivering a one-time password (OTP) via a phone call. To do this, you call the number and read a sequence of digits to the recipient via text-to-speech. To verify the number, the user needs to confirm the digits by entering them using the phone’s keypad.

Developers commonly use voice OTP to verify new user registrations, online transactions, and login sessions in an app or website. In this blog post, we walk you through a sample implementation of sending a Voice OTP using the Plivo Voice platform and PHLO, our visual workflow builder. Plivo’s direct carrier connectivity and intelligent routing engine guarantee the best call connectivity and quality.


Before you get started, you’ll need:

  • A Plivo account — sign up for one for free if you don’t have one already.
  • A voice-enabled Plivo phone number if you want to receive incoming calls. To search for and buy a number, go to Phone Numbers > Buy Numbers on the Plivo console.
Buy a New Plivo Number
  • Laravel and Plivo PHP packages — run composer require laravel/installer and composer require plivo/plivo-php to install them.
  • ngrok — a utility that exposes your local development server to the internet over secure tunnels.

Create a PHLO to send OTP via phone call

PHLO lets you construct your entire use case and build and deploy workflows visually. With PHLO, you pay only for calls you make and receive, and building with PHLO is free.

To get started, visit PHLO in the Plivo console and click Create New PHLO. On the Choose your use case window, click Build my own. The PHLO canvas will appear with the Start node. Click on the Start Node, and under API request fill in the Keys as from, to, and otp, then click Validate. From the list of components on the left-hand side, drag and drop the Initial Call component onto the canvas and connect the Start node with the Initiate Call node using the API Request trigger state.

Configure the Initiate Call node with the using the From field and in the To field. Once you’ve configured a node, click Validate to save the configuration. Similarly, create a node for the Play Audio component and connect it to the Initiate Call node using the Answered trigger state. Next, configure the Play Audio node to play a message to the user — in this case, “Your verification code is <otp>.” Under Speak Text, click on Amazon Polly and paste the following:

<Speak voice="Polly.Amy">
   <prosody rate="medium">
      Your verification code is
      <say-as interpret-as="spell-out">{{Start.http.params.otp}}</say-as>

Click Validate to save.

Connect the Initiate Call node with the Play Audio node using the Answered trigger state. After you complete the configuration, provide a friendly name for your PHLO and click Save.

Use the PHLO in a Laravel application

Now you can use the PHLO in a PHP Laravel application by following the below steps:

  • Create a project directory and change into it.
  $ mkdir mylaravelapp
  $ cd mylaravelapp
  $ composer require plivo/plivo-php
  • Install other modules.
  $ composer require predis/predis

Run the PHLO to send OTP via phone call

Now you can trigger the PHLO and test it out. Copy the PHLO ID from the end of the URL of the workflow you just created. You’re also going to need your Auth ID and Auth Token.

Create a Laravel Controller

Change the directory to our newly created project directory and create a Laravel controller for outbound calls.

$ php artisan make:controller VoiceController

This command generates a controller named VoiceController in the app/http/controllers/ directory. Edit app/http/controllers/voiceController.php and paste into it this code.

namespace App\Http\Controllers;
require '../../vendor/autoload.php';
use Plivo\RestClient;
use Illuminate\Support\Facades\Redis;

class VoiceController extends Controller
    public function dispatch_otp()
        $dst_number = $_REQUEST["dst_number"];
        $code = random_int(100000, 999999);

        $auth_id    = "<auth_id>";
        $auth_token = "<auth_token>";
        $client = new PhloRestClient($auth_id, $auth_token);
        $phlo = $client->phlo->get(<YOUR_PHLO_ID>);
         try {
         $response = $phlo->run(["from" => "+15673234567", "to" => $dst_number,"otp"=>$code]);
         // These are the fields entered in the PHLO console
      } catch (PlivoRestException $ex) {
        Redis::setex($dst_number, 60, $code);
        header('Content-Type: application/json');
        echo json_encode($response);
        echo '{"status": "success", "message": "verification initiated"}';
    public function verify_otp()
        $dst_number = $_REQUEST["dst_number"];
        $code = $_REQUEST["otp"];
        $value = Redis::get($dst_number);

        if ($code==$value)
            echo '{"status": "success", "message": "codes match! number verified"}';
            echo '{"status": "failure", "message": "codes do not match! number not verified"}';
            echo '{"status": "failure", "message": "number not found!"}';

Substitute actual values for <auth_id>, <auth_token>, and <PHLO_ID>. Save the file and run it.

Add a route

Now, to add a route for the outbound function in the VoiceController class, edit routes/web.php and add these lines at the end of the file.

Route::match(['get', 'post'], '/dispatch_otp', 'App\Http\Controllers\VoiceController@dispatch_otp');
Route::match(['get', 'post'], '/verify_otp', 'App\Http\Controllers\VoiceController@verify_otp');

Edit your .env file and add this line.



Now the VoiceController is ready for your first outbound call. Start the Laravel controller.

$ php artisan serve

Start Redis.

$ redis-server

And you should see your basic server application in action.


Boom — you’ve made an outbound call with the OTP as a text-to-speech message.

Simple and reliable

And that’s all there is to send OTP via a phone call using Plivo’s PHP SDK. Our simple APIs work in tandem with our comprehensive global network. You can also use Plivo’s premium direct routes that guarantee the highest possible delivery rates and the shortest possible delivery times for your 2FA SMS and voice messages. See for yourself — sign up for a free trial account.

Get Volume Pricing

Thousands of businesses in more than 220 countries trust Plivo’s cloud communications platform

The best communications platform forthe world’s leading entertainment service

Frequently asked questions

No items found.
footer bg

Subscribe to Our Newsletter

Get monthly product and feature updates, the latest industry news, and more!

Thank you icon
Thank you!
Thank you for subscribing
Oops! Something went wrong while submitting the form.