Phone number verification by SMS or voice method by using Authy verification API in Ruby on Rails

Phone number verification by SMS or voice method by using Authy verification API in Ruby on Rails

26 Sep 2019
Phone number verification by SMS or voice method by using Authy verification API in Ruby on Rails

 What is Authy Verification API?

Authy is a more complete authentication API and best for an ongoing relationship with a customer. Authy provides SMS and voice time-based one-time password(OTP). Its primary use is two-factor Authentication(2FA). It supports OTP send via voice and SMS.

Purpose of phone number verification

1) Phone number verification is the process of verifying a phone number is valid, reachable and accessible by the user.

2) Phone number verification is used as a form of two-factor authentication.

3) Phone verification can be done by SMS or by voice.

Things needed before starting

1) Twilio account by visiting


2) Authy app which you can create in Twilio console from where you will need an API key.

3) Ruby version: 2.5.1 and rails version: 5.2.3


Getting started

1) Create a new application in rails: rails new application_name

2) cd application_name is used to go to a particular file.

3) Go to gemfile and add

gem ‘authy’

gem ‘envyable’

4) Run bundle install

5) Initialize envyable from the command line by running the command

bundle exec envyable install

6) Open config/env.yml and add authy api key

7) Create a file authy.rb in config/initializer and add the following code
require 'authy'

Authy.api_key = ENV['AUTHY_API_KEY']

Authy.api_uri = ''

The process for Phone number verification includes

1) Three information

1.1) Country code.

1.2) Phone number.

1.3) methods to verify phone number includes SMS or Voice.


2) Application which we have created in rails make a request to API to initiate verification.

3) If a user chose a SMS method then, user will get a text message with a 4-digit code.

4) User enter a 4-digit code into a form.

5) The code is sent to Authy API with a country code and phone number. If a entered 4-digit code is correct then User has verified a number.


Building an app

1)First, create a controller by running a command

rails generate controller phone_verifications

2) To generate a list of country code so that we can search by country code of the particular country.

Add this in app/views/layouts/application.html.erb

<%= stylesheet_link_tag', media: 'all', 'data-turbolinks-track': 'reload' %>

<%= javascript_include_tag '', 'data-turbolinks-track': 'reload'%>

3) Create a new file as new.html.erb in app/views/your_application_name/new.html.erb

add a code for country code and phone number and verification methods such as SMS and call

authy-countries is an id of authy javascript used to get all country code and telepone_field_tag is input tag to accept the only mobile number.

4) Run server to see whether we are getting a mobile verification form or not.

5) The most important task is to create a method in the controller where country code and phone number parameters are passed. The session is used to store data.


6) After entering all details in mobile verification there should be a form where user can enter OTP received in mobile either by receiving a text message or by voice. For creating a text field to enter OTP I have created challenge.html.erb in views.


7) Verify method is created in the controller to verify entered OTP is correct or not. Here PhoneVerification.check passing of all parameter of phone number verification form.

8) If an OTP entered by the user is correct then there should be a message as congratulation your entered OTP is correct. We can do this by creating a success.html.erb in app/views/phone_verifications/success.html.erb as


<p>You have successfully verified your Phone Number.</p>

9) Run a server to see phone verification by using either an SMS or Voice method is working properly.

rails s command is used to run a server.

In phone number verification by voice or SMS, we have created a controller. It can be done with the help of model to save records in the database for phone number.


Leave a comment: