Using Twilio with Codenvy

Below are various different projects talking to Twilio API that you can create with Codenvy Factories:

This document will explain the details of integrating the Twilio API into Codenvy. Twilio lets you to build voice and sms applications using its API that will enable you to receive incoming calls/smses and send outgoing calls/smses via Twilio API. The explanation has been done with 3 very basic Java servlet projects that makes use of the Twilio API through the Twilio Java client library to explain some basic features provided by the API.

Before proceeding to the creation of the Java projects, please register at with your email id and then enter a phone number in the next screen to get a verification code from Twilio. This phone number will be registered in your Twilio account:


In the next screen, you will need to enter the verification code and you will then be taken to your account dashboard:


Under the Getting Started section, you will get your Twilio number that can received incoming calls and send outgoing calls on your application’s behalf. After getting the number, you can configure the trial number to invoke your application’s URL using the desired HTTP method:


We will get to know more about this in our sample source codes.

On your account dashboard, you can also view your Account SID and Auth Token which would be used in your applications and these are demonstrated in our sample source codes.

The Twilio API has various methods to integrate with your application.

You can get more information about the API methods at This section has various sub sections explaining the usage of various API methods. In a nutshell, the process of incoming calls is achieved using Twilio Markup Language (TwiML) and the process of outgoing calls is achieved using REST API which then makes use of TwiML. We will explain them in our sample source codes.

The sample source code is to explain the process of receiving a simple incoming call from any phone number to your Twilio number and the your application responding to the call with a simple message.

Create a Project

Login to your Codenvy workspace and create a WAR project named as ‘TwilioSample’. This will have the following structure:


Create pom.xml File

To make use of the Twilio API inside a servlet using the Twilio Java helper library, please update the pom.xml to include the HttpClient and Java EE Web API jars and the servletapi jar and the twilio-java-sdk jar into your project as Maven dependencies:

<project xmlns="" xmlns:xsi=""
      <name>Maven Repository Switchboard</name>

Creater Java Class

Now create a Java servlet class named ‘’ that will send a TwiML response back to Twilio when the application is invoked via the incoming call. The TwiML response is being sent with the help of the Twilio API provided by the Twilio Java library. All this is done inside the method doGet() in the sample source Java class.

package com.codenvy;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.twilio.sdk.verbs.TwiMLResponse;
import com.twilio.sdk.verbs.TwiMLException;
import com.twilio.sdk.verbs.Say;

public class TwilioIncomingServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
      // Create a TwiML response and add our friendly message.
        TwiMLResponse twiml = new TwiMLResponse();
        Say say = new Say("Hello World, How are you doing today");
        try {
        } catch (TwiMLException e) {

Create index.jsp File

Your Codenvy project will have an index.jsp which will invoke the Servlet class -


Create web.xml File

The servlet mapping ‘/hello’ is defined in the web.xml as follows –

<display-name>Twilio Incoming Servlet App</display-name>


Build and Run Your Project

Now you build and run your Codenvy project and the TwiML response will be displayed in the browser:
This browser URL will now need to be configured in the Voice URL section of your Twilio number in your Twilio account and please also select the HTTP method type:

Now using any phone make a call to your Twilio number. On receiving the call, the Twilio will invoke your application URL configured and the application will then send the TwiML response displayed above to the Twilio and Twilio will then send out the greeting message to the caller. This way, you can make your application respond to an incoming call using the Twilio API.