Ask A Question


You’re not receiving notifications from this thread.

How to send SMS Messages in Rails with Twilio Discussion

Great intro! We use Twilio to send SMS notifications to our users.

Question - I set up the Twilio client in an initializer. Is there any advantage to this over setting it up in the Twilio service object? I know global variables are generally frowned upon but they seem ok for clients like this.

# config/initializers/twilio.rb
require 'twilio-ruby'

Twilio.configure do |config|
  config.account_sid = ENV['TWILIO_ACCOUNT_SID']
  config.auth_token  = ENV['TWILIO_AUTH_TOKEN']

$twilio =

And thanks for the tip on the Google phone number gem. I've been using Twilio's Lookup REST API to validate phone numbers. It's likely more accurate but makes an API for every validation. Not so great. I think I'll be switching to phonelib soon 👍


That initializer works too. I'm just putting everything in the same class for now to keep it organized.


when i run this

  • rails credentials:edit --environment=development No $EDITOR to open file in. Assign one like this:

EDITOR="mate --wait" bin/rails credentials:edit

For editors that fork and exit immediately, it's important to pass a wait flag,
otherwise the credentials will be saved immediately with no chance to edit.


This is what I am using:
EDITOR=vim rails credentials:edit --environment=development


Interesting curve on the road to sending a Twilio SMS. Running the command as per the send_text method suggested, ran the first time from localhost.

But somehow, after adjusting the method to be able to handle the API response, Rails 5 complains with:
ArgumentError (Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true) I am certain the method is firing (because when the conditions are not met, the console prompt returns to a ready state & testing without the added line generates the SMS).

Thus, adding status_callback: update_twilio_reminder_url(reminder.user_id),to the method is hanging because it needs a defaulturl option. But which? certainly not action_mailer's? config.application.default_url_options? config.application.routes.default_url_options? On this point, the rails guide is as opaque as the error message...


You can always include the host: option in your url helpers, but I always forget which default_url_options it looks for when that error comes up.

I would think it would be the routes default_url_options.

Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,969+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.