Skip to contentSkip to navigationSkip to topbar
On this page

Programmable Voice quickstart for Ruby


This quickstart shows you how to build a Ruby application that can make and receive phone calls. It uses the Twilio REST API to make the outbound call and TwiML to respond to the inbound call by reading a message using text-to-speech.


Get a phone number

get-a-phone-number page anchor

Log in to your Twilio account(link takes you to an external page) or sign up for a new one(link takes you to an external page).

If you need a Twilio phone number, buy one in the Twilio Console:

  1. Go to Phone Numbers > Manage > Buy a Number(link takes you to an external page).
  2. Under Capabilities, select Voice.
  3. Click Search. The page displays a list of available voice-capable phone numbers.
  4. Click Buy next to a phone number to add it to your account.

Check if you already have Ruby installed on your computer by opening a terminal and running the following command:

ruby --version

If your computer has Ruby, then you see something like this:

ruby 2.7.2

You'll need Ruby 2.6 or later to run this quickstart. If you don't have a matching version of Ruby on your computer, then follow the Ruby installation instructions for your operating system(link takes you to an external page).


Create a new directory for this project and navigate to it using the command line. Run the following commands to create a Gemfile and add the dependencies that this quickstart relies on.

1
bundle init
2
bundle add puma twilio-ruby sinatra

Make an outgoing phone call with Ruby

make-an-outgoing-phone-call-with-ruby page anchor
(error)

Danger

This quickstart hardcodes your credentials to get you started quickly. Use environment variables and API keys to keep credentials secret and control access when you deploy to production.

  1. Create a new file named make_call.rb with the following code:

    1
    require 'twilio-ruby'
    2
    3
    # To set up environment variables, see https://twil.io/secure
    4
    account_sid = ENV['TWILIO_ACCOUNT_SID']
    5
    auth_token = ENV['TWILIO_AUTH_TOKEN']
    6
    7
    # Set up a client to talk to the Twilio REST API
    8
    @client = Twilio::REST::Client.new(account_sid, auth_token)
    9
    10
    call = @client.calls.create(
    11
    to: "+15558675310",
    12
    from: "+15017122661",
    13
    url: "http://demo.twilio.com/docs/voice.xml")
    14
    puts call.to

    This code starts a phone call between the two phone numbers that you pass as arguments. The from number is your Twilio number, and the to number is the call recipient.

    The url argument points to a TwiML file that tells Twilio what to do when the call recipient answers their phone. This TwiML instructs Twilio to read a message using text-to-speech and then play an MP3.

  2. Log in to the Twilio Console(link takes you to an external page).

  3. Copy the Account SID and replace ENV['TWILIO_ACCOUNT_SID'] with it in the make_call.rb file.

  4. Copy the Auth Token and replace ENV['TWILIO_AUTH_TOKEN'] with it.

  5. Copy your Twilio phone number and replace the from value in make_call.rb with it.

  6. Replace the to value with your own phone number (the number you want to call).

  7. Save the file.

  8. Run the script using this command:

    ruby make_call.rb

    Your phone should ring with a call from your Twilio number. Answer it, and you'll hear a short message.

(information)

Twilio trial account limitations

Twilio trial accounts limit outgoing phone calls to phone numbers you have verified with Twilio. To verify your phone numbers, use your Twilio Console's Verified Caller IDs(link takes you to an external page). To learn about other trial account limitations, see how to work with your free Twilio trial account.


Receive a phone call with your Twilio number

receive-a-phone-call-with-your-twilio-number page anchor

Create and start your Sinatra app

create-and-start-your-sinatra-app page anchor
  1. Create a file in your project directory named answer_call.rb.
  2. Add the following code to create a server that responds to incoming calls:
    1
    require 'sinatra'
    2
    require 'twilio-ruby'
    3
    4
    configure :development do
    5
    set :host_authorization, { permitted_hosts: [] }
    6
    end
    7
    8
    def self.get_or_post(url,&block)
    9
    get(url,&block)
    10
    post(url,&block)
    11
    end
    12
    13
    get_or_post '/' do
    14
    content_type 'text/xml'
    15
    16
    Twilio::TwiML::VoiceResponse.new do | response |
    17
    response.say(message: "Ahoy world!")
    18
    end.to_s
    19
    end
  3. Save the file.
  4. Start your Sinatra server with the following command:
    ruby answer_call.rb

Expose your local server to the internet with ngrok

expose-your-local-server-to-the-internet-with-ngrok page anchor
  1. If you don't already use ngrok, then download and install it(link takes you to an external page).

  2. Ensure your Sinatra server is still running, then open a new command line window and start ngrok with this command:

    ngrok http 4567

    You should see output that resembles this:

    1
    Session Status online
    2
    Version 3.23.2
    3
    Web Interface http://127.0.0.1:4040
    4
    Forwarding https://ff4660c91380.ngrok.app -> http://localhost:4567
    5
    6
    Connections ttl opn rt1 rt5 p50 p90
    7
    0 0 0.00 0.00 0.00 0.00
  3. Copy the URL ending in ngrok.app and paste it into your browser. You should see a TwiML document that contains the string 'Ahoy world!'

  4. In the Twilio Console, go to Phone Numbers > Manage > Active numbers(link takes you to an external page).

  5. Click your Twilio phone number.

  6. On the Configure tab, under the Voice Configuration section, set A call comes in to Webhook.

  7. Enter your ngrok public URL in the URL field (for example, https://ff4660c91380.ngrok.app).

  8. Click Save configuration.

  9. Head back to your terminal. Make sure that ngrok is still running in one tab and your Sinatra server is running in another tab.

Make a phone call to your Twilio phone number. You'll hear a short message once the call connects.


Your new application uses the <Say> TwiML verb to read a message to the caller using text-to-speech. You can create more powerful constructs and call flows using other TwiML verbs. Try a few, such as <Record>, <Gather>, and <Conference>.

Check out these pages to learn more:

Let's build something amazing.