Nexmo Voice API - Record audio file

How to Record Audio from Incoming Calls with Node.js

Published February 06, 2017 by Tomomi Imura

This is the third tutorial in the “Getting Started with Nexmo and Node.js” series, which followed our Getting Started series on SMS APIs. See links to prior tutorials in these series at the bottom of the post.

In the previous tutorial, you created a voice application and learned how to receive a text-to-voice call using the Nexmo Voice API and the Node.js client library. In this tutorial, you will extend the app to record a message from a caller.

GitHub icon View the source code record-call.js GitHub

Defining a Nexmo Call Control Object to Record Incoming Calls

The previous tutorial walked through creating webhook endpoint URLs and associating them with a voice application to receive incoming calls. In this tutorial, you will modify the NCCO to record audio. If you have not completed the previous tutorial yet, follow that one first.

To begin, run ngrok:

You are going to use the forwarding URLs, which look like https://db95720f.ngrok.io, as your temporary webhook endpoints during development.

Update your Nexmo application with the ngrok URLs. (You can skip this part if you resume using the same ngrok or your server URL):

Now, let’s modify your webhook code.

Edit the NCCO in the HTTP GET route to handle the requests for /answer:

Notice the eventUrl in the NCCO. This is where the information about the recording is sent. You can reuse the generic /event route you have created for the application, or create an another POST route:

Run the script, and try calling your Nexmo phone number. If everything is working, you should hear a greeting from “Jennifer” followed by a beep. Leave a message and press # on your keypad.

By default, the recorded audio is saved in MP3 format and stored by Nexmo for 30 days.

When a recording is completed, the event returns the information, including the audio file URL, recording_uuid:

Next, you will retrieve the recording from the recording_url.

Retrieving the Voicemail Message

Let’s modify the /record route to download the audio file as soon as the recording is completed.

Install the Nexmo Node.js library via npm. You will need version 1.2.0 of the library to be able to use the save feature, so upgrade if you are using an older version.

Include the nexmo module and then initialize with your credentials, the App ID, and the private key you generated with the CLI tool for the previous tutorial:

As explained earlier, when a voice message is recorded, the specified webhook is triggered and it returns the info with the recording_url. You can fetch the MP3 audio file from this URL. Please note that the recorded file ID is not the same as other uuids!

Let’s modify the /record route and use the files.save method to download the file to your disk:

Run the script and call your Nexmo phone number to record your message! After the call, you will see an audio file downloaded in the same directory! Yay!

Nexmo Voice API - audio file download

Learn More

Here are some resources you can use to dive deeper into Nexmo APIs and Node.js.

API References and Tools

Nexmo Getting Started Guide for Node.js

Nexmo Logo