Server-side quickstart for Programmable Voice Positive Feedback Negative Feedback 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 .
Positive Feedback Negative Feedback
Positive Feedback Negative Feedback
Positive Feedback Negative Feedback Select your programming language and complete the prerequisites:
Python Node.js PHP C# or .NET Java Go Ruby
Positive Feedback Negative Feedback Follow these steps to get your Twilio account credentials and set them as environment variables.
macOS Terminal Windows command line PowerShell
Go to the Twilio Console(link takes you to an external page) .
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
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
Go to the Twilio Console(link takes you to an external page) .
Copy your Account SID and set it as an environment variable using the following command. Replace YOUR_ACCOUNT_SID with your actual Account SID.
set TWILIO_ACCOUNT_SID=YOUR_ACCOUNT_SID
Copy your Auth Token and set it as an environment variable using the following command. Replace YOUR_AUTH_TOKEN with your actual Auth Token.
set TWILIO_AUTH_TOKEN=YOUR_AUTH_TOKEN
Go to the Twilio Console(link takes you to an external page) .
Copy your Account SID and set it as an environment variable using the following command. Replace YOUR_ACCOUNT_SID with your actual Account SID.
$Env:TWILIO_ACCOUNT_SID = "YOUR_ACCOUNT_SID"
Copy your Auth Token and set it as an environment variable using the following command. Replace YOUR_AUTH_TOKEN with your actual Auth Token.
$Env:TWILIO_AUTH_TOKEN = "YOUR_AUTH_TOKEN"
Positive Feedback Negative Feedback To make a phone call from your Twilio number, follow these steps:
Python Node.js PHP C# or .NET Java Go Ruby
Create a file named make_call.py and add the following code:
1 # Download the helper library from https://www.twilio.com/docs/python/install
3 from twilio.rest import Client
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)
11 call = client.calls.create(
12 url = "http://demo.twilio.com/docs/voice.xml" ,
Replace the from_ phone number with your Twilio number.
Replace the to phone number with your own phone number.
Save your changes.
Run the script:
Your phone rings and you hear the short message in the [TwiML][] document that links to your script.
Create a file named make_call.js in the folder where you installed Express and the Twilio SDK.
Add the following code to make_call.js:
1 // Download the helper library from https://www.twilio.com/docs/node/install
2 const twilio = require ( "twilio" ); // Or, for ESM: import twilio from "twilio";
4 // Find your Account SID and Auth Token at twilio.com/console
5 // and set the environment variables. See http://twil.io/secure
6 const accountSid = process.env. TWILIO_ACCOUNT_SID ;
7 const authToken = process.env. TWILIO_AUTH_TOKEN ;
8 const client = twilio (accountSid, authToken);
10 async function createCall () {
11 const call = await client.calls. create ({
14 url: "http://demo.twilio.com/docs/voice.xml" ,
Replace the from phone number with your Twilio number.
Replace the to phone number with your own phone number.
Save your changes.
Run the script:
Your phone rings and you hear the short message in the [TwiML][] document that links to your script.
Create a file named make_call.php in the folder where you installed the Twilio SDK.
Add the following code to make_call.php:
3 // Update the path below to your autoload.php,
4 // see https://getcomposer.org/doc/01-basic-usage.md
5 require_once "/path/to/vendor/autoload.php" ;
9 // Find your Account SID and Auth Token at twilio.com/console
10 // and set the environment variables. See http://twil.io/secure
11 $sid = getenv ( "TWILIO_ACCOUNT_SID" );
12 $token = getenv ( "TWILIO_AUTH_TOKEN" );
13 $twilio = new Client ($sid, $token);
15 $call = $twilio -> calls -> create (
18 [ "url" => "http://demo.twilio.com/docs/voice.xml" ],
Update line 5 of make_call.php to require __DIR__ . '/vendor/autoload.php';
Replace the first argument to $twilio->messages->create() with your own phone number.
Replace the second argument to $twilio->messages->create() with your Twilio phone number.
Save your changes.
Run the script:
Your phone rings and you hear the short message in the [TwiML][] document that links to your script.
Create a directory for this project:
Open that directory in the terminal:
Initialize your app:
Install the Twilio .NET SDK:
dotnet add package Twilio
Replace the contents of Program.cs with the following code:
1 // Install the C# / .NET helper library from twilio.com/docs/csharp/install
5 using Twilio . Rest . Api . V2010 . Account ;
6 using System . Threading . Tasks ;
9 public static async Task Main ( string [] args ) {
10 // Find your Account SID and Auth Token at twilio.com/console
11 // and set the environment variables. See http://twil.io/secure
12 string accountSid = Environment. GetEnvironmentVariable ( "TWILIO_ACCOUNT_SID" );
13 string authToken = Environment. GetEnvironmentVariable ( "TWILIO_AUTH_TOKEN" );
15 TwilioClient. Init (accountSid, authToken);
17 var call = await CallResource. CreateAsync (
18 url : new Uri ( "http://demo.twilio.com/docs/voice.xml" ),
19 to : new Twilio . Types . PhoneNumber ( "+18005550100" ),
20 from : new Twilio . Types . PhoneNumber ( "+18005550199" ));
22 Console. WriteLine (call.Sid);
Replace the from phone number with your Twilio number.
Replace the to phone number with your own phone number.
Save your changes.
Run the app:
Your phone rings and you hear the short message in the [TwiML][] document that links to your script.
Create a folder for this project:
Go to the project folder:
Initialize the app:
gradle init --type basic --dsl groovy --use-defaults
Create folders for the project code:
Replace the contents of the build.gradle file with the following code:
15 implementation 'com.twilio.sdk:twilio:10.5.2'
16 implementation 'org.slf4j:slf4j-simple:2.0.16'
17 implementation 'com.sparkjava:spark-core:2.9.4'
Save the file.
Create a file named Example.java in src/main/java and add the following code:
1 // Install the Java helper library from twilio.com/docs/java/install
4 import com.twilio.Twilio;
5 import com.twilio.rest.api.v2010.account.Call;
8 // Find your Account SID and Auth Token at twilio.com/console
9 // and set the environment variables. See http://twil.io/secure
10 public static final String ACCOUNT_SID = System. getenv ( "TWILIO_ACCOUNT_SID" );
11 public static final String AUTH_TOKEN = System. getenv ( "TWILIO_AUTH_TOKEN" );
13 public static void main ( String [] args ) {
14 Twilio. init (ACCOUNT_SID, AUTH_TOKEN);
15 Call call = Call. creator ( new com.twilio.type. PhoneNumber ( "+18005550100" ),
16 new com.twilio.type. PhoneNumber ( "+18005550199" ),
17 URI. create ( "http://demo.twilio.com/docs/voice.xml" ))
20 System.out. println (call. getSid ());
Replace +18005550199 with your Twilio phone number.
Replace +18005550100 with your own phone number.
Save your changes.
Run the app:
Your phone rings and you hear the short message in the [TwiML][] document that links to your script.
Create a folder for this project:
Go to the folder:
Create a Go module:
go mod init voice-quickstart
Create a file named make_call.go and add the following code:
1 // Download the helper library from https://www.twilio.com/docs/go/install
6 " github.com/twilio/twilio-go "
7 api " github.com/twilio/twilio-go/rest/api/v2010 "
12 // Find your Account SID and Auth Token at twilio.com/console
13 // and set the environment variables. See http://twil.io/secure
14 // Make sure TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN exists in your environment
15 client := twilio. NewRestClient ()
17 params := & api . CreateCallParams {}
18 params. SetUrl ( "http://demo.twilio.com/docs/voice.xml" )
19 params. SetTo ( "+18005550100" )
20 params. SetFrom ( "+18005550199" )
22 resp, err := client.Api. CreateCall (params)
Replace the from phone number with your Twilio number.
Replace the to phone number with your own phone number.
Save your changes.
Install dependencies:
Run the script:
Your phone rings and you hear the short message in the [TwiML][] document that links to your script.
Create a Gemfile:
Install Sinatra(link takes you to an external page) and other project dependencies:
bundle add puma twilio-ruby sinatra
Create a file named make_call.rb and add the following code:
1 # Download the helper library from https://www.twilio.com/docs/ruby/install
4 # Find your Account SID and Auth Token at twilio.com/console
5 # and set the environment variables. See http://twil.io/secure
6 account_sid = ENV [ 'TWILIO_ACCOUNT_SID' ]
7 auth_token = ENV [ 'TWILIO_AUTH_TOKEN' ]
8 @client = Twilio :: REST :: Client . new (account_sid, auth_token)
15 url: 'http://demo.twilio.com/docs/voice.xml' ,
Replace the from phone number with your Twilio number.
Replace the to phone number with your own phone number.
Save your changes.
Run the script:
Your phone rings and you hear the short message in the [TwiML][] document that links to your script.
Positive Feedback Negative Feedback Follow these steps to receive a phone call with your Twilio number and respond using text-to-speech.
Python Node.js PHP C# or .NET Java Go Ruby
Create a file named answer_phone.py and add the following code:
2 from twilio.twiml.voice_response import VoiceResponse
7 @app.route ( "/" , methods = [ 'GET' , 'POST' ])
9 """Respond to incoming calls with a simple text message."""
12 resp.say( "Hello from your pals at Twilio! Have fun." )
16 if __name__ == "__main__" :
Save the file.
In a new terminal window, run the following command to start the Python development server:
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:
Set up a webhook that triggers when your Twilio phone number receives a phone call.
Twilio Console Legacy Console
If you use the new Twilio Console, follow these steps:
Go to Numbers & senders (link takes you to an external page) .
Click your Twilio phone number you want for your app.
Click the Configuration details tab.
Click Messaging > Edit details .
On the Edit messaging configuration dialog, select the Webhook, TwiML Bin, Function, Studio Flow, Proxy Service option.
Under How do you want to set up your primary method? , select Webhook .
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.
If you use the legacy Console, follow these steps:
Go to Active Numbers (link takes you to an external page) .
Click your Twilio phone number.
Click Configure next to your phone number. Twilio opens the Phone Number page with the Configure tab selected.
Go to the Voice Configuration section.
In the A call comes in dropdown menu, select Webhook .
In the URL box, paste your ngrok public URL.
For example, https://<your-app-name>.ngrok.dev).
Leave the other boxes and menus with their default values.
At the end of the page, click Save configuration .
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.
Create a file named answer_phone.js in the same folder as make_call.js and add the following code:
1 const express = require ( 'express' );
2 const VoiceResponse = require ( 'twilio' ).twiml.VoiceResponse;
6 // Create a route that will handle Twilio webhook requests, sent as an
7 // HTTP POST to /voice in our application
8 app. post ( '/voice' , ( request , response ) => {
9 // Use the Twilio Node.js SDK to build an XML response
10 const twiml = new VoiceResponse ();
12 twiml. say ( 'Hello from your pals at Twilio! Have fun.' );
14 // Render the response as XML in reply to the webhook request
15 response. type ( 'text/xml' );
16 response. send (twiml. toString ());
19 // Create an HTTP server and listen for requests on port 1337
21 console. log ( 'TwiML server running at http://127.0.0.1:1337/' );
Save the file.
In a new terminal window, run the following command to start your server:
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:
Set up a webhook that triggers when your Twilio phone number receives a phone call.
Twilio Console Legacy Console
If you use the new Twilio Console, follow these steps:
Go to Numbers & senders (link takes you to an external page) .
Click your Twilio phone number you want for your app.
Click the Configuration details tab.
Click Messaging > Edit details .
On the Edit messaging configuration dialog, select the Webhook, TwiML Bin, Function, Studio Flow, Proxy Service option.
Under How do you want to set up your primary method? , select Webhook .
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.
If you use the legacy Console, follow these steps:
Go to Active Numbers (link takes you to an external page) .
Click your Twilio phone number.
Click Configure next to your phone number. Twilio opens the Phone Number page with the Configure tab selected.
Go to the Voice Configuration section.
In the A call comes in dropdown menu, select Webhook .
In the URL box, paste your ngrok public URL.
For example, https://<your-app-name>.ngrok.dev).
Leave the other boxes and menus with their default values.
At the end of the page, click Save configuration .
With your server and ngrok running, call your Twilio phone number. You hear the text-to-speech message defined in answer_phone.js.
Create a file named answer_call.php in the same folder as make_call.php and add the following code:
2 require __DIR__ . '/vendor/autoload.php' ;
3 use Twilio\TwiML\VoiceResponse ;
5 // Start our TwiML response
6 $response = new VoiceResponse ();
8 // Read a message aloud to the caller
10 "Thank you for calling! Have a great day." ,
Save the file.
In a new terminal window, run the following command to start your PHP development server:
php -S localhost:8000 answer_call.php
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:
Set up a webhook that triggers when your Twilio phone number receives a phone call.
Twilio Console Legacy Console
If you use the new Twilio Console, follow these steps:
Go to Numbers & senders (link takes you to an external page) .
Click your Twilio phone number you want for your app.
Click the Configuration details tab.
Click Messaging > Edit details .
On the Edit messaging configuration dialog, select the Webhook, TwiML Bin, Function, Studio Flow, Proxy Service option.
Under How do you want to set up your primary method? , select Webhook .
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.
If you use the legacy Console, follow these steps:
Go to Active Numbers (link takes you to an external page) .
Click your Twilio phone number.
Click Configure next to your phone number. Twilio opens the Phone Number page with the Configure tab selected.
Go to the Voice Configuration section.
In the A call comes in dropdown menu, select Webhook .
In the URL box, paste your ngrok public URL.
For example, https://<your-app-name>.ngrok.dev).
Leave the other boxes and menus with their default values.
At the end of the page, click Save configuration .
With the PHP development server and ngrok running, call your Twilio phone number. You hear the text-to-speech message defined in answer_call.php.
Replace the contents of Program.cs with the following code:
2 var builder = WebApplication. CreateBuilder (args);
3 var app = builder. Build ();
5 app. MapMethods ( "/" , [ "GET" , "POST" ], () =>
7 var response = new VoiceResponse ()
8 . Say ( "Thank you for calling! Have a great day." );
9 return Results. Text (response. ToString (), "application/xml" );
12 app. Run ( "http://localhost:4567" );
Save the file.
In a new terminal window, run the following command to start your development server:
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:
Set up a webhook that triggers when your Twilio phone number receives a phone call.
Twilio Console Legacy Console
If you use the new Twilio Console, follow these steps:
Go to Numbers & senders (link takes you to an external page) .
Click your Twilio phone number you want for your app.
Click the Configuration details tab.
Click Messaging > Edit details .
On the Edit messaging configuration dialog, select the Webhook, TwiML Bin, Function, Studio Flow, Proxy Service option.
Under How do you want to set up your primary method? , select Webhook .
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.
If you use the legacy Console, follow these steps:
Go to Active Numbers (link takes you to an external page) .
Click your Twilio phone number.
Click Configure next to your phone number. Twilio opens the Phone Number page with the Configure tab selected.
Go to the Voice Configuration section.
In the A call comes in dropdown menu, select Webhook .
In the URL box, paste your ngrok public URL.
For example, https://<your-app-name>.ngrok.dev).
Leave the other boxes and menus with their default values.
At the end of the page, click Save configuration .
With your development server and ngrok running, call your Twilio phone number. You hear the text-to-speech message defined in Program.cs.
Create a file named VoiceApp.java in the src/main/java directory and add the following code:
1 import com.twilio.twiml.VoiceResponse;
2 import com.twilio.twiml.voice.Say;
4 import static spark.Spark. * ;
7 public static void main ( String [] args ) {
9 get ( "/hello" , (req, res) -> "Hello Web" );
11 post ( "/" , (request, response) -> {
12 Say say = new Say. Builder (
13 "Hello from your pals at Twilio! Have fun." )
15 VoiceResponse voiceResponse = new VoiceResponse. Builder ()
18 return voiceResponse. toXml ();
Save the file.
Open the build.gradle file and replace Example with VoiceApp. Save the file.
In a new terminal window, run the following command to start your Spark server:
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:
Set up a webhook that triggers when your Twilio phone number receives a phone call.
Twilio Console Legacy Console
If you use the new Twilio Console, follow these steps:
Go to Numbers & senders (link takes you to an external page) .
Click your Twilio phone number you want for your app.
Click the Configuration details tab.
Click Messaging > Edit details .
On the Edit messaging configuration dialog, select the Webhook, TwiML Bin, Function, Studio Flow, Proxy Service option.
Under How do you want to set up your primary method? , select Webhook .
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.
If you use the legacy Console, follow these steps:
Go to Active Numbers (link takes you to an external page) .
Click your Twilio phone number.
Click Configure next to your phone number. Twilio opens the Phone Number page with the Configure tab selected.
Go to the Voice Configuration section.
In the A call comes in dropdown menu, select Webhook .
In the URL box, paste your ngrok public URL.
For example, https://<your-app-name>.ngrok.dev).
Leave the other boxes and menus with their default values.
At the end of the page, click Save configuration .
With your Spark server and ngrok running, call your Twilio phone number. You hear the text-to-speech message defined in VoiceApp.java.
Create a file named main.go and add the following code:
6 " github.com/gin-gonic/gin "
7 " github.com/twilio/twilio-go/twiml "
13 router. POST ( "/answer" , func ( context * gin . Context ) {
15 Message: "Hello from your pals at Twilio! Have fun." ,
18 twimlResult, err := twiml. Voice ([] twiml . Element {say})
20 context. String (http.StatusInternalServerError, err. Error ())
22 context. Header ( "Content-Type" , "text/xml" )
23 context. String (http.StatusOK, twimlResult)
Save the file.
Install Gin(link takes you to an external page) and other dependencies:
In a new terminal window, run the following command to start your Gin app:
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:
Set up a webhook that triggers when your Twilio phone number receives a phone call.
Twilio Console Legacy Console
If you use the new Twilio Console, follow these steps:
Go to Numbers & senders (link takes you to an external page) .
Click your Twilio phone number you want for your app.
Click the Configuration details tab.
Click Messaging > Edit details .
On the Edit messaging configuration dialog, select the Webhook, TwiML Bin, Function, Studio Flow, Proxy Service option.
Under How do you want to set up your primary method? , select Webhook .
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.
If you use the legacy Console, follow these steps:
Go to Active Numbers (link takes you to an external page) .
Click your Twilio phone number.
Click Configure next to your phone number. Twilio opens the Phone Number page with the Configure tab selected.
Go to the Voice Configuration section.
In the A call comes in dropdown menu, select Webhook .
In the URL box, paste your ngrok public URL.
For example, https://<your-app-name>.ngrok.dev).
Leave the other boxes and menus with their default values.
At the end of the page, click Save configuration .
With your Gin app and ngrok running, call your Twilio phone number. You hear the text-to-speech message defined in main.go.
Create a file named answer_call.rb in the same folder as make_call.rb and add the following code:
4 configure :development do
5 set :host_authorization , { permitted_hosts: [] }
8 def self.get_or_post (url, & block)
16 Twilio :: TwiML :: VoiceResponse . new do | response |
17 response.say( message: "Ahoy world!" )
Save the file.
In a new terminal window, run the following command to start your Sinatra server:
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:
Set up a webhook that triggers when your Twilio phone number receives a phone call.
Twilio Console Legacy Console
If you use the new Twilio Console, follow these steps:
Go to Numbers & senders (link takes you to an external page) .
Click your Twilio phone number you want for your app.
Click the Configuration details tab.
Click Messaging > Edit details .
On the Edit messaging configuration dialog, select the Webhook, TwiML Bin, Function, Studio Flow, Proxy Service option.
Under How do you want to set up your primary method? , select Webhook .
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.
If you use the legacy Console, follow these steps:
Go to Active Numbers (link takes you to an external page) .
Click your Twilio phone number.
Click Configure next to your phone number. Twilio opens the Phone Number page with the Configure tab selected.
Go to the Voice Configuration section.
In the A call comes in dropdown menu, select Webhook .
In the URL box, paste your ngrok public URL.
For example, https://<your-app-name>.ngrok.dev).
Leave the other boxes and menus with their default values.
At the end of the page, click Save configuration .
With your Sinatra server and ngrok running, call your Twilio phone number. You hear the text-to-speech message defined in answer_call.rb.
Positive Feedback Negative Feedback 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.
Positive Feedback Negative Feedback To build on what you've learned in this guide, consult the following guides: