Ruby Quickstart: Hello Monkey Client

Let's walk through making our first phone call from your browser to Twilio with Twilio Client.

This tutorial assumes you have a Ruby development environment with Sinatra and the twilio-ruby helper library. If you don't have these yet, please see our post on setting up your environment.

Hello Monkey Client

We're going to generate a secure Capability Token, that Twilio Client will use to authorize the connection between your browser and Twilio. Open a file called client-quickstart.rb and add the following lines:

require 'rubygems'
require 'sinatra'
require 'twilio-ruby'

get '/' do
    # Find these values at
    account_sid = 'ACxxxxxxxxxxxxxxxxxxxxx'
    auth_token = '456yyyyyyyyyyyyyyyyyyyyyy'
    # This application sid will play a Welcome Message.
    demo_app_sid = 'APabe7650f654fc34655fc81ae71caa3ff'
    capability =, auth_token)
    scope =
    token = capability.to_s
    erb :index, :locals => {:token => token}

Now create an ERB file that will be rendered when the URL is requested:

<!DOCTYPE html>
    <title>Hello Client Monkey 1</title>
    <script type="text/javascript"
    <script type="text/javascript"
    <link href="//"
      type="text/css" rel="stylesheet" />
    <script type="text/javascript">

    /* Create the Client with a Capability Token */
    Twilio.Device.setup("<%= token %>", {debug: true});

    /* Let us know when the client is ready. */
    Twilio.Device.ready(function (device) {

    /* Report any errors on the screen */
    Twilio.Device.error(function (error) {
        $("#log").text("Error: " + error.message);

    Twilio.Device.connect(function (conn) {
        $("#log").text("Successfully established call");

    /* Connect to Twilio when we call this function. */
    function call() {

    <button class="call" onclick="call();">

    <div id="log">Loading pigeons...</div>

Now start your server:

$ ruby client-quickstart.rb
== Sinatra has taken the stage ...
>> Listening on

Load http://localhost:4567 in your browser -- it will display a button that will initiate an audio connection into Twilio when clicked. The first few lines of code in client-quickstart.rb generate a secure Capability Token that Twilio Client uses to authorize the connection (using your account credentials).

We also registered two Twilio Client callbacks:

  1. ready - callback for when the client is successfully registered with Twilio.
  2. error - callback for when an error happens, such as a connection error.

You are now ready to make the call, so go ahead and click the 'Call' button! Did you hear the welcome message? Cool!

But hey, how do you close the connection and hangup the call? Keep reading...

Next: Hanging Up Calls »

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.