Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Programmable Voice Quickstart for Ruby


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(link takes you to an external page), and Ruby's Sinatra(link takes you to an external page) framework to ease development.
In this quickstart, you will learn how to:

  1. Sign up for Twilio and get your first voice-enabled Twilio phone number
  2. Set up your development environment to make and receive phone calls
  3. Make an outbound phone call which plays an MP3
  4. Receive and respond to an inbound phone call which reads a message to the caller using Text to Speech

Prefer to get started by watching a video? Check out our video on how to place and receive phone calls with Ruby on Youtube(link takes you to an external page).


Sign up for Twilio and get a phone number

sign-up-for-twilio-and-get-a-phone-number page anchor
(information)

Info

If you already have a Twilio account and a voice-enabled Twilio phone number you're all set here! Log in(link takes you to an external page) 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(link takes you to an external page) to get your free trial account or log in(link takes you to an external page) to an account you already have.

The next thing you'll need is a voice-capable Twilio phone number(link takes you to an external page). 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(link takes you to an external page) page, check the "Voice" box and click "Search."

Search for a voice-enabled phone number.

_10
ruby --version

You should see something like:


_10
$ ruby --version
_10
ruby 2.7.2

Windows users can use RubyInstaller(link takes you to an external page) 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(link takes you to an external page), RVM(link takes you to an external page) or Homebrew(link takes you to an external page) to upgrade to the minimum supported version.)

Install the Twilio Ruby Helper Library

install-the-twilio-ruby-helper-library page anchor

The easiest way to install twilio-ruby is from RubyGems.


_10
gem install twilio-ruby

Manual Installation
Or, you can clone the source code(link takes you to an external page) 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.


Make an outgoing phone call with Ruby

make-an-outgoing-phone-call-with-ruby page anchor

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.

Make a phone call using Twilio

make-a-phone-call-using-twilio page anchor

_15
require '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
_15
account_sid = ENV['TWILIO_ACCOUNT_SID']
_15
auth_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
_15
call = @client.calls.create(
_15
to: "+15558675310",
_15
from: "+15017122661",
_15
url: "http://demo.twilio.com/docs/voice.xml")
_15
puts 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.

Replace the placeholder credential values

replace-the-placeholder-credential-values page anchor

Swap the placeholder values for account_sid and auth_token with your personal Twilio credentials.

Go to https://www.twilio.com/console(link takes you to an external page) 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:

Reveal Your Auth Token.Rate this page:

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.