The Twilio Python Helper Library

The twilio-python helper library lets you write Python code to make HTTP requests to the Twilio API.

This library is open source, so if you find a feature missing or a bug, we encourage you to contribute back to the twilio-python project hosted on GitHub.

More Documentation

If you'd like to learn more about how Twilio works, or for inspiration about what to build, try the Python quickstarts.

The Rest API Documentation has examples written in Python for every kind of action you could take with the Twilio API. You should start there if you know what you want to do with the Python library.

You may also want to refer to the open source twilio-python specific documentation for more details about the functions and classes contained in the module.

Installation

The easiest way to install twilio-python is from PyPi using pip, a package manager for Python. Simply run this in the terminal:

pip install twilio

If you get a pip: command not found error, you can also use easy_install. Run this in your terminal:

easy_install twilio

Or, you can download the source code (ZIP) for twilio-python, and then install the library by running:

python setup.py install

in the folder containing the twilio-python library.

"Permission Denied"

If the command line gives you a big long error message that says Permission Denied in the middle of it, try running the above commands with sudo, ex. sudo pip install twilio.

Testing your installation

Try sending yourself an SMS message. Save the following code in a plain text editor on your computer:

from twilio.rest import TwilioRestClient

account_sid = "{{ account_sid }}" # Your Account SID from www.twilio.com/console
auth_token  = "{{ auth_token }}"  # Your Auth Token from www.twilio.com/console

client = TwilioRestClient(account_sid, auth_token)

message = client.messages.create(body="Hello from Python",
    to="+12345678901",    # Replace with your phone number
    from_="+12345678901") # Replace with your Twilio number

print(message.sid)

Save the file as send-sms.py. In the terminal, cd to the directory containing the file you just saved, then run:

python send_sms.py

Using the Module

Try the Python quickstarts and refer to the open source twilio-python specific documentation for details about the functions and classes contained in the library.

Handling Exceptions

If the Twilio API returns a 400 or a 500 level HTTP response, the twilio-python library will throw a TwilioRestException. 400-level errors are normal during API operation ("Invalid number", "Cannot deliver SMS to that number", for example) and should be handled appropriately.

from twilio import TwilioRestException
from twilio.rest import TwilioRestClient

account_sid = "{{ account_sid }}" # Your Account SID from www.twilio.com/console
auth_token  = "{{ auth_token }}"  # Your Auth Token from www.twilio.com/console

client = TwilioRestClient(account_sid, auth_token)

try:
    message = client.messages.create(body="Hello from Python",
        to="+12345678901",    # Replace with your phone number
        from_="+12345678901") # Replace with your Twilio number
except TwilioRestException as e:
    print(e)

Getting Help

Still running into problems?

Installing the Next-Gen Version

The "next-gen" version of the Twilio library will markedly improve the developer experience. As we put the finishing touches on this new way of building helper libraries, we need to hear your feedback. Please let us know what you think.

To install the next-gen version of twilio-python install the package as follows, specifying the 6.0-rc5 version:

pip install twilio-python==6.0rc5

Working with the helper library is a bit different than the current version. Here's an example of how to send an SMS:

from twilio.rest import Client

account_sid = "{{ account_sid }}" # Your Account SID from www.twilio.com/console
auth_token  = "{{ auth_token }}"  # Your Auth Token from www.twilio.com/console

client = Client(account_sid, auth_token)

message = client.messages.create(to="+12345678901", 
    from_="+12345678901",
    body="Hello from Python!")

print(message.sid)

Visit the next-gen branch on GitHub for more information and to provide feedback.