Record a message

A code snippet that shows how to record a conversation. Answer an incoming call and return an NCCO that includes a record action. When the call is complete, a webhook is sent to the eventUrl you specify. The webhook includes the URL of the recording.

Prerequisites

Create an Application

You can install the CLI with the following command:

npm install --location=global @vonage/cli

Before you can start working with your apps, you need to register your configuration: API Key and Secret. You can find them via the Dashboard, in API Settings. Once set, initialize your account using the following command:

vonage config:set --apiKey=XXXXXX --apiSecret=XXXXXX

As soon as the CLI is both installed and configured, use it to create a Vonage application using the following command:

vonage apps:create

The command starts an interactive prompt to ask for the application name, and the capabilities you want to enable - make sure to enable Voice.

When finished, it creates the vonage_app.json file in the current directory containing the Application ID, Application name and private key. It also creates a second file with the private key name app_name.key.

Rent a Number

You can rent a number using the Vonage CLI. The following command purchases an available number in the United States:

vonage numbers:search USvonage numbers:buy 15555555555 US

Specify an alternative two-character country code to purchase a number in another country.

Now that you have both an application and a number, you need to link them together.

Replace YOUR_VONAGE_NUMBER with the number you bought and APPLICATION_ID with your application id, then run the following command:

vonage apps:link APPLICATION_ID --number=YOUR_VONAGE_NUMBER

Example

Prerequisites

npm install express body-parser

Write the code

Add the following to record-a-message.js:

const Express = require('express');
const bodyParser = require('body-parser');

const app = new Express();
app.use(bodyParser.json());

const onInboundCall = (request, response) => {
  const ncco = [
    {
      action: 'talk',
      text: 'Please leave a message after the tone, then press #. We will get back to you as soon as we can.',
    },
    {
      action: 'record',
      endOnKey: '#',
      beepStart: 'true',
      endOnSilence: '3',
      eventUrl: [`${request.protocol}://${request.get('host')}/webhooks/recordings`],
    },
    {
      action: 'talk',
      text: 'Thank you for your message. Goodbye.',
    },
  ];

  response.json(ncco);
};

const onRecording = (request, response) => {
  const recording_url = request.body.recording_url;
  console.log(`Recording URL = ${recording_url}`);

  response.status(204).send();
};

app
  .get('/webhooks/answer', onInboundCall)
  .post('/webhooks/recordings', onRecording);

app.listen(3000);

View full source

Run your code

Save this file to your machine and run it:

node record-a-message.js

Try it out

You will need to:

  1. Record a message by dialling your Vonage Number, and leaving your message after the tone (this code snippet).
  2. Download the recording. See the Download a recording code snippet for how to do this.

Further Reading

  • Voicemail - Learn how to record audio from inbound calls using .NET and the Vonage Voice API.