Download Recordings Using Node

Overview

This guide shows how to retrieve recordings and download them to local storage. Plivo begins charging for stored recordings after 90 days. To avoid these charges, you can download recordings and store them elsewhere.

Prerequisites

To use Plivo APIs, follow our instructions to set up a Node development environment and a web server and safely expose that server to the internet.

Download recordings to local storage

Here’s sample code you can use to retrieve recordings to a local directory.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Example script for downloading recording files

var plivo = require('plivo');
var axios = require('axios');
var fs = require('fs');
var path = require('path');

const AUTH_ID = "<auth_id>";
const AUTH_TOKEN = "<auth_token>";

(function main() {
  'use strict';

  var client = new plivo.Client(AUTH_ID,AUTH_TOKEN);

  // Directory where the recordings will be saved
  var recordingsDir = path.join(__dirname, "recordings");

  if (!fs.existsSync(recordingsDir)) {
    fs.mkdirSync(recordingsDir, { recursive: true });
  }

  client.recordings.list(
    {
      add_time__gt: "2023-04-01 00:00:00",
      add_time__lt: "2023-04-30 00:00:00",
      offset: 0,
      limit: 5,
    },
  ).then(function (response) {
    console.log("Found " + response.length + " recordings.");

    response.forEach(recording => {
      var recording_url = recording.recordingUrl;
      var recording_id = recording.recordingId;
      var format = recording.recordingFormat;
      console.log("Downloading recording: " + recording_url);

      var output_file = path.join(recordingsDir, recording_id + "." + format);

      // Download the file
      axios({
        url: recording_url,
        method: 'GET',
        responseType: 'stream',
      }).then(function (response) {
        var fileStream = fs.createWriteStream(output_file);
        response.data.pipe(fileStream);
        fileStream.on('finish', function () {
          console.log("Downloaded file to: " + output_file);
        });
      }).catch(function (error) {
        console.log("Error downloading file: " + error.message);
      });
    });
  }, function (err) {
    console.error(err);
  });
})();

Delete recordings from Plivo storage

You can delete a recording by using the Delete a Recording API and specifying a recording ID, which you can retrieve from list all recordings API or the HTTP callback details stored in your database. You can also delete recordings from the Voice Recordings page of the Plivo console.