Skip to contentSkip to navigationSkip to topbar
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.

You'll then see a list of available phone numbers and their capabilities. Find a number that suits your fancy and click "Buy" to add it to your account.

Purchase a voice-enabled phone number from Twilio.

Now that you have a Twilio account and a programmable phone number, you have the basic tools you need to make a phone call.

Next, we'll install Twilio's official Ruby helper library to help us communicate with the Twilio APIs.


Install Ruby and the Twilio Helper Library

install-ruby-and-the-twilio-helper-library page anchor
(information)

Info

If you've gone through one of our other Ruby Quickstarts already and have Ruby and the Twilio Ruby helper library installed, you can skip this step and get straight to making your first phone call.

To make your first phone call, you'll need to have Ruby and the Twilio Ruby helper library installed.

Install Ruby

install-ruby page anchor

If you're using a Mac or Linux machine, you probably already have Ruby installed. You can check this by opening up a terminal and running the following command:

ruby --version

You should see something like:

1
$ ruby --version
2
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.

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
1
require 'twilio-ruby'
2
3
# Get your Account Sid and Auth Token from twilio.com/console
4
# To set up environmental variables, see http://twil.io/secure
5
account_sid = ENV['TWILIO_ACCOUNT_SID']
6
auth_token = ENV['TWILIO_AUTH_TOKEN']
7
8
# set up a client to talk to the Twilio REST API
9
@client = Twilio::REST::Client.new(account_sid, auth_token)
10
11
call = @client.calls.create(
12
to: "+15558675310",
13
from: "+15017122661",
14
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.

Open make_call.rb and replace the values for account_sid and auth_token with your unique values.

(error)

Danger

Please note: it's okay to hardcode your credentials when getting started, but you should use environment variables to keep them secret before deploying to production. Check out how to set environment variables(link takes you to an external page) for more information.

Replace the to and from phone numbers

replace-the-to-and-from-phone-numbers page anchor

Remember that voice-enabled phone number you bought just a few minutes ago? Go ahead and replace the existing from number with that one, making sure to use E.164 formatting:

[+][country code][phone number including area code]

Next, replace the to phone number with your mobile phone number. This can be any phone number that can receive calls, but it's a good idea to test with your phone so that you can see the magic happen! As above, you should use E.164 formatting for this value.

Save your changes and run the script from your terminal:

ruby make_call.rb

That's it! Your phone should ring with a call from your Twilio number, and you'll hear our short message for you. 😉

(warning)

Warning

If you're using a Twilio trial account, outgoing phone calls are limited to phone numbers you have verified with Twilio. Phone numbers can be verified via your Twilio Console's Verified Caller IDs(link takes you to an external page). For other trial account restrictions and limitations, check out our guide on how to work with your free Twilio trial account.

Next, we'll learn how to respond to a call made to your Twilio phone number. First, we'll need to get a Sinatra server up and running.


Install Sinatra and set up your development environment

install-sinatra-and-set-up-your-development-environment page anchor

In order to receive and reply to incoming SMS messages, we'll need to create a very lightweight web application that can accept incoming requests. We'll use Sinatra(link takes you to an external page) for this Quickstart, but if you prefer to use Rails, you can find instructions in this blog post(link takes you to an external page).
First, you need a Gemfile with the following content in it:

1
# Gemfile
2
source 'https://rubygems.org'
3
4
gem 'sinatra'
5
gem 'twilio-ruby'
6
7

Ruby projects use Bundler(link takes you to an external page) to manage dependencies, so the command to pull Sinatra and the Twilio SDK into our development environment is bundle install. (If you don't have Bundler installed on your machine already, you'll need to run gem install bundler first.)

1
$ bundle install
2
1
Use `bundle show [gemname]` to see where a bundled gem is installed.
2
3

Create a Sinatra application

create-a-sinatra-application page anchor

We can test that our development environment is configured correctly by creating a Sinatra application. We'll grab the example from Sinatra's documentation and drop it in a new file called quickstart.rb.

1
require 'sinatra'
2
require 'twilio-ruby'
3
4
get '/' do
5
content_type 'text/xml'
6
7
Twilio::TwiML::VoiceResponse.new do | response |
8
response.say(message: "Hello World")
9
end.to_s
10
end

We can then try running our new Sinatra application with the command ruby quickstart.rb. You can then open http://localhost:4567(link takes you to an external page) in your browser and you should see the <?xml version="1.0" encoding="UTF-8"?><Response><Say>Hello World</Say></Response> response.


Allow Twilio to talk to your Sinatra application

allow-twilio-to-talk-to-your-sinatra-application page anchor

We're building a small Sinatra application to accept incoming phone calls. Before we do that, we need to make sure that Twilio can reach our application.
Most Twilio services use webhooks to communicate with your application. When Twilio receives a phone call, for example, it reaches out to a URL in your application for instructions on how to handle the call.
When you're working on your Sinatra application in your development environment, your app is only reachable by other programs on your computer, so Twilio won't be able to talk to it. We need to solve this problem by making your application accessible over the internet.
While there are a lot of ways to do this, like deploying your application to Heroku or AWS, you'll probably want a less laborious way to test your Twilio application. For a lightweight way to make your app available on the internet, we recommend a tool called ngrok. Once started, ngrok provides a unique URL on the ngrok.io domain which forwards incoming requests to your local development environment.
It works something like this:

How ngrok helps Twilio reach your local server.

If you don't already use ngrok, head over to their download page(link takes you to an external page) and grab the appropriate binary for your operating system. Once downloaded, unzip the package.
If you're working on a Mac or Linux, you're all set. If you're on Windows, follow our guide on how to install and configure ngrok on Windows. For more info on ngrok, including some great tips and tricks, check out this in-depth blog post(link takes you to an external page).

Once you've got ngrok set up, start that Sinatra application we made previously:

ruby quickstart.rb

Your application must be running locally for ngrok to do its magic.

Then open a new terminal tab or window and start ngrok with this command:

./ngrok http 4567

4567 is the default port for Sinatra applications. If your local server is running on a different port, replace 4567 with the correct port number.

You should see output similar to this:

Ngrok server terminal output.

Copy your public URL from this output and paste it into your browser. If everything's working correctly, you should see your Sinatra application's <?xml version="1.0" encoding="UTF-8"?><Response><Say>Hello World</Say></Response>message.


Respond to incoming calls with Twilio

respond-to-incoming-calls-with-twilio page anchor

When your Twilio number receives an incoming phone call, it sends an HTTP request to your server asking for instructions on what to do next. Once you receive the request, you can tell Twilio how to respond to the call.

For this quickstart, we'll have our Sinatra app reply to answer the phone call and say a short message to the caller. Open up quickstart.rb again and update the code to look like this code sample:

Respond to an incoming call with a brief message

respond-to-an-incoming-call-with-a-brief-message page anchor

Respond to an incoming request from Twilio with instructions on how to handle the call

1
require 'bundler'
2
Bundler.require()
3
4
def self.get_or_post(url,&block)
5
get(url,&block)
6
post(url,&block)
7
end
8
9
get_or_post '/answer' do
10
11
Twilio::TwiML::VoiceResponse.new do |r|
12
r.say(message: "Thank you for calling! Have a great day.")
13
end.to_s
14
15
end

Respond to an incoming request from Twilio with instructions on how to handle the call

Save the file and restart your app with

ruby quickstart.rb

You should now be able to open a web browser to http://localhost:4567/answer(link takes you to an external page). If you view the page source code, you should see the following text:

1
<?xml version="1.0" encoding="UTF-8"?>
2
<Response>
3
<Say">Thank you for calling! Have a great day.</Say>
4
</Response>

This source code is TwiML XML generated by your code with the help of the Twilio helper library.

Double-check that ngrok is still running on localhost with the same port as before. Now Twilio will be able to find your application. There's just one last thing we need before we're ready to call your app: we need to tell Twilio where to send its request.

Configure your webhook URL

configure-your-webhook-url page anchor

For Twilio to know where to look, you need to configure your Twilio phone number to call your webhook URL whenever a call comes in.

  1. Log into twilio.com and go to the Numbers page in the Console(link takes you to an external page).
  2. Click on your voice-enabled phone number.
  3. Find the Voice & Fax section. The default "CONFIGURE WITH" is what you'll need: "Webhooks, TwiML Bins, [etc.]"
  4. In the "A CALL COMES IN" section, select "Webhook" and paste in the URL you want to use (don't forget the /answer endpoint!).
Configure webhook on phone number for voice.

Save your changes - you're ready!

As long as your localhost and ngrok server are up and running, we're ready for the fun part - testing our new Sinatra application!
Make a phone call from your mobile phone to your Twilio phone number. You should see an HTTP request in your ngrok console. Your Sinatra app will process the incoming request and respond with your TwiML. Then you'll hear your message once the call connects.


Now you know the basics of making and responding to phone calls with Ruby.
Our Sinatra app here only used the <Say> TwiML verb to read a message to the caller using text to speech, but you can do much more with different TwiML verbs like <Record>, <Gather>, and <Conference>.
Check out these pages to learn more:

We can't wait to see what you build!

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.