Skip to main content

Documentation Index

Fetch the complete documentation index at: https://plivo.com/docs/llms.txt

Use this file to discover all available pages before exploring further.

Overview

This guide shows how to use a voice one-time password (OTP) to verify a mobile number. We first make a call to the phone number to be verified and use text-to-speech to read a random sequence of digits to the call recipients. The user then confirms the digits by entering them using dialpad keypresses. Voice OTP is commonly used to verify new user registrations for an app or website. You can send a voice OTP either by using our PHLO visual workflow builder or our APIs and XML documents. Follow the instructions in one of the tabs below.
Here’s how to use Plivo APIs and XML to implement voice OTPs.

Prerequisites

To get started, you need a Plivo account — sign up with your work email address if you don’t have one already. If this is your first time using Plivo APIs, follow our instructions to set up a Ruby development environment.

Create a Rails controller

Change to the project directory and run this command to create a Rails controller for the voice OTP application.
$ rails generate controller Plivo voice
It generates a controller named plivo_controller in the app/controllers/ directory and a respective view in app/views/plivo. We can delete the view as we don‘t need it.
$ rm app/views/plivo/voice.html.erb

Create a voice OTP application

Edit app/controllers/plivo_controller.rb file and add this code.
include Plivo
require 'redis'
require 'json'
include Plivo::Exceptions

class PlivoController < ApplicationController
	def dispatch_otp
		redis = Redis.new(host: "localhost")
		code = rand(999_999)
		dst_number = params[:dst_number]
		
		api = RestClient.new("<auth_id>","<auth_token>")
		begin
			response = api.calls.create(
				'<caller_id>',
				[dst_number],
				"https://<yourdomain>.com/answer_url/#{code}"
			)
			puts response
		end
		redis.setex(dst_number, 60, code) # Verification code is valid for 1 min
		puts JSON.pretty_generate({ :status=> 'success', :message=> 'verification initiated' })
	rescue PlivoRESTError => e
		puts 'Exception: ' + e.message
	end
	
	def verify_otp
		redis = Redis.new(host: "localhost")
		code = params[:otp]
		number = params[:number]
		original_code = redis.get(number)
		if original_code == code
			redis.del(number)  # verification successful, delete the code
			puts JSON.pretty_generate( { :status=> 'success', :message=> 'Codes match — number verified'})
		elsif original_code != code
			puts JSON.pretty_generate({ :status => "failure", :message=> 'Codes do not match — number not verified' })
		else
			puts JSON.pretty_generate( { :status=> 'rejected', :message=> 'Number not found' })
		end
	end
end
Replace the auth placeholders with your authentication credentials from the Plivo console. Replace the phone number placeholder with an actual phone number in E.164 format (for example, +12025551234).
Note: We recommend that you store your credentials in the auth_id and auth_token environment variables, to avoid the possibility of accidentally committing them to source control. If you do this, you can initialize the client with no arguments and Plivo will automatically fetch the values from the environment variables. You can use ENV to store environment variables and fetch them when initializing the client.

Add a route

Edit the file config/routes.rb and change the line:
get 'plivo/voice' 
to
get 'plivo/verify_otp'
get 'plivo/dispatch_otp'

Test

Start Rails and Redis.
$ rails server
$ redis-server
You should see your basic server application in action as below:
http://localhost:3000/plivo/dispatch_otp?destination_number=<destination_number>
http://localhost:3000/plivo/verify_otp?destination_number=<destination_number>&otp=<otp>