Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)



SMS and Voice Services via Twilio

Python | Java | PHP | Go
Note: Twilio is a third-party company whose services are not covered by the Google App Engine Service Level Agreement .

Twilio is powering the future of business communications, enabling developers to embed voice, VoIP, and messaging into applications. They virtualize all infrastructure needed in a cloud-based, global environment, exposing it through the Twilio communications API platform. Applications are simple to build and scalable. Enjoy flexibility with pay-as-you go pricing, and benefit from cloud reliability.

Twilio Voice enables your application to make and receive phone calls. Twilio SMS enables your application to send and receive text messages. Twilio Client allows you to make VoIP calls from any phone, tablet, or browser and supports WebRTC.

Pricing

Google App Engine customers receive complimentary credit for 2,000 SMS messages or inbound minutes when you upgrade. Redeem this Twilio credit and get started here .

Twilio is a pay-as-you-go service. There are no set-up fees and you can close your account at any time. You can find more details at Twilio Pricing .

Platform

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. You can find the full list at Twilio Helper Libraries .

TwiML

TwiML is a set of instructions you can use to tell 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 will look up the URL associated with that phone number and make a request to that URL. Twilio will read TwiML instructions at that URL to determine what to do:

  • <Say> - text to speech
  • <Record> - record the call
  • <Play> - play a message for the caller
  • <Gather> - prompt the caller to press digits on their keypad
  • <Sms> - send an SMS

Learn about the other verbs and capabilities from the Twilio Markup Language documentation .

REST API

The Twilio REST API allows you to query metadata about your account, phone numbers, calls, text messages, and recordings. You can also do some fancy things like initiate outbound calls and send text messages.

Since the API is based on REST principles, it's very easy to write and test applications. You can use your browser to access URLs, and you can use pretty much any HTTP client in any programming language to interact with the API. Learn more about the Twilio REST API .

Setting Up

We will be using the standard Google App Engine Python Runtime Environment to construct this example. If this is your first time writing Python for Google App Engine, we recommend that you use the Getting Started guide for Python . Follow the tutorial until you have completed the "Hello, World!" guide.

After you have a working "Hello, World!" application, you will need to add Twilio's Python library to your application. Do this by doing the following:

  • cd to the directory where your application is stored, this will be the helloworld directory you created if you followed the Getting Started guide.
  • Install virtualenv :

    `$ sudo easy_install pip`
    
    `$ sudo pip install virtualenv`
    
  • Set up a virtual python environment:

    `$ virtualenv --distribute venv`
    
  • Activate virtualenv:

    `$ source venv/bin/activate`
    
  • Use pip to install Twilio's Python library and dependencies:

    `$ pip install twilio`
    
  • Deactivate your virtual environment:

    `$ deactivate`
    
  • Link the Twilio library and its dependencies into your project:

    `$ ln -s venv/lib/python2.7/site-packages/twilio .`
    
    `$ ln -s venv/lib/python2.7/site-packages/httplib2 .`
    
    `$ ln -s venv/lib/python2.7/site-packages/six.py .`
    

You have now installed the Twilio library into your Google App Engine project.

Receiving an Incoming Call

Let's walk through creating your first application, titled "Hello Monkey."

After completing the instructions in Setting Up , modify helloworld.py to look like this .

After updating helloworld.py , follow these directions to deploy your project to App Engine .

After you've deployed your project to App Engine you will be able to send a POST request to http://<your app>.appspot.com/twiml , which will return the following text:

<?xml version="1.0" encoding="UTF-8"?>
  <Response>
  <Say>Hello Monkey!</Say>
</Response>

Next, copy and paste the http://<your app>.appspot.com/twiml URL into the "Voice" URL box on the Numbers page of your Twilio Account.

Now call your Twilio number! You should hear a voice say "Hello Monkey!" 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

The twilio-python helper library makes it easy to send an outgoing SMS using Twilio.

To do this, modify helloworld.py thusly, filling in your "Account SID" and "Auth Token" which are found here: https://www.twilio.com/user/account

You will also need to change the from_ and to parameters to use real phone numbers. The from_ number must be a valid Twilio phone number in your account . For this example, use the phone number you called in the example above. The to number can be any outgoing number, your cell phone for example.

After deploying the updated code, send the SMS by putting this URL into a web browser: http://<your app>.appspot.com/send_sms

Learning More about Twilio

Now that you've learned some of the basics, learn more about additional features and some best practices for building secure and scalable applications:

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.