Sending SMS Messages in Ruby on Rails with Twilio

Twilio Intro & Setup

Twilio is a powerful cloud communications platform which provides an interface for SMS, voice call utilities, answering machines, virtual phone lines, and more.

Today, we will be creating a very basic Rails 6 app that will send our new user’s a text via Twilio’s SMS API upon successful account creation. As it turns out, with only a few lines of code we can get up and running thanks to Twilio’s API.

Setup

  1. If you don’t already have a Twilio account, you can sign up for a free trial . FYI — this gives each user $15.00 worth of credit to play around. Each successful SMS/Voice Message you send costs around $0.10. Another caveat of the trial version is that you can only send to ‘verified’ phone numbers which you have to manually setup one by one.
  2. After you sign up, Twilio will provide you with your Account SID, Auth Token & Twilio phone number. Keep these nearby as we’ll be adding them to our Rails app to setup our Twilio Client.

Rails Setup

Twilio and the Ruby community have created a gem to make integration with Twilio an absolute breeze.

Let’s add the add the following line to our Gemfile in order to bring in Twilio’s functionality:

gem 'twilio-ruby'

After a bundle install we need to add them to an environment file in order to keep our account information private. You can do this in a number of ways; in the config/secrets.yml file, with the help of the gem, or in our case we’ll store in Rails’ credentials file. For additional detail on Custom Credentials check out the following section of the Rails Guides ().

We can edit the credentials file with the following command:

rails credentials:edit --environment=development 

Once the .yml file opens up you can setup your account_sid, auth_token and your phone number (this is optional as it’s not really private but I’m adding for ease of use as all my Twilio credentials will now be in the same spot).

twilio:
account_sid: YOUR_TWILIO_ACCT_SID
auth_token: YOUR_TWILIO_AUTH_TOKEN
phone_number: YOUR_TWILIO_PHONE_NUM

We will now create a wrapper around the Twilio library to make it easier to interface in our code with their API. To do this we are creating a directory under app called /services and within that new directory a file called twilio_client.rb which we’ll populate with the following code.

We are first initializing our Twilio Rest Client by passing in our account_sid and auth_token. We are pulling out each of the respective credentials that we setup in the previous step and then making those methods private. Once setup I recommend testing in the rails console to ensure you can instantiate a new client.

After we have successfully setup our Twilio Client we need to create a method to actually send our messages in our TwilioClient class. This is well laid out in the gem’s docs and pretty straightforward. After we have our new method setup we’ll now call it from inside our controller after a successful user creation to send them a welcome text.

And believe it or not just like that we’re all set with our integration and are able to send SMS text messages out to our new users!