SMS and Voice Services via Twilio

You can use Twilio to embed voice, VoIP, and messaging into applications. The Twilio platform consists of the Twilio Markup Language (TwiML), a RESTful API, and VoIP SDKs for web browsers, Android, and iOS. Helper libraries are available in multiple languages.

TwiML tells Twilio what to do when you receive an incoming call or SMS. When someone makes a call or sends an SMS to one of your Twilio numbers, Twilio looks up the URL associated with that phone number, makes a request, and then reads TwiML instructions at that URL to determine what to do, such as record a call, or send an SMS.

You can use the Twilio REST API to query metadata about your account, phone numbers, calls, text messages, and recordings. You can also initiate outbound calls and send text messages.


Google App Engine customers receive complimentary credit when you upgrade. Find more details at Twilio Pricing.

Before you begin

    Select or create a Cloud Platform Console project.

    Go to the Projects page

Setting Up

To use Twilio to receive an incoming call, send an SMS, or receive an SMS:

  1. Create a Twilio account.

  2. In Twilio, create a number. Configure the URLs for your Twilio number as needed for your app. For example, for the sample code:

    • Configure the voice request URL to be https://[your-app-id]
    • Configure the SMS request URL to be https://[your-app-id]
  3. Configure your Twilio settings in the environment variables section in appengine-web.xml. For example:

    <appengine-web-app xmlns="">
        <env-var name="TWILIO_ACCOUNT_SID" value="YOUR-TWILIO-ACCOUNT-SID" />
        <env-var name="TWILIO_AUTH_TOKEN" value="YOUR-TWILIO-AUTH-TOKEN" />
        <env-var name="TWILIO_NUMBER" value="YOUR-TWILIO-NUMBER" />

  4. Add Twilio's Java library to your application. For example, if you use Maven, you can add the library to the project's pom.xml:


    All dependencies that are declared in pom.xml will be installed when you deploy your application.

  5. To prepare for local testing of your app, set the environment variables on your local machine using the command line. For example, for the sample code:

    export TWILIO_ACCOUNT_SID=[your-twilio-account-sid]
    export TWILIO_AUTH_TOKEN=[your-twilio-auth-token]
    export TWILIO_NUMBER=[your-twilio-number]
  6. You can run the application locally to test the callbacks and SMS sending. For example, using Maven:

    mvn clean jetty:run

Copying the sample code

A complete code sample showing integration with Twilio is available at Github. If you want to use this sample code, copy it to your local machine:

git clone
cd java-docs-samples/appengine/twilio

Receiving an Incoming Call

To create your first application with Twilio:

  1. Set up your application to respond to an incoming call and return TwiML instructions.

    In this example, TwiML tells Twilio to convert the specified text to speech using the Say verb:

    public class ReceiveCallServlet extends HttpServlet {
      public void service(HttpServletRequest req, HttpServletResponse resp) throws IOException,
          ServletException {
        TwiMLResponse twiml = new TwiMLResponse();
        Say say = new Say("Hello from Twilio!");
        try {
        } catch (TwiMLException e) {
          throw new ServletException("Twilio error", e);

  2. Deploy your project to App Engine.

  3. Send a HTTP POST request to, which returns the following text:

    <?xml version="1.0" encoding="UTF-8"?>
        <Say>Hello from Twilio!</Say>
  4. Copy and paste the URL into the Voice URL box on the Numbers page of your Twilio Account.

  5. Now call your Twilio number! You should hear a voice say "Hello from Twilio!" in response. When you call, Twilio will fetch your URL, and execute the XML instructions listed above. Then Twilio will hang up, because there are no more instructions.

Sending an SMS

To send an SMS using Twilio, you can use the Twilio Java helper library and Java classes included in the JAR file:

  1. Set up your application to send an SMS. For example, the sends an SMS greeting:

    public class SendSmsServlet extends HttpServlet {
      public void service(HttpServletRequest req, HttpServletResponse resp) throws IOException,
          ServletException {
        final String twilioAccountSid = System.getenv("TWILIO_ACCOUNT_SID");
        final String twilioAuthToken = System.getenv("TWILIO_AUTH_TOKEN");
        final String twilioNumber = System.getenv("TWILIO_NUMBER");
        final String toNumber = (String) req.getParameter("to");
        if (toNumber == null) {
              .print("Please provide the number to message in the \"to\" query string parameter.");
        TwilioRestClient client = new TwilioRestClient(twilioAccountSid, twilioAuthToken);
        Account account = client.getAccount();
        MessageFactory messageFactory = account.getMessageFactory();
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("To", toNumber));
        params.add(new BasicNameValuePair("From", twilioNumber));
        params.add(new BasicNameValuePair("Body", "Hello from Twilio!"));
        try {
          Message sms = messageFactory.create(params);
        } catch (TwilioRestException e) {
          throw new ServletException("Twilio error", e);

  2. Change the to parameter to use a real phone number. The to number can be any outgoing number, such as your mobile phone number.

  3. Deploy your updated code and trigger an SMS message by loading the follow request path for your app:

    http://<your app>

Receiving an SMS

To receive an incoming SMS using Twilio, use the Twilio Java helper library.

  1. Set up your application to respond to an incoming SMS and return TwiML instructions. For example:

    public class ReceiveSmsServlet extends HttpServlet {
      public void service(HttpServletRequest request, HttpServletResponse response) throws IOException,
          ServletException {
        String fromNumber = request.getParameter("From");
        String body = request.getParameter("Body");
        String message = String.format("Hello, %s, you said %s", fromNumber, body);
        TwiMLResponse twiml = new TwiMLResponse();
        Message sms = new Message(message);
        try {
        } catch (TwiMLException e) {
          throw new ServletException("Twilio error", e);

  2. Deploy your updated code to App Engine.

  3. Send an SMS to your Twilio number. You should receive