Download a recording

In this code snippet you see how to download a recording.

Example

Replace the following variables in the example code:

Key Description
RECORDING_URL The URL of the recording to download. You typically get this from the JSON response received on the /webhooks/recordings endpoint when the record action is used.

Prerequisites

To fetch a recording you must use the same VONAGE_APPLICATION_ID and private key for the application that owns the call that you're trying to download.

Execute the following command at your terminal prompt to create the JWT for authentication:

export JWT=$(nexmo jwt:generate $PATH_TO_PRIVATE_KEY application_id=$NEXMO_APPLICATION_ID)

Write the code

Add the following to download-a-recording.sh:

curl $RECORDING_URL \
  -H "Authorization: Bearer "$JWT \
  --output recording.mp3

View full source

Run your code

Save this file to your machine and run it:

bash download-a-recording.sh

Prerequisites

To fetch a recording you must use the same VONAGE_APPLICATION_ID and private key for the application that owns the call that you're trying to download.

npm install @vonage/server-sdk

Create a file named download-a-recording.js and add the following code:

const Vonage = require('@vonage/server-sdk');

const vonage = new Vonage({
  apiKey: VONAGE_API_KEY,
  apiSecret: VONAGE_API_SECRET,
  applicationId: VONAGE_APPLICATION_ID,
  privateKey: VONAGE_PRIVATE_KEY
}, {debug: true});

View full source

Write the code

Add the following to download-a-recording.js:

vonage.files.save(RECORDING_URL, 'test.mp3', (err, res) => {
  if(err) { console.error(err); }
  else {
      console.log(res);
  }
});

View full source

Run your code

Save this file to your machine and run it:

node download-a-recording.js

Prerequisites

To fetch a recording you must use the same VONAGE_APPLICATION_ID and private key for the application that owns the call that you're trying to download.

Add the following to `build.gradle`:

compile 'com.vonage:client:6.2.0'

Create a class named DownloadRecording and add the following code to the main method:

VonageClient client = VonageClient.builder()
        .applicationId(VONAGE_APPLICATION_ID)
        .privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
        .build();

View full source

Write the code

Add the following to the main method of the DownloadRecording class:

/*
 * A recording webhook endpoint which automatically downloads the specified recording to a file in the
 * current working directory, called "downloaded_recording.mp3"
 */
Route downloadRoute = (req, res) -> {
    RecordEvent event = RecordEvent.fromJson(req.body());
    final String RECORDING_URL = event.getUrl();

    System.out.println("Downloading from " + RECORDING_URL);
    client.getVoiceClient().downloadRecording(RECORDING_URL).save("downloaded_recording.mp3");
    return "OK";
};

Spark.port(3000);
Spark.post("/recording", downloadRoute);

View full source

Run your code

We can use the application plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:

  apply plugin: 'application'
  mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''

Run the following gradle command to execute your application, replacing com.vonage.quickstart.voice with the package containing DownloadRecording:

gradle run -Pmain=com.vonage.quickstart.voice.DownloadRecording

Prerequisites

To fetch a recording you must use the same VONAGE_APPLICATION_ID and private key for the application that owns the call that you're trying to download.

Install-Package Vonage

Create a file named GetRecording.cs and add the following code:

using Vonage;
using Vonage.Request;
using Vonage.Voice;

View full source

Add the following to GetRecording.cs:

var credentials = Credentials.FromAppIdAndPrivateKeyPath(VONAGE_APPLICATION_ID, VONAGE_PRIVATE_KEY_PATH);
var client = new VonageClient(credentials);

View full source

Write the code

Add the following to GetRecording.cs:

var response = client.VoiceClient.GetRecording(RECORDING_URL);
File.WriteAllBytes("your_recording.mp3", response.ResultStream);

View full source

Prerequisites

To fetch a recording you must use the same VONAGE_APPLICATION_ID and private key for the application that owns the call that you're trying to download.

composer require vonage/client

Create a file named index.php and add the following code:

$keypair = new \Vonage\Client\Credentials\Keypair(
    file_get_contents(VONAGE_APPLICATION_PRIVATE_KEY_PATH),
    VONAGE_APPLICATION_ID
);

$client = new \Vonage\Client($keypair);

View full source

Write the code

Add the following to index.php:

$recordingUrl = 'https://api.nexmo.com/v1/files/'.VONAGE_RECORDING_ID;
$data = $client->get($recordingUrl);
file_put_contents($recordingId.'.mp3', $data->getBody());

View full source

Run your code

Save this file to your machine and run it:

php index.php

Prerequisites

To fetch a recording you must use the same VONAGE_APPLICATION_ID and private key for the application that owns the call that you're trying to download.

pip install vonage

Create a file named get-recording.py and add the following code:

client = vonage.Client(
    application_id=VONAGE_APPLICATION_ID,
    private_key=VONAGE_APPLICATION_PRIVATE_KEY_PATH,
)

View full source

Write the code

Add the following to get-recording.py:

response = client.get_recording(URL)
pprint(response)

View full source

Run your code

Save this file to your machine and run it:

python3 get-recording.py > recording.mp3

Prerequisites

To fetch a recording you must use the same VONAGE_APPLICATION_ID and private key for the application that owns the call that you're trying to download.

gem install vonage

Create a file named download-a-recording.rb and add the following code:

client = Vonage::Client.new(
  application_id: VONAGE_APPLICATION_ID,
  private_key: File.read(VONAGE_APPLICATION_PRIVATE_KEY_PATH)
)

View full source

Write the code

Add the following to download-a-recording.rb:

response = client.files.save(RECORDING_URL, 'recording.mp3')

puts response.inspect

View full source

Run your code

Save this file to your machine and run it:

ruby download-a-recording.rb

Try it out

You will need a Recording URL from which to download the recording file. You typically get this from the JSON response received on the /webhooks/recordings endpoint when the record action is used when recording a call, connecting another call and so on. A typical JSON response will resemble the following:

{'conversation_uuid': 'CON-ddddaaaa-bbbb-cccc-dddd-0123456789de',
 'end_time': '2018-08-10T11:19:31Z',
 'recording_url': 'https://api.nexmo.com/v1/files/aaaaaaaa-bbbb-cccc-dddd-0123456789ab',
 'recording_uuid': 'ccccaaaa-dddd-cccc-dddd-0123456789ab',
 'size': 162558,
 'start_time': '2018-08-10T11:18:51Z',
 'timestamp': '2018-08-10T11:19:31.744Z'}
1.2.3.4 - - [10/Aug/2018 11:19:31] "POST /webhooks/recordings HTTP/1.1" 200 -

When you run the script, the recording located at the recording URL will be downloaded. You can then listen to the recording.