The Twilio Python Helper Library makes it easy to interact with the Twilio API from your Python application. The most recent version of the library can be found on PyPi. The Twilio Python Helper Library supports Python applications written in Python 3.6 and above. If you are interested in migrating to the newer 7.x version of the Twilio Python Helper Library from the 6.x version, check out this guide.
The easiest way to install the library 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:
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.
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 pip install twilio).
Try sending yourself an SMS message. Save the following code sample to your computer with a text editor. Be sure to update the
from_ phone number with values from your Twilio account. The
to phone number can be your own mobile phone.
Save the file as
send_sms.py. In the terminal,
cd to the directory containing the file you just saved then run:
You should receive the text message on your phone.
It's okay to hardcode your credentials when testing locally, but you should use environment variables to keep them secret before committing any code or deploying to production. Check out How to Set Environment Variables for more information.
from twilio.rest import Client account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" auth_token = "your_auth_token" client = Client(account_sid, auth_token)
from twilio.rest import Client account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" auth_token = "your_auth_token" client = Client(account_sid, auth_token) call = client.calls.create( to="+14155551212", from_="+15017250604", url="http://demo.twilio.com/docs/voice.xml" ) print(call.sid)
from twilio.rest import Client account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" auth_token = "your_auth_token" client = Client(account_sid, auth_token) call = client.calls.get("CA42ed11f93dc08b952027ffbc406d0868") print(call.to)
from twilio.rest import Client account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" auth_token = "your_auth_token" client = Client(account_sid, auth_token) for sms in client.messages.list(): print(sms.to)
The library automatically handles paging for you. Collections, such as
stream methods that page under the hood. With both
stream, you can specify the number of records you want to receive (
limit) and the maximum size you want each page fetch to be (
page_size). The library will then handle the task for you.
list eagerly fetches all records and returns them as a list, whereas
stream returns an iterator and lazily retrieves pages of records as you iterate over the collection. You can also page manually using the
For more information about these methods, view the auto-generated library docs.
Version 6.x of
twilio-python exports an exception class to aid with handling exceptions that are specific to Twilio methods.
To use it, import the
TwilioRestException and catch it in a given
from twilio.base.exceptions import TwilioRestException try: # This could potentially throw an exception! message = client.messages.create( to="+15558675309", from_="+15017250604", body="Hello there!") except TwilioRestException as e: # Implement your fallback code print(e)
Once you're up and running with the Python helper library, you'll find code samples using the latest version in our REST API docs and in the documentation for every Twilio product. You can also find auto-generated library docs for the latest SDK here.
New functionality will only be added to the new library (Python Helper Library 7.x). The old library (6.x) is no longer supported: Twilio will not provide bug fixes and Support might ask you to upgrade before debugging issues. Learn how to migrate your existing application.
The most recent version of the Python Helper Library is not API compatible with the previous 6.x version you may have used in previous Twilio applications. The older version will continue to work, and you will continue to find sample code for this version throughout our documentation. Should you need to install this version you can do so with the following command:
pip install twilio==6.63.2
We'd love to hear your feedback on the Twilio Python Helper Library and help you past any issues you may encounter. Feel free to drop us a line, and we'll make sure to get you sorted!