Skip to contentSkip to navigationSkip to topbar
On this page

SMS developer quickstart


In this quickstart, you'll build your first application to programmatically send and receive text messages with Twilio Programmable Messaging. This quickstart uses the Programmable Messaging REST API, the Twilio helper libraries, and the Twilio Virtual Phone.

For a no-code quickstart, see No-code programmable messaging quickstart with Twilio Studio.


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 and set up ngrok(link takes you to an external page).

  • Install Flask(link takes you to an external page) and Twilio's Python helper library(link takes you to an external page). To install using pip(link takes you to an external page), run:

    pip install flask twilio

Sign up for Twilio and get a phone number

sign-up-for-twilio-and-get-a-phone-number page anchor
  1. Sign up for Twilio(link takes you to an external page). When prompted to select a plan, click Continue with trial.
  2. On the landing page:
    1. Click Get phone number to get a phone number.
    2. Copy your Account SID and Auth Token and paste them in a temporary local file for use later in this quickstart.

Open the Twilio Virtual Phone

open-the-twilio-virtual-phone page anchor
  1. Open the Send an SMS page in the Twilio Console(link takes you to an external page).
  2. On the Send to Virtual Phone tab, select the number that Twilio gave you from the Phone number list.
  3. Click Virtual Phone. Messages you send with your application display on the Virtual Phone.

Send an outbound SMS message

send-an-outbound-sms-message page anchor

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_sms.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)
  2. In the send_sms.py file, replace the values for account_sid and auth_token with your Account SID and Auth Token surrounded by quotation marks.

    (error)

    Danger

    This quickstart hardcodes your credentials to get you started quickly. Use environment variables and API keys to keep credentials secret and control access when you deploy to production.

  3. Replace the value for from with the phone number that Twilio gave you in E.164 format.

  4. Replace the value for to with the Twilio Virtual Phone number (+18777804236).

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

    python send_sms.py

    After a few moments, you receive an SMS from your Twilio number on the Twilio Virtual Phone.


Receive and reply to an inbound SMS message

receive-and-reply-to-an-inbound-sms-message page anchor

Follow these steps to reply to an SMS message sent to your Twilio phone number.

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

    1
    from flask import Flask, request, Response
    2
    from twilio.twiml.messaging_response import MessagingResponse
    3
    4
    app = Flask(__name__)
    5
    6
    @app.route("/reply_sms", methods=['POST'])
    7
    def reply_sms():
    8
    # Create a new Twilio MessagingResponse
    9
    resp = MessagingResponse()
    10
    resp.message("The Robots are coming! Head for the hills!")
    11
    12
    # Return the TwiML (as XML) response
    13
    return Response(str(resp), mimetype='text/xml')
    14
    15
    if __name__ == "__main__":
    16
    app.run(port=3000)

    Save the file.

  2. In a new terminal window, run the following command to start the Python development server on port 3000:

    python reply_sms.py
  3. In a new terminal window, run the following command to start ngrok(link takes you to an external page) and create a tunnel to your localhost:

    ngrok http 3000
    (warning)

    Warning

    Use ngrok only for testing because it creates a temporary URL that exposes your local development machine to the internet. Host your application with a cloud provider or your public server when you deploy to production.

  4. Set up a webhook that triggers when your Twilio phone number receives an SMS message:

    1. Open the Active Numbers page in the Twilio Console(link takes you to an external page).

    2. Click your Twilio phone number.

    3. In the Messaging Configuration section, in the URL field for A message comes in, enter the temporary forwarding URL from your ngrok console with /reply_sms appended to the end.

      For example, if your ngrok console shows Forwarding https://1aaa-123-45-678-910.ngrok-free.app, enter https://1aaa-123-45-678-910.ngrok-free.app/reply_sms.

    4. Click Save configuration.

  5. With the Python development server and ngrok running, send an SMS to your Twilio phone number:

    1. Enter a message in the Click here to reply field at the bottom of the Twilio Virtual Phone.
    2. Click the send icon.

    An HTTP request shows in your ngrok console, and you get the response back as an SMS on the Twilio Virtual Phone.