Verify Ruby on Rails Quickstart
With just a few lines of code, your Rails application can verify phone numbers and add an additional layer of security with Twilio Verify.
This Ruby Verify Quickstart will teach you how to do this using our Verify REST API, the Twilio Ruby helper library, and Ruby on Rails to ease development.
In this Quickstart, you will learn how to:
- Sign up for Twilio
- Set up your development environment
- Send your first SMS phone verification
- Check verification codes
Short on time? Spin up a low-code, fully editable verification demo in less than 2 minutes using Twilio's Code Exchange and Quick Deploy here.
Sign up for Twilio
If you already have a Twilio account, you’re all set here! Feel free to jump to the next step.
Before you can send an SMS from Python, you'll need to sign up for a Twilio account or sign into your existing account.
You can sign up for a free Twilio trial account here.
- When you sign up, you'll be asked to verify your personal phone number. This helps Twilio verify your identity and also allows you to send test verification messages to your phone from your Twilio account while in trial mode. This phone verification step is exactly what you'll learn how to build in this tutorial!
- Once you verify your number, you'll be asked to create a project. For the sake of this tutorial, you can click on the "Learn and Explore" template. Give your project a name, or just click "skip remaining steps" to continue with the default.
- Once you get through the project creation flow, you'll arrive at your project dashboard in the Twilio Console. This is where you'll be able to access your Account SID, authentication token, create a verification service, and more.
Do I need a phone number?
If you've sent SMS with Twilio in the past, you might remember needing to buy a phone number. With Twilio Verify, we take care of that for you! The Verify API selects the best routes for quickly and reliably delivering verification codes globally.
Create a Verify Service
Verify uses Services for configuration. To send a Verify API request you will need both your Twilio Credentials and a Service SID. You can create and update a Service in two ways:
- In the Verify Console
- With the Verify API
Services can be used to edit the name (which shows up in the message template), set the code length (4-10 characters), enable settings like the "do not share warning" and more.
Now that you have a Twilio account and a verification service, you can start writing some code! To make things even easier, we'll next install Twilio's official helper for Ruby applications.
Install Ruby and the Twilio Helper Library
If you’ve gone through one of our other Ruby Quickstarts already and have Ruby and the Twilio Ruby helper library installed, you can skip this step and get straight to sending your first verification.
To start a phone verification, you’ll need to have Ruby and the Twilio Ruby helper library installed.
Install Ruby
If you're using Mac, you probably already have Ruby installed but an older version. You can use homebrew to install the latest version using the following command:
brew install ruby
If you’re on a Linux machine, you can install it using your default package manager, for example for Ubuntu:
sudo apt-get install ruby-full
For other distributions, you can find the command in this listing.
You can check if Ruby is installed and which version by opening up a terminal and running the following command:
ruby --version
You should see something like:
$ ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18]
Windows users can use RubyInstaller.
Twilio’s Ruby SDK supports older versions of Ruby. But for this quickstart, we recommend using at least Ruby 2.3 for compatibility with some of the dependencies used.
Clone and Setup the Verification Application
Start by cloning our Rails repository.
git clone git@github.com:TwilioDevEd/verify-v2-quickstart-rails.git
If you don't have git installed or prefer to download the source code you can grab a zip file of the project here.
Set up your environment and install dependencies
You'll need to have bundler installed in your Ruby environment, simply run the command:
gem install bundler
Once it's installed, you'll be able to install all the quickstart's dependencies by navigating to its directory and running bundler:
cd verify-v2-quickstart-rails
bundle install
Copy .env.example
to .env
. This is where we'll store sensitive data in environment variables.
cp .env.example .env
the application will automatically read and load those environments variables from the .env
file. Finally, all that's left to do is run the migrations which will prepare an SQLite database automatically for you and start the development server:
rails db:migrate
rails server
If your credentials are set up correctly you'll soon get a message that the app is up!
Use the Rails Twilio Verify Demo
Navigate to http://localhost:3000/register. You should see a registration form that looks like this:
Sign Up
button and wait. You'll either receive a phone call or an SMS with the verification token. If you requested a phone call, as an additional security feature you may need to interact to proceed (the call will tell you to enter a number on the phone keypad).
Enter the token into the Verification entry form and click 'Verify':
And with that, your demo app is protected with Twilio's Phone Verification!
What's Next?
Your demo app is now keeping fraudulent users from registering with your business and polluting your database. Next, check out all of the variables and options available to you in the Verify API Reference.
After that, check out adding additional verification channels supported by the Verify API like:
Lastly, to protect your service against fraud, view our guidance on Preventing Toll Fraud when using Verify.
Need some help?
We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.