In this tutorial, we'll show you how to use Twilio's Programmable Messaging to send SMS and MMS messages from your Python application.
While you can send text-only SMS messages almost anywhere on the planet, sending media is currently only available in the US and Canada. Learn more in this support article.
The code samples in this tutorial use Twilio's Python helper library. Let's get started!
If you have a Twilio account and Twilio phone number with SMS capabilities, you're all set! Feel free to jump straight to the code.
If you are sending SMS to the U.S. or Canada, before proceeding further please be aware of updated restrictions on the use of Toll-Free numbers for messaging, including TF numbers obtained through Free Trial. Please click here for details.
Before you can receive phone calls and send messages, you'll need to sign up for a Twilio account and purchase a Twilio phone number.
If you're brand new to Twilio, you can sign up for a free trial account to get started. Once you've signed up, head over to your Console and grab your Account SID and your Auth Token. You will need those values for the code samples below.
Sending messages requires a Twilio phone number with SMS capabilities. If you don't currently own a Twilio phone number with SMS capabilities, you'll need to buy one. After navigating to the Buy a Number page, check the SMS box and click Search:
If you live in the US or Canada and also wish to send MMS messages, you can select the MMS box. When viewing the search results, you can see the capability icons in the list of available numbers:
Find a number you like and click Buy to add it to your account.
If you're using a trial account, you will need to verify your personal phone number via the console so that you can test sending SMSes to yourself.
Learn more about how to work with your free trial account.
Now that you have a Twilio phone number you can start sending messages to mobile devices.
To send an outgoing SMS message from your Twilio account you'll need to make an HTTP POST
to Twilio's Message resource.
Twilio's Python library helps you to create a new instance of the Message resource, specifying the To, From, and Body parameters of your message.
If you don't already have the Python helper library installed you can install it using pip:
pip install twilio
If you're not using pip you can find manual installation instructions here.
Now, create a file named send_sms.py
and include the following code:
1# Download the helper library from https://www.twilio.com/docs/python/install2import os3from twilio.rest import Client45# Find your Account SID and Auth Token at twilio.com/console6# and set the environment variables. See http://twil.io/secure7account_sid = os.environ["TWILIO_ACCOUNT_SID"]8auth_token = os.environ["TWILIO_AUTH_TOKEN"]9client = Client(account_sid, auth_token)1011message = client.messages.create(12body="This is the ship that made the Kessel Run in fourteen parsecs?",13from_="+15017122661",14to="+15558675310",15)1617print(message.body)
1{2"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",3"api_version": "2010-04-01",4"body": "This is the ship that made the Kessel Run in fourteen parsecs?",5"date_created": "Thu, 30 Jul 2015 20:12:31 +0000",6"date_sent": "Thu, 30 Jul 2015 20:12:33 +0000",7"date_updated": "Thu, 30 Jul 2015 20:12:33 +0000",8"direction": "outbound-api",9"error_code": null,10"error_message": null,11"from": "+15017122661",12"messaging_service_sid": "MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",13"num_media": "0",14"num_segments": "1",15"price": null,16"price_unit": null,17"sid": "SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",18"status": "queued",19"subresource_uris": {20"media": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Media.json"21},22"tags": null,23"to": "+15558675310",24"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json"25}
Replace the placeholder values for account_sid
and auth_token
with your unique values. You can find these in your Twilio console.
Please note: it's okay to hardcode your credentials when getting started, but you should use environment variables to keep them secret before deploying to production. Check out how to set environment variables for more information.
You'll tell Twilio which phone number to use to send this message by replacing the from_
number with the Twilio phone number you purchased earlier.
Next, specify yourself as the message recipient by replacing the to
number with your mobile phone number. Both of these parameters must use E.164 formatting (+
and a country code, e.g., +16175551212
)
We also include the body
parameter, which contains the content of the SMS we're going to send.
Once you've updated the code sample, you can test it out by running it from the command line:
python send_sms.py
In just a few moments you should receive an SMS!
If you're using a trial account, you'll notice that any messages you send will always begin with "Sent from a Twilio trial account." Once you upgrade your account, you will no longer see this message. Learn more about sending SMS and MMS messages from a trial account.
Let's take a moment to understand what's going on behind the scenes when you send this request to Twilio.
When Twilio receives your request to send an SMS via the REST API, it will check that you've included a valid Twilio phone number in the from_
field. Twilio will then either queue the SMS or return this HTTP error in its response to your request.
Assuming your request didn't result in any errors, Twilio's HTTP response will include the SID of the new message. This unique identifier will help us reference this message later: in the code above, we printed that SID to the terminal.
Twilio's JSON response includes a robust amount of data about your message. A sample response might look like this:
1{2"sid": "SMxxxxxxxxxxxxxxx",3"date_created": "Thu, 09 Aug 2018 17:26:08 +0000",4"date_updated": "Thu, 09 Aug 2018 17:26:08 +0000",5"date_sent": null,6"account_sid": "ACxxxxxxxxxxxxxxxx",7"to": "+15558675310",8"from": "+15017122661",9"messaging_service_sid": "MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",10"body": "This is the ship that made the Kessel Run in fourteen parsecs?",11"status": "queued",12"num_segments": "1",13"num_media": "0",14"direction": "outbound-api",15"api_version": "2010-04-01",16"price": null,17"price_unit": "USD",18"error_code": null,19"error_message": null,20"uri": "/2010-04-01/Accounts/ACxxxxxxxxx/Messages/SMxxxxxxxxxxxx.json",21"subresource_uris": {22"media": null23}24}25
You can access any of these attributes from your Python code, much like we did when we printed the message.sid
.
Try adding a print statement like print(message.status)
. Save the file, then run the code with python send_sms.py
one more time. You should see the status of your message, "queued
", printed to your terminal.
If you receive an error in response from Twilio or never receive the message, you may want to check out these tips for troubleshooting undelivered messages.
If you'd like to track the status of your messages in real-time, you'll need to set up a StatusCallback URL. Learn more in our guide on tracking outbound message delivery.
If you want to send a message to several recipients, you could create an array of recipients and iterate through each phone number in the array:
1numbers_to_message = ['+15558675310', '+14158141829', '+15017122661']2for number in numbers_to_message:3client.messages.create(4body='Hello from my Twilio number!',5from_='+15017122662',6to=number7)
This will create a new Message instance for each phone number in the list.
As you send more messages via the API, Twilio will queue them up for delivery at your prescribed rate limit. API requests for messages that exceed the specified rates will be queued and executed as capacity is available.
If your application tries to enqueue more than 4 hours worth of outbound traffic (e.g., enqueuing more than 14,400 messages to Canada over one long code phone number), the API will start returning 429
errors.
If you need to enqueue a large volume of messages, you may find that it's helpful to leverage Twilio's Messaging Services. See our guide on how to set up and send messages from a messaging service in your language of choice for more tips.
While you can send text-only SMS messages almost anywhere on the planet, sending media is currently only available in the US and Canada.
To include media in your Twilio-powered text message, you need to make an addition to the code we wrote above. This time, we need to add the media_url
parameter.
Create a file called send_mms.py
and include the following code:
1# Download the helper library from https://www.twilio.com/docs/python/install2import os3from twilio.rest import Client45# Find your Account SID and Auth Token at twilio.com/console6# and set the environment variables. See http://twil.io/secure7account_sid = os.environ["TWILIO_ACCOUNT_SID"]8auth_token = os.environ["TWILIO_AUTH_TOKEN"]9client = Client(account_sid, auth_token)1011message = client.messages.create(12body="This is the ship that made the Kessel Run in fourteen parsecs?",13from_="+15017122661",14media_url=[15"https://c1.staticflickr.com/3/2899/14341091933_1e92e62d12_b.jpg"16],17to="+15558675310",18)1920print(message.body)
1{2"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",3"api_version": "2010-04-01",4"body": "This is the ship that made the Kessel Run in fourteen parsecs?",5"date_created": "Thu, 24 Aug 2023 05:01:45 +0000",6"date_sent": "Thu, 24 Aug 2023 05:01:45 +0000",7"date_updated": "Thu, 24 Aug 2023 05:01:45 +0000",8"direction": "outbound-api",9"error_code": null,10"error_message": null,11"from": "+15017122661",12"num_media": "0",13"num_segments": "1",14"price": null,15"price_unit": null,16"messaging_service_sid": "MGaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",17"sid": "SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",18"status": "queued",19"subresource_uris": {20"media": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Media.json"21},22"tags": {23"campaign_name": "Spring Sale 2022",24"message_type": "cart_abandoned"25},26"to": "+15558675310",27"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages/SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json"28}
Again, update the from_
and to
parameters to use your Twilio phone number and your mobile phone.
The new media_url
parameter in this code tells Twilio where to go to get the media we want to include. This must be a publicly accessible URL: Twilio will not be able to reach any URLs that are hidden or that require authentication.
Just as when you send an SMS, Twilio will send data about the message in its response to your request. The JSON response will contain the unique SID and URI for your media resource:
"subresource_uris": {"media": "/2010-04 01/Accounts/ACxxxxxxxx/Messages/SMxxxxxxxxxxxxx/Media.json"}
When the Twilio REST API creates your new Message resource, it will save the image found at the specified media_url
as a Media resource. Once created, you can access this resource at any time via the API.
You can print this value from your Python code to see where the image is stored. Add the following line to the end of your send_mms.py
file to see your newly provisioned Media URI:
print(message.media._uri)
Save the file and run it from the command line:
python send_mms.py
In just a moment you should receive a text message with an image!
You've now successfully sent some messages with the Twilio Programmable Messaging API and the Python helper library.
Check out these in-depth resources to take your programmatic messaging a step further: