Skip to contentSkip to navigationSkip to topbar
On this page

Server-side quickstart for Programmable Voice


This quickstart shows you how to build a server-side application that makes and receives phone calls. The code in this quickstart makes an outbound call with the Twilio Voice API and it handles an inbound call with text-to-speech.


Complete the prerequisites

complete-the-prerequisites page anchor

Select your programming language and complete the prerequisites:

PythonNode.jsPHPC#/.NETJavaGoRuby
  • Install Python 3.3 or later(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
  • Create a Twilio account(link takes you to an external page).

  • Buy a voice-enabled phone number(link takes you to an external page).


Set environment variables

set-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

Follow these steps to make a phone call from your Twilio number.

PythonNode.jsPHPC#/.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 your script links to.

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#/.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:

    1. Go to the Active Numbers(link takes you to an external page) page in the Twilio Console.
    2. Click your Twilio phone number.
    3. Go to the Configure tab and find the Voice Configuration section.
    4. In the A call comes in row, select the Webhook option.
    5. Paste your ngrok public URL in the URL field. 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.
    6. Click Save configuration.
  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.