Skip to contentSkip to navigationSkip to topbar
Page toolsOn this page

Set up your Ruby and Sinatra development environment


This tutorial covers how to build a Twilio Voice web application in the Ruby language and Sinatra framework. It includes the configuration settings needed for Twilio webhook testing.

Time to complete: Approximately 20-45 minutes depending on your operating system and internet speed.


Prerequisites

prerequisites page anchor

Before you begin this tutorial, configure, acquire, and install the following:


Build a Twilio Voice web app

build-a-twilio-voice-web-app page anchor

Using VS Code, create a directory and configure your Sinatra settings for your app:

  1. Open VS Code.

  2. To open the terminal in VS Code, go to View > Terminal.

  3. Create a directory for your project.

    1
    mkdir my-twilio-project
    2
    cd my-twilio-project
    3
    code .
  4. Install the required gems:

    gem install sinatra rackup puma twilio-ruby

Create the application source file

create-the-application-source-file page anchor

To create a Twilio app using Sinatra, create Ruby file, run it with Sinatra, and verify its response in a browser:

  1. Add a file called index.rb to your workspace.

  2. with the following code:

    index.rb

    indexrb page anchor
    1
    require 'rack'
    2
    require 'rack/handler/puma'
    3
    require 'twilio-ruby'
    4
    5
    puts "Sinatra server starting..." # This confirms that the server is running.
    6
    7
    class BasicApp
    8
    def call(env)
    9
    if env['REQUEST_METHOD'] == 'GET' && env['PATH_INFO'] == '/'
    10
    twiml = Twilio::TwiML::VoiceResponse.new do |response|
    11
    response.say(message: 'Hello World')
    12
    end
    13
    [200, {'Content-Type' => 'text/xml'}, [twiml.to_s]]
    14
    else
    15
    [404, {'Content-Type' => 'text/plain'}, ['Not Found']]
    16
    end
    17
    end
    18
    end
    19
    20
    options = {
    21
    Host: '0.0.0.0',
    22
    Port: 4567
    23
    }
    24
    Rack::Handler::Puma.run(BasicApp.new, **options)
    25
    # This line of code bypasses Sinatra's protection middleware which allows external requests.
  3. Save this file in your workspace.

  4. Run your Twilio Voice web app in the terminal.

    ruby index.rb &
  5. Open your browser to http://localhost:4567. The browser should display:

    <?xml version="1.0" encoding="UTF-8"?><Response><Say>Hello World</Say></Response>

    This verifies the proper configuration of your Ruby and Sinatra development environment and that you can access your app on your local system.


Check your webhook configuration

check-your-webhook-configuration page anchor

Web applications launched from your computer can't be accessed outside your computer. To communicate with your app, most Twilio services use webhooks(link takes you to an external page).

For example: When Twilio receives an incoming phone call, it requests a URL in your app for instructions on how to handle the call.

To allow webhooks from Twilio to access your application, use ngrok.

  1. Start your app as described previously.

  2. Start ngrok with the following options:

    ngrok http 4567

    Terminal output resembles the following:

    1
    ngrok by @inconshreveable
    2
    3
    Tunnel Status online
    4
    Version 3.x.x
    5
    Region us
    6
    Web Interface http://127.0.0.1:4040
    7
    Forwarding http://<random_subdomain>.ngrok.io -> http://localhost:4567
    8
    Forwarding https://<random_subdomain>.ngrok.io -> http://localhost:4567
    9
    Connections ttl opn rt1 rt5 p50 p90
    10
    0 0 0.00 0.00 0.00 0.00
  3. A successful ngrok configuration results in your Twilio Voice app returning the following XML at your ngrok URL:

    <?xml version="1.0" encoding="UTF-8"?><Response><Say>Hello World</Say></Response>

Learn more about Twilio and Sinatra with the following resources: