The Twilio Ruby Helper Library

The twilio-ruby helper library lets you write Ruby code to make HTTP requests to the Twilio API.

This library is open source, so if you find a feature missing or a bug, we encourage you to contribute back to the twilio-ruby project hosted on GitHub.

More Documentation

If you'd like to learn more about how Twilio works, or for inspiration about what to build, try the Ruby quickstarts.

The Rest API Documentation has examples written in Ruby for every kind of action you could take with the Twilio API. You should start there if you know what you want to do with the Ruby library.

You may also want to refer to the open source twilio-ruby specific documentation for more details about the methods and classes contained in the gem.

Installation

The easiest way to install twilio-ruby is from RubyGems. In the command line, run gem install twilio-ruby:

# Be sure to type "twilio-ruby", not just "twilio"
gem install twilio-ruby

Or, you can clone the source code for twilio-ruby, and install the library from there.

"Permission Denied"

If the command line gives you a big long error message that says Permission Denied in the middle of it, try running the above commands with sudo, ex. sudo gem install twilio-ruby.

Testing your installation

Try sending yourself an SMS message, like this:

require 'twilio-ruby'

account_sid = "{{ account_sid }}" # Your Account SID from www.twilio.com/console
auth_token = "{{ auth_token }}"   # Your Auth Token from www.twilio.com/console

@client = Twilio::REST::Client.new account_sid, auth_token
message = @client.account.messages.create(:body => "Hello from Ruby",
    :to => "+12345678901",    # Replace with your phone number
    :from => "+12345678901")  # Replace with your Twilio number

puts message.sid

Using the Gem

Try the Ruby quickstarts and refer to the open source twilio-ruby specific documentation for details about the methods and classes contained in the library.

Handling Exceptions

If the Twilio API returns a 400 or a 500 level HTTP response, the twilio-ruby library will throw a Twilio::REST::RequestError. 400-level errors are normal during API operation (“Invalid number”, “Cannot deliver SMS to that number”, for example) and should be handled appropriately.

require 'twilio-ruby'

account_sid = "{{ account_sid }}" # Your Account SID from www.twilio.com/console
auth_token = "{{ auth_token }}"   # Your Auth Token from www.twilio.com/console

begin
    @client = Twilio::REST::Client.new account_sid, auth_token
    message = @client.account.messages.create(:body => "Hello from Ruby",
        :to => "+12345678901",    # Replace with your phone number
        :from => "+12345678901")  # Replace with your Twilio number
rescue Twilio::REST::RequestError => e
    puts e.message
end

Getting Help

Still running into problems?

Installing the Next-Gen Version

The "next-gen" version of the Twilio library will markedly improve the developer experience. As we put the finishing touches on this new way of building helper libraries, we need to hear your feedback. Please let us know what you think.

To install the next-gen version of twilio-ruby install the gem as follows, specifying the 5.0.0-rc5 version:

gem install twilio-ruby -v 5.0.0.rc5

Working with the helper library is a bit different than the current version. Here's an example of how to send an SMS:

require 'twilio-ruby'

account_sid = '{{ account_sid }}' # Your Account SID from www.twilio.com/user/account
auth_token = '{{ auth_token }}'   # Your Auth Token from www.twilio.com/user/account

@client = Twilio::REST::Client.new account_sid, auth_token
message = @client.account.messages.create(
  from: '+12345678901',
  to: '+12345678901',
  body: 'Hey there from ruby!'
)

puts message.sid

Visit the next-gen branch on GitHub for more information and to provide feedback.