Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Record Phone Calls in Node.js


In this guide we'll show you how to use Programmable Voice(link takes you to an external page) to record phone calls with your Node.js web application. You can tell Twilio to record part of a phone call or the entire thing. The code snippets in this guide are written using modern JavaScript language features in Node.js version 6 or higher, and make use of the following modules:

Let's get started!


Set up your web application

set-up-your-web-application page anchor
Incoming Voice.

While it might be fun to program a robot to answer a physical phone, Twilio makes answering a phone call as easy as responding to an HTTP request. When a phone number you have bought through Twilio receives an incoming call, Twilio will send an HTTP request to a server you control, asking for instructions on how to handle the call. Your server will respond with an XML document containing TwiML that tells Twilio to read out a message, play an MP3 file, make a recording, and much more.

To start answering phone calls, you must:

(warning)

Warning

If you are sending SMS messages to the U.S. or Canada, before proceeding further please be aware of updated restrictions on the use of Toll-Free numbers for messaging, including TF numbers obtained by purchasing them. These restrictions do not apply to Voice or other uses outside of SMS messaging. Please click here(link takes you to an external page) for details.

Buy and configure a phone number

buy-and-configure-a-phone-number page anchor

In the console(link takes you to an external page), you can search for and buy phone numbers in dozens of different countries, capable of calling (and being called by) just about every phone on the planet.

Search for voice capable numbers.

Once you purchase a number, you'll need to configure that number to send a request to your web application. This callback mechanism is called a webhook(link takes you to an external page). This can be done in the number's configuration page.

configure an incoming phone number URL.

A webhook(link takes you to an external page) is a callback mechanism that allows two systems to communicate events to one another over the Internet using HTTP requests. In this case, Twilio is sending a webhook request to your web application whenever a phone number you control receives an incoming call. You'll see this webhook mechanism used in many Twilio APIs for handling event notifications like this.

Not working on a server with a public URL? We'll show you how to expose your local development machine to the public Internet later in this guide. Next, you'll need to write some server-side code that will be executed when an incoming call comes in.


Record part of a phone call

record-part-of-a-phone-call page anchor

Now comes the fun part - writing code that will handle an incoming HTTP request from Twilio!

In this example we'll use the Express web framework(link takes you to an external page) for Node.js to respond to Twilio's request and we'll use TwiML to tell Twilio how to handle the call.

Record part of an incoming call

record-part-of-an-incoming-call page anchor

Use the <Record> TwiML verb to record a message from the caller

Node.js

_24
const express = require('express');
_24
const VoiceResponse = require('twilio').twiml.VoiceResponse;
_24
_24
const app = express();
_24
_24
// Returns TwiML which prompts the caller to record a message
_24
app.post('/record', (request, response) => {
_24
// Use the Twilio Node.js SDK to build an XML response
_24
const twiml = new VoiceResponse();
_24
twiml.say('Hello. Please leave a message after the beep.');
_24
_24
// Use <Record> to record the caller's message
_24
twiml.record();
_24
_24
// End the call with <Hangup>
_24
twiml.hangup();
_24
_24
// Render the response as XML in reply to the webhook request
_24
response.type('text/xml');
_24
response.send(twiml.toString());
_24
});
_24
_24
// Create an HTTP server and listen for requests on port 3000
_24
app.listen(3000);

TwiML is a set of XML tags that tell Twilio how to handle an incoming call (or SMS). In this example we tell Twilio to read some instructions to the caller and then record whatever the caller says next.

You can listen to your recordings in your Twilio Console or access them directly through Twilio's REST API.

Transcribing a recording

transcribing-a-recording page anchor

You can also tell Twilio to transcribe a recording, giving you a text representation of what the caller said.

Record and transcribe an incoming call

record-and-transcribe-an-incoming-call page anchor

Use the <Record> TwiML verb with some extra arguments to transcribe the caller's message

Node.js

_24
const express = require('express');
_24
const VoiceResponse = require('twilio').twiml.VoiceResponse;
_24
_24
const app = express();
_24
_24
// Returns TwiML which prompts the caller to record a message
_24
app.post('/record', (request, response) => {
_24
// Use the Twilio Node.js SDK to build an XML response
_24
const twiml = new VoiceResponse();
_24
twiml.say('Hello. Please leave a message after the beep.');
_24
_24
// Use <Record> to record and transcribe the caller's message
_24
twiml.record({ transcribe: true, maxLength: 30 });
_24
_24
// End the call with <Hangup>
_24
twiml.hangup();
_24
_24
// Render the response as XML in reply to the webhook request
_24
response.type('text/xml');
_24
response.send(twiml.toString());
_24
});
_24
_24
// Create an HTTP server and listen for requests on port 3000
_24
app.listen(3000);

Here we add "transcribe: true" to our response to tell Twilio to transcribe the recording after it's complete. We also pass a "maxLength" argument to limit the length of the recording (it defaults to an hour).

Check out the <Record> reference docs to see all the parameters you can use to customize your recordings.


Record an entire outgoing call

record-an-entire-outgoing-call page anchor

When you make outgoing calls with the Twilio REST API, you can tell Twilio to record the entire call from beginning to end.

Grab your Twilio account credentials

grab-your-twilio-account-credentials page anchor

First, you'll need to get your Twilio account credentials. They can be found on the home page of the console.

console credentials.

Make and record an outbound call

make-and-record-an-outbound-call page anchor

Just pass an extra "record" argument to "client.calls.create()" and Twilio will record the entire phone call.

Node.js

_15
// Download the helper library from https://www.twilio.com/docs/node/install
_15
// Find your Account SID and Auth Token at twilio.com/console
_15
// and set the environment variables. See http://twil.io/secure
_15
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_15
const authToken = process.env.TWILIO_AUTH_TOKEN;
_15
const client = require('twilio')(accountSid, authToken);
_15
_15
client.calls
_15
.create({
_15
record: true,
_15
url: 'http://demo.twilio.com/docs/voice.xml',
_15
to: '+14155551212',
_15
from: '+15017122661'
_15
})
_15
.then(call => console.log(call.sid));

Output

_37
{
_37
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_37
"answered_by": null,
_37
"api_version": "2010-04-01",
_37
"caller_name": null,
_37
"date_created": "Tue, 31 Aug 2010 20:36:28 +0000",
_37
"date_updated": "Tue, 31 Aug 2010 20:36:44 +0000",
_37
"direction": "inbound",
_37
"duration": "15",
_37
"end_time": "Tue, 31 Aug 2010 20:36:44 +0000",
_37
"forwarded_from": "+141586753093",
_37
"from": "+15017122661",
_37
"from_formatted": "(501) 712-2661",
_37
"group_sid": null,
_37
"parent_call_sid": null,
_37
"phone_number_sid": "PNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_37
"price": "-0.03000",
_37
"price_unit": "USD",
_37
"sid": "CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_37
"start_time": "Tue, 31 Aug 2010 20:36:29 +0000",
_37
"status": "completed",
_37
"subresource_uris": {
_37
"notifications": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Notifications.json",
_37
"recordings": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Recordings.json",
_37
"payments": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Payments.json",
_37
"events": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Events.json",
_37
"siprec": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Siprec.json",
_37
"streams": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Streams.json",
_37
"user_defined_message_subscriptions": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/UserDefinedMessageSubscriptions.json",
_37
"user_defined_messages": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/UserDefinedMessages.json"
_37
},
_37
"to": "+14155551212",
_37
"to_formatted": "(415) 555-1212",
_37
"trunk_sid": null,
_37
"uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json",
_37
"queue_time": "1000"
_37
}

Once the call is complete, you can listen to your recordings in your Twilio Console(link takes you to an external page) or access them directly through Twilio's REST API.

You can also gain access to the recording as soon as the call is complete by including a recordingStatusCallback with your client.calls.create method. When your recording is ready, Twilio will send a request to the URL you specified, and that request will include a link to the recording's audio file.

You can learn more about the RecordingStatusCallback parameter in the Call Resource API documentation.


If this guide was helpful, you might also want to check out these tutorials for Programmable Voice and Node.js. Tutorials walk through full sample applications, implementing Twilio use cases like these:

Happy hacking!


Rate this page: