With just a few lines of code, your Ruby application can make and receive phone calls with Twilio Programmable Voice.
This Ruby quickstart will teach you how to do this using our REST API, the Twilio Ruby helper library, and Ruby's Sinatra framework to ease development.
In this quickstart, you will learn how to:
Prefer to get started by watching a video? Check out our video on how to place and receive phone calls with Ruby on Youtube.
If you already have a Twilio account and a voice-enabled Twilio phone number you're all set here! Log in then feel free to jump to the next step.
Before you can make a phone call from Ruby, you'll need a Twilio account. Sign up here to get your free trial account or log in to an account you already have.
The next thing you'll need is a voice-capable Twilio phone number. If you don't currently own a Twilio phone number with voice call functionality, you'll need to purchase one. After navigating to the Buy a Number page, check the "Voice" box and click "Search."
_10ruby --version
You should see something like:
_10$ ruby --version_10ruby 2.7.2
Windows users can use RubyInstaller to install Ruby.
Twilio's Ruby SDK is tested against and supports Ruby versions from 2.4 through 3.0. (Got an older version of Ruby? You can use rbenv, RVM or Homebrew to upgrade to the minimum supported version.)
The easiest way to install twilio-ruby is from RubyGems.
_10gem install twilio-ruby
Manual Installation
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 long error message that says Permission Denied in the middle of it, try running the above commands with sudo: sudo gem install twilio-ruby.
Now that we have Ruby and twilio-ruby
installed, we can make an outgoing phone call with a single API request from the Twilio phone number we just purchased. Create a new file called make_call.rb
and type or paste in this code sample.
_15require 'twilio-ruby'_15_15# Get your Account Sid and Auth Token from twilio.com/console_15# To set up environmental variables, see http://twil.io/secure_15account_sid = ENV['TWILIO_ACCOUNT_SID']_15auth_token = ENV['TWILIO_AUTH_TOKEN']_15_15# set up a client to talk to the Twilio REST API_15@client = Twilio::REST::Client.new(account_sid, auth_token)_15_15call = @client.calls.create(_15 to: "+15558675310",_15 from: "+15017122661",_15 url: "http://demo.twilio.com/docs/voice.xml")_15puts call.to
This code starts a phone call between the two phone numbers that we pass as arguments. The 'from' number is our Twilio number, and the 'to' number is who we want to call.
The URL argument points to some TwiML (Twilio Markup Language), which tells Twilio what to do next when our recipient answers their phone. This TwiML tells Twilio to read a message using text to speech and then play an MP3.
Before this code will work, though, we need to edit it a little to work with your Twilio account.
Swap the placeholder values for account_sid
and auth_token
with your personal Twilio credentials.
Go to https://www.twilio.com/console and log in. On this page, you'll find your unique Account SID and Auth Token, which you'll need any time you send messages through the Twilio client like this. You can reveal your auth token by clicking on the eyeball icon: