How to Make a Phone Call Using Node.js and Twilio Programmable Voice

March 24, 2021
Written by
Diane Phan
Twilion
Reviewed by

phonecallnode

Twilio Programmable Voice allows you to make and receive voice calls in your software application. In this tutorial, you’ll see a demonstration of how to use Programmable Voice to make a voice call directly from a Node.js app.

Prerequisites

  • A free Twilio account (register here and receive $10 in Twilio credit!)
  • Node.js installed on your machine
  • A phone that can make phone calls, to test the project

Set up your environment

In this section you are going to set up a brand new Node.js project. To keep things nicely organized, open a terminal or command prompt, find a suitable place and create a new directory where the project you are about to create will live:

mkdir nodejs-phone-call
cd nodejs-phone-call

Install the Twilio Node Helper Library from your command line with the following:

npm install twilio

Configure Twilio credentials

To be able to access the Twilio service, your Node.js application will need your Twilio account credentials to log in and authenticate. The most secure way to define these credentials is to add them as environment variables.

The information that you need is the “Account SID” and the “Auth Token”. You can find both on the dashboard of the Twilio Console:

Twilio account SID and auth token

In your project directory, create a .env file. Open the new .env file in your favorite text editor and copy the Account SID to add it as the value for the TWILIO_ACCOUNT_SID environment variable. Likewise, copy the Auth Token and add it as the value for the TWILIO_AUTH_TOKEN environment variable:

TWILIO_ACCOUNT_SID=xxxxxxxxx
TWILIO_AUTH_TOKEN=xxxxxxxxx

Buy a Twilio phone number

To be able to make a phone call you need to have a phone number associated with your Twilio account. Log in to the Twilio Console, select Phone Numbers, and then click on the red plus sign to buy a Twilio number. Note that if you are using a free account you will be using your trial credit for this purchase.

In the Buy a Number screen you can select your country and check Voice in the capabilities field. If you’d like to request a number from your region, you can enter your area code in the Number field.

screenshot of buy a number page

Click the Search button to see what numbers are available, and then click Buy for the number that you like from the results. After you confirm your purchase, click the Close button.

Create a TwiML Bin

TwiML is Twilio's markup language, an extension to XML that is used to provide instructions to Twilio on how certain events need to be handled. When a call is made from the Twilio phone number you purchased earlier, Twilio looks up the URL associated with your number and sends a request to that URL. If your URL responds with TwiML, Twilio will follow these instructions to handle the phone call.

You can host TwiML directly through Twilio, in a TwiML Bin. Navigate to the TwiML Bins section of the Console.

Click the Create new TwiML Bin button to create a new bin. This will direct you to a new page where you can configure your bin.

Screenshot of TwiML Bin configuration page

Give your bin any friendly name of your choosing, such as "Nodejs-Call". Then copy and paste the following TwiML into the TwiML field, replacing anything there already:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Say>Hello, from Node.js!</Say>
</Response>

Scroll down and click the Create button. The page will refresh and then at the top of the new page, you will see the SID and URL values assigned to your new TwiML bin. Copy the URL to the clipboard.

Make a phone call with Node.js

Fire up your text editor or IDE and create a new JavaScript file in the nodejs-phone-call directory you created at the start of the tutorial.

Enter the following code in this file:

require('dotenv').config();

const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);

client.calls
  .create({
    from:'<YOUR_TWILIO_PHONE_NUMBER>',
    to:'<YOUR_PHONE_NUMBER>',     
    url: '<YOUR_TWIML_BIN_URL>'
  })
  .then(call => console.log(call.sid));

Make sure you update the following parts of the above code snippet:

  • Replace the value of the from argument with your Twilio phone number
  • Replace the value of the to argument with your personal phone number
  • Replace the value of the url argument with the URL you just copied from your TwiML bin

Be sure to use E.164 format for the phone numbers.

Head back to your terminal, make sure you have defined the environment variables with your Twilio credentials as indicated above, and then run the script as follows:

node make-call.js

Your personal phone will ring in just a few moments. When you answer the call, you’ll hear “Hello, from Node.js!”.

Feel free to go back to your TwiML bin in the Console and edit the message you want the caller to hear!

What's next for making phone calls with Node.js?

In this tutorial you’ve learned how to make a phone call from a Node.js application. But this is just the beginning. Be sure to check the TwiML reference to find out how you can do lots of other cool things with your phone calls!

Diane Phan is a Developer Network editor on the Developer Voices team. She loves to help programmers tackle difficult challenges that might prevent them from bringing their projects to life. She can be reached at dphan [at] twilio.com or LinkedIn.