Thanks to visit codestin.com
Credit goes to www.twilio.com

Skip to contentSkip to navigationSkip to topbar
Page toolsOn 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 SDKs, and the Twilio Virtual Phone.

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


Complete the prerequisites

Codestin Search App

Select your programming language and complete the prerequisites:

PythonNode.jsPHPC# (.NET Framework)C# (.NET Core)JavaGoRuby
  • Install PythonCodestin Search App.

  • Install and set up ngrokCodestin Search App.

  • Install FlaskCodestin Search App and Twilio's Python SDKCodestin Search App. To install using pipCodestin Search App, run:

    pip install flask twilio

Sign up for Twilio and get a phone number

Codestin Search App
  1. Sign up for TwilioCodestin Search App. 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

Codestin Search App

The Twilio Virtual Phone lets you try Twilio quickly regardless of your country's regulations for messaging mobile handsets. To message a mobile handset, see Send SMS and MMS messages.

  1. Open the Send an SMS page in the Twilio ConsoleCodestin Search App.
  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

Codestin Search App

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 PythonCodestin Search App
    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)

    Don't include credentials in production apps

    This quickstart hardcodes your credentials for faster setup. To keep credentials secret and control access when you deploy to production, use environment variables and API keys.

  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

Codestin Search App

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 ngrokCodestin Search App 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 ConsoleCodestin Search App.

    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.