Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

212 lines (179 sloc) 12.532 kB

How to Make a Phone Call Using Twilio in a Java Application on Azure

The following example shows you how you can use Twilio to make a call from a web page hosted in Azure. The resulting application will prompt the user for phone call values, as shown in the following screen shot.

Azure Call Form Using Twilio and Java

You'll need to do the following to use the code in this topic:

  1. Acquire a Twilio account and authentication token. To get started with Twilio, evaluate pricing at You can sign up at For information about the API provided by Twilio, see
  2. Obtain the Twilio JAR. At, you can download the GitHub sources and create your own JAR, or download a pre-built JAR (with or without dependencies). The code in this topic was written using the pre-built TwilioJava-3.3.8-with-dependencies JAR.
  3. Add the JAR to your Java build path.
  4. If you are using Eclipse to create this Java application, include the Twilio JAR in your application deployment file (WAR) using Eclipse's deployment assembly feature. If you are not using Eclipse to create this Java application, ensure the Twilio JAR is included within the same Azure role as your Java application, and added to the class path of your application.
  5. Ensure your cacerts keystore contains the Equifax Secure Certificate Authority certificate with MD5 fingerprint 67:CB:9D:C0:13:24:8A:82:9B:B2:17:1E:D1:1B:EC:D4 (the serial number is 35:DE:F4:CF and the SHA1 fingerprint is D2:32:09:AD:23:D3:14:23:21:74:E4:0D:7F:9D:62:13:97:86:63:3A). This is the certificate authority (CA) certificate for the service, which is called when you use Twilio APIs. For information about adding this CA certificate to your JDK's cacert store, see Adding a Certificate to the Java CA Certificate Store.

Additionally, familiarity with the information at Creating a Hello World Application Using the Azure Plugin for Eclipse with Java (by Microsoft Open Technologies), or with other techniques for hosting Java applications in Azure if you are not using Eclipse, is highly recommended.

Create a web form for making a call

The following code shows how to create a web form to retrieve user data for making a call. For purposes of this example, a new dynamic web project, named TwilioCloud, was created, and callform.jsp was added as a JSP file.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Automated call form</title>
 <p>Fill in all fields and click <b>Make this call</b>.</p>
  <form action="makecall.jsp" method="post">
       <td><input type="text" size=50 name="callTo" value="" />
       <td><input type="text" size=50 name="callFrom" value="" />
       <td>Call message:</td>
       <td><input type="text" size=400 name="callText" value="Hello. This is the call text. Good bye." />
       <td colspan=2><input type="submit" value="Make this call" />

Create the code to make the call

The following code, which is called when the user completes the form displayed by callform.jsp, creates the call message and generates the call. For purposes of this example, the JSP file is named makecall.jsp and was added to the TwilioCloud project. (Use your Twilio account and authentication token instead of the placeholder values assigned to accountSID and authToken in the code below.)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Call processing happens here</title>
    <b>This is my make call page.</b><p/>
     // Use your account SID and authentication token instead
     // of the placeholders shown here.
     String accountSID = "your_twilio_account";
     String authToken = "your_twilio_authentication_token";

     // Instantiate an instance of the Twilio client.     
     TwilioRestClient client;
     client = new TwilioRestClient(accountSID, authToken);

     // Retrieve the account, used later to retrieve the CallFactory.
     Account account = client.getAccount();

     // Display the client endpoint. 
     out.println("<p>Using Twilio endpoint " + client.getEndpoint() + ".</p>");

     // Display the API version.
     String APIVERSION = TwilioRestClient.DEFAULT_VERSION;
     out.println("<p>Twilio client API version is " + APIVERSION + ".</p>");

     // Retrieve the values entered by the user.
     String callTo = request.getParameter("callTo");  
     // The Outgoing Caller ID, used for the From parameter,
     // must have previously been verified with Twilio.
     String callFrom = request.getParameter("callFrom");
     String userText = request.getParameter("callText");

     // Replace spaces in the user's text with '%20', 
     // to make the text suitable for a URL.
     userText = userText.replace(" ", "%20");

     // Create a URL using the Twilio message and the user-entered text.
     String Url="";
     Url = Url + "?Message%5B0%5D=" + userText;

     // Display the message URL.
     out.println("The URL is " + Url);

     // Place the call From, To and URL values into a hash map. 
     HashMap<String, String> params = new HashMap<String, String>();
     params.put("From", callFrom);
     params.put("To", callTo);
     params.put("Url", Url);

     CallFactory callFactory = account.getCallFactory();
     Call call = callFactory.create(params);
     out.println("<p>Call status: " + call.getStatus()  + "</p>"); 
catch (TwilioRestException e) 
    out.println("<p>TwilioRestException encountered: " + e.getMessage() + "</p>");
    out.println("<p>StackTrace: " + e.getStackTrace().toString() + "</p>");
catch (Exception e) 
    out.println("<p>Exception encountered: " + e.getMessage() + "");
    out.println("<p>StackTrace: " + e.getStackTrace().toString() + "</p>");

In addition to making the call, makecall.jsp displays the Twilio endpoint, API version, and the call status. An example is the following screen shot:

Azure Call Response Using Twilio and Java

Run the application

Following are the high-level steps to run your application; details for these steps can be found at Creating a Hello World Application Using the Azure Plugin for Eclipse with Java (by Microsoft Open Technologies).

  1. Export your TwilioCloud WAR to the Azure approot folder.
  2. Modify startup.cmd to unzip your TwilioCloud WAR.
  3. Compile your application for the compute emulator.
  4. Start your deployment in the compute emulator.
  5. Open a browser, and run http://localhost:8080/TwilioCloud/callform.jsp.
  6. Enter values in the form, click Make this call, and then see the results in makecall.jsp.

When you are ready to deploy to Azure, recompile for deployment to the cloud, deploy to Azure, and run in the browser (substitute your value for your_hosted_name).

Next steps

This code was provided to show you basic functionality using Twilio in Java on Azure. Before deploying to Azure in production, you may want to add more error handling or other features. For example:

For additional information about Twilio, see

See Also

Jump to Line
Something went wrong with that request. Please try again.