How to make a phone call using Twilio in a web role on Windows Azure
This guide demonstrates how to use Twilio to make a call from a web page hosted in Windows Azure. The resulting application prompts the user for phone call values, as shown in the following screenshot.
Table of Contents
You will need to do the following to use the code in this topic:
- Acquire a Twilio account and authentication token. To get started with Twilio, sign up at https://www.twilio.com/try-twilio. You can evaluate pricing at http://www.twilio.com/pricing. For information about the API provided by Twilio, see http://www.twilio.com/voice/api.
- Verify your phone number with Twilio. For information on how to verify your phone number, see https://www.twilio.com/user/account/phone-numbers/verified#. As an alternative to using an existing number, you can purchase a Twilio phone number.
For the purposes of this example you will use the Twilio sandbox phone number to send a message to the verified phone number. You can only use the sandbox phone number to send to verified phone numbers.
- Add the Twilio .NET libary to your web role. See "To add the Twilio libraries to your web role project," later in this topic.
You should be familiar with creating a basic web role on Windows Azure.
How to: Create a web form for making a call
To add the Twilio libraries to your web role project:
- Open your solution in Visual Studio.
- Right-click References.
- Click Manage NuGet Packages.
- Click Online.
- In the search online box, type twilio.
- Click Install on the Twilio package.
The following code shows how to create a web form to retrieve user data for making a call. In this example, an ASP.NET web role named TwilioCloud is created.
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master"
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:BulletedList ID="varDisplay" runat="server" BulletStyle="NotSet">
<p>Fill in all fields and click <b>Make this call</b>.</p>
To:<br /><asp:TextBox ID="toNumber" runat="server" /><br /><br />
Message:<br /><asp:TextBox ID="message" runat="server" /><br /><br />
<asp:Button ID="callpage" runat="server" Text="Make this call"
How to: Create the code to make the call
The following code, which is called when the user completes the form, creates the call message and generates the call. In this example, the code is run in the onclick event handler of the button on the form. (Use your Twilio account and authentication token instead of the placeholder values assigned to accountSID and authToken in the code below.)
public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
protected void callpage_Click(object sender, EventArgs e)
// Call porcessing happens here.
// Use your account SID and authentication token instead of
// the placeholders shown here.
string accountSID = "ACNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN";
string authToken = "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN";
// Instantiate an instance of the Twilio client.
client = new TwilioRestClient(accountSID, authToken);
// Retrieve the account, used later to retrieve the
Twilio.Account account = client.GetAccount();
string APIversuion = client.ApiVersion;
string TwilioBaseURL = client.BaseUrl;
if (this.toNumber.Text == "" || this.message.Text == "")
"You must enter a phone number and a message.");
// Retrieve the values entered by the user.
string to = this.toNumber.Text;
string myMessage = this.message.Text;
// Create a URL using the Twilio message and the user-entered
// text. You must replace spaces in the user's text with '%20'
// to make the text suitable for a URL.
String Url = "http://twimlets.com/message?Message%5B0%5D="
+ myMessage.Replace(" ", "%20");
// Diplay the enpoint, API version, and the URL for the message.
this.varDisplay.Items.Add("Using Tilio endpoint "
this.varDisplay.Items.Add("Twilioclient API Version is "
this.varDisplay.Items.Add("The URL is " + Url);
// Instantiate the call options that are passed
// to the outbound call.
CallOptions options = new CallOptions();
// Set the call From, To, and URL values into a hash map.
// This sample uses the sandbox number provided by Twilio
// to make the call.
options.From = "+14155992671";
options.To = to;
options.Url = Url;
// Place the call.
var call = client.InitiateOutboundCall(options);
this.varDisplay.Items.Add("Call status: " + call.Status);
The call is made, and the Twilio endpoint, API version, and the call status are displayed. The following screenshot shows output from a sample run.
More information about TwiML can be found at http://www.twilio.com/docs/api/twiml. More information about <Say> and other Twilio verbs can be found at http://www.twilio.com/docs/api/twiml/say.
This code was provided to show you basic functionality using Twilio in an ASP.NET web role on Windows Azure. Before deploying to Windows Azure in production, you may want to add more error handling or other features. For example: