Skip to contentSkip to navigationSkip to topbar
On this page

Send SMS and MMS messages


In this tutorial, you'll learn to use Twilio Programmable Messaging(link takes you to an external page) to programmatically send SMS and Multimedia Messaging Service (MMS) messages using your web application. This tutorial makes HTTP POST requests to the Message resource(link takes you to an external page) and the Media resource(link takes you to an external page) in the Twilio REST API, using the Twilio helper libraries to simplify requests in your programming language.

To send messages without writing code, use Twilio Studio (our low-code application builder).


Complete the prerequisites

complete-the-prerequisites page anchor

Select your programming language and complete the prerequisites:

PythonNode.jsPHPC# (.NET Framework)C# (.NET Core)JavaGoRuby
  • Install Python(link takes you to an external page).
  • Install the Twilio Python module(link takes you to an external page). To install using pip(link takes you to an external page), run:
    pip install twilio
  1. Sign up for Twilio(link takes you to an external page).

  2. On the Twilio Console landing page(link takes you to an external page):

    1. To acquire a phone number, click Get phone number.
    2. Copy your Account SID and Auth Token and paste them in a temporary local file for use later in this tutorial.
  3. Complete any applicable verification or registration requirements.

    While you wait for verification or registration, you can test with the Twilio Virtual Phone(link takes you to an external page). To learn more, see the SMS developer quickstart.


Follow these steps to send an SMS message from your Twilio phone number.

PythonNode.jsPHPC# (.NET Framework)C# (.NET Core)JavaGoRuby
  1. Create and open a new file called send_message.py anywhere on your machine and paste in the following code:

    Send an SMS Using Twilio with PythonLink to code sample: Send an SMS Using Twilio with Python
    1
    # Download the helper library from https://www.twilio.com/docs/python/install
    2
    import os
    3
    from twilio.rest import Client
    4
    5
    # Find your Account SID and Auth Token at twilio.com/console
    6
    # and set the environment variables. See http://twil.io/secure
    7
    account_sid = os.environ["TWILIO_ACCOUNT_SID"]
    8
    auth_token = os.environ["TWILIO_AUTH_TOKEN"]
    9
    client = Client(account_sid, auth_token)
    10
    11
    message = client.messages.create(
    12
    body="Join Earth's mightiest heroes. Like Kevin Bacon.",
    13
    from_="+15017122661",
    14
    to="+15558675310",
    15
    )
    16
    17
    print(message.body)

    To learn all of the API response values that you can return with print(), see the response for Send an SMS Message in the API documentation. Precede the response value with message. (for example: print(message.status) returns the status value).

  2. Set the environment variables for your Account SID and Auth Token.

    (warning)

    Improve security with API keys

    To better control access, use API keys instead of the Account SID and Auth Token when you deploy to production. To learn more, see Why you should use API keys.

    On Mac or Linux:

    1. Run the following commands to add your credentials as environment variables in a twilio.env file and source them. Replace ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX with your Account SID and replace your_auth_token with your Auth Token.

      1
      echo "export TWILIO_ACCOUNT_SID='ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'" > twilio.env
      2
      echo "export TWILIO_AUTH_TOKEN='your_auth_token'" >> twilio.env
      3
      source ./twilio.env
    2. If you're committing code with git, run the following command to add the twilio.env file to .gitignore to avoid uploading your credentials in plain text:

      echo "twilio.env" >> .gitignore

    On Windows command line (cmd.exe), run the following commands. Replace ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX with your Account SID and replace your_auth_token with your Auth Token.

    1
    set TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    2
    set TWILIO_AUTH_TOKEN=your_auth_token

    To learn more, see Store your Twilio credentials safely.

  3. In the send_message.py file, replace the value for to with the recipient phone number in E.164 format.

    To send a message to multiple recipients:

    1. Add a statement below client to define the array of phone numbers. Replace the phone numbers with your recipients' phone numbers in E.164 format.

      numbers = ["+15558675310", "+12345678901", "+10987654321"]
    2. Replace the message = and print() statements with the following code that iterates through each phone number in the numbers list and returns the message sent to each number. Twilio makes one API call for each number.

      1
      for number in numbers:
      2
      message = client.messages.create(
      3
      body="This is the ship that made the Kessel Run in fourteen parsecs?",
      4
      from_="+15017122661",
      5
      to=number,
      6
      )
      7
      print(f"Sent to {number}: {message.body}")
    (information)

    Limits on message cadence

    Message delivery performance to wireless carrier networks has limits. To learn more, see Understanding Twilio Rate Limits and Message Queues(link takes you to an external page).

  4. Replace the value for from with your Twilio phone number in E.164 format.

  5. Save your changes and run this command from your terminal in the directory that contains send_message.py:

    python send_message.py

    After a few moments, you receive an SMS from your Twilio number.


(information)

Info

To learn which countries support MMS messages, see Sending and receiving MMS messages(link takes you to an external page).

To send an MMS message, follow the steps to send an SMS message, adding the media URL to the code as shown in the following example. The media URL tells Twilio where to get the media you want to include.

The media URL must be a publicly accessible URL. Twilio can't reach any hidden URLs or URLs that require authentication.

When the Twilio REST API creates your new Message resource, it saves the image found at the specified in the media URL as a Media resource(link takes you to an external page). Once you create a resource, you can access it at any time by using the API.

PythonNode.jsPHPC# (.NET Framework)C# (.NET Core)JavaGoRuby

Set the media_url parameter:

Send a Message with an Image URL using Twilio with PythonLink to code sample: Send a Message with an Image URL using Twilio with Python
1
# Download the helper library from https://www.twilio.com/docs/python/install
2
import os
3
from twilio.rest import Client
4
5
# Find your Account SID and Auth Token at twilio.com/console
6
# and set the environment variables. See http://twil.io/secure
7
account_sid = os.environ["TWILIO_ACCOUNT_SID"]
8
auth_token = os.environ["TWILIO_AUTH_TOKEN"]
9
client = Client(account_sid, auth_token)
10
11
message = client.messages.create(
12
body="This is the ship that made the Kessel Run in fourteen parsecs?",
13
from_="+15017122661",
14
media_url=[
15
"https://c1.staticflickr.com/3/2899/14341091933_1e92e62d12_b.jpg"
16
],
17
to="+15558675310",
18
)
19
20
print(message.body)