Twilio API with Ruby on Rails

Ashraful Alam
Oceanize Lab Geeks
Published in
3 min readSep 29, 2017

Twilio is a cloud communication platform. By using Twilio you can send SMS, Voice, Video. Twilio support for web and mobile platform. You have to follow some steps. Let’s check the steps how to integrate Twilio with Ruby on Rail application.

Step 1: Create and Configure Twilio Account

You must have an Twilio account maybe it’s trial or paid. First of all sign up your account with required information. To confirm you account you have to verify your by your mobile number via SMS or Call.

After verification Twilio provide you a mobile number. You have to use this mobile number when you send any request to API.

Note: If you are using trial account then you can communicate only the verified phone number.

To verify phone number you have access this URL: https://www.twilio.com/console/phone-numbers/verified

Step 2: Integrate Twilio on Rails App

To integrate Twilio on Rails you have to use twilio-ruby gem. Open gemfile and add the following line.

gem ‘twilio-ruby’

Then run bundle install

Step 3: Write Rails Code

After install Rails app and twilio-ruby gem you have to add route and related code.

routes.rb

In the below image you can see account_sid and auth_token variable. This Account SID and Auth Token provided by Twilio. And in from parameter at exec! method here you have to set the mobile number which you get from Twilio.

twilio_command.rb

After send API request it’s call controller and controller run twilio command to send messages.

notification_controller.rb

Wrapping Up

At the end to integrate Twilio with Rails you have to follow above codes. First you have to create and configure Twilio account after that install gem and make controller, command, route for this. I write code this way maybe you can follow others process. This example only for Messages you can send Audio and Video also by changing very limited code.
Happy Coding

--

--