Record a call

A code snippets that shows how to answer an incoming call and set it up to record, then connect the call. When the call is completed, the eventUrl you specify in the record action of the NCCO will receive a webhook including the URL of the recording for download.

Example

Replace the following variables in the example code:

KeyDescription
VONAGE_VIRTUAL_NUMBER

Your Vonage Number. E.g. 447700900000

VOICE_TO_NUMBER

The recipient number to call, e.g. 447700900002.

Prerequisites

npm install express body-parser

Write the code

Add the following to record-a-call.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: 'record',
      eventUrl: [`${request.protocol}://${request.get('host')}/webhooks/recordings`],
    },
    {
      action: 'connect',
      from: VONAGE_VIRTUAL_NUMBER,
      endpoint: [
        {
          type: 'phone',
          number: VOICE_TO_NUMBER,
        },
      ],
    },
  ];
  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(port, () => {
  console.log(`Example app listening on port ${port}`);
});

View full source

Run your code

Save this file to your machine and run it:

node record-a-call.js

Try it out

You will need to:

  1. Answer and record the call (this code snippet).
  2. Download the recording. See the Download a recording code snippet for how to do this.

Further Reading

  • Call Recording - Recording audio input from a caller or recording the conversation between two callers.