Skip to contentSkip to navigationSkip to topbar
Page toolsOn this page
Looking for more inspiration?Visit the

Server-side quickstart for Programmable Voice


This quickstart shows you how to build your first server-side application that makes and receives phone calls. For other voice quickstarts, such as no-code or SDK quickstarts, see Programmable Voice quickstarts.


Complete the prerequisites

complete-the-prerequisites page anchor

Complete the Twilio prerequisites

complete-the-twilio-prerequisites page anchor

Complete the language prerequisites

complete-the-language-prerequisites page anchor

Select your programming language and complete the prerequisites:

PythonNode.jsPHPC# or .NETJavaGoRuby
  • Install Python 3.3 or later(link takes you to an external page).
  • Install Flask(link takes you to an external page) and the Twilio Python Helper Library(link takes you to an external page). Using pip(link takes you to an external page), run the following command:
    pip install flask twilio
  • Install and set up ngrok(link takes you to an external page).

Get credentials and set as environment variables

get-credentials-and-set-as-environment-variables page anchor

Follow these steps to get your Twilio account credentials and set them as environment variables.

macOS TerminalWindows command linePowerShell
  1. Go to the Twilio Console(link takes you to an external page).
  2. Copy your Account SID and set it as an environment variable using the following command. Replace YOUR_ACCOUNT_SID with your actual Account SID.
    export TWILIO_ACCOUNT_SID=YOUR_ACCOUNT_SID
  3. Copy your Auth Token and set it as an environment variable using the following command. Replace YOUR_AUTH_TOKEN with your actual Auth Token.
    export TWILIO_AUTH_TOKEN=YOUR_AUTH_TOKEN

Make an outgoing phone call

make-an-outgoing-phone-call page anchor

To make a phone call from your Twilio number, follow these steps:

PythonNode.jsPHPC# or .NETJavaGoRuby
  1. Create a file named make_call.py and add the following code:
    Make a phone call using TwilioLink to code sample: Make a phone call using Twilio
    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
    call = client.calls.create(
    12
    url="http://demo.twilio.com/docs/voice.xml",
    13
    to="+18005550100",
    14
    from_="+18005550199",
    15
    )
    16
    17
    print(call.sid)
  2. Replace the from_ phone number with your Twilio number.
  3. Replace the to phone number with your own phone number.
  4. Save your changes.
  5. Run the script:
    python make_call.py
    Your phone rings and you hear the short message in the [TwiML][] document that links to your script.

Receive a phone call with your Twilio number

receive-a-phone-call-with-your-twilio-number page anchor

Follow these steps to receive a phone call with your Twilio number and respond using text-to-speech.

PythonNode.jsPHPC# or .NETJavaGoRuby
  1. Create a file named answer_phone.py and add the following code:

    1
    from flask import Flask
    2
    from twilio.twiml.voice_response import VoiceResponse
    3
    4
    app = Flask(__name__)
    5
    6
    7
    @app.route("/", methods=['GET', 'POST'])
    8
    def hello_monkey():
    9
    """Respond to incoming calls with a simple text message."""
    10
    11
    resp = VoiceResponse()
    12
    resp.say("Hello from your pals at Twilio! Have fun.")
    13
    return str(resp)
    14
    15
    16
    if __name__ == "__main__":
    17
    app.run(debug=True)
  2. Save the file.

  3. In a new terminal window, run the following command to start the Python development server:

    python answer_phone.py
  4. 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 5000
  5. Set up a webhook that triggers when your Twilio phone number receives a phone call.

    Twilio ConsoleLegacy Console

    If you use the new Twilio Console, follow these steps:

    1. Go to Numbers & senders(link takes you to an external page).
    2. Click your Twilio phone number you want for your app.
    3. Click the Configuration details tab.
    4. Click Messaging > Edit details.
    5. On the Edit messaging configuration dialog, select the Webhook, TwiML Bin, Function, Studio Flow, Proxy Service option.
    6. Under How do you want to set up your primary method?, select Webhook.
    7. Paste your ngrok public URL in the URL box.
      If your ngrok console shows Forwarding https://<your-app-name>.ngrok.dev, paste https://<your-app-name>.ngrok.dev to the URL box.
  6. With the Python development server and ngrok running, call your Twilio phone number. You hear the text-to-speech message defined in answer_phone.py.


After following this guide, you can make an outbound call and handle inbound calls programmatically with Twilio Programmable Voice. You can confirm it works by running your server-side script to trigger an outbound call to your personal device or by calling your Twilio number to hear the <Say> text-to-speech response.


To build on what you've learned in this guide, consult the following guides: