- Products
- Solutions
- API & Docs
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.
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.
If you'd like to learn more about how Twilio works, or for inspiration about what to build, try the Python quickstarts.
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.
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.
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.
Try sending yourself an SMS message. Save the following code in a plain text editor on your computer:
from twilio.rest import TwilioRestClient # Your Account Sid and Auth Token from twilio.com/user/account account_sid = "AC32a3c49700934481addd5ce1659f04d2" auth_token = "" client = TwilioRestClient(account_sid, auth_token) message = client.messages.create(body="Jenny please?! I love you <3", to="+14159352345", # Replace with your phone number from_="+14158141829") # Replace with your Twilio number print message.sid
In the terminal,
cd to the directory containing the file you just saved, then
run:
python send_sms.py
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.
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.
import twilio import twilio.rest try: client = twilio.rest.TwilioRestClient(account_sid, auth_token) message = client.messages.create( body="Hello World", to="+14159352345", from_="+14158141829" ) except twilio.TwilioRestException as e: print e
Still running into problems?