Skip to main content

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.
// 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);
  });
})();

Streaming Plivo recording files to custom S3 bucket

Here’s sample code you can use to stream your Plivo recording files to a custom S3 buket.
// Example script for downloading recording files

const plivo = require('plivo');
const axios = require('axios');
const { S3Client } = require('@aws-sdk/client-s3');
const { Upload } = require('@aws-sdk/lib-storage');

// ======= CONFIG =======
const AUTH_ID = process.env.PLIVO_AUTH_ID || "<auth_id>";
const AUTH_TOKEN = process.env.PLIVO_AUTH_TOKEN || "<auth_token>";
const AWS_REGION = process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION || "ap-south-1";
const S3_BUCKET = process.env.S3_BUCKET || "your-bucket";
const S3_PREFIX = process.env.S3_PREFIX || "plivo/recordings"; // optional folder prefix
const USE_SSE = process.env.S3_SSE !== "false"; // AES256 by default
// ======================

(async function main() {
  'use strict';

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

  const s3 = new S3Client({
    region: AWS_REGION,
    // (Optional) customize retries/timeouts if you want:
    // requestHandler: new NodeHttpHandler({ connectionTimeout: 30000, socketTimeout: 120000 })
  });

  try {
    const recordings = await 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,
    });

    console.log(`Found ${recordings.length} recordings.`);

    for (const rec of recordings) {
      const recordingUrl = rec.recordingUrl;
      const recordingId = rec.recordingId;
      const format = rec.recordingFormat || 'mp3'; // fallback

      const key = `${S3_PREFIX}/${recordingId}.${format}`;
      console.log(`Uploading ${recordingUrl} -> s3://${S3_BUCKET}/${key}`);

      try {
        // Stream download from Plivo
        const resp = await axios({
          url: recordingUrl,
          method: 'GET',
          responseType: 'stream',
          timeout: 60_000,
          // If the URL requires auth headers/cookies, add here
        });

        const contentType = resp.headers['content-type'] || 'application/octet-stream';
        const contentDisposition = resp.headers['content-disposition'];

        // Multipart upload to S3 (handles large files & retries)
        const uploader = new Upload({
          client: s3,
          params: {
            Bucket: S3_BUCKET,
            Key: key,
            Body: resp.data, // streaming body
            ContentType: contentType,
            ...(contentDisposition ? { ContentDisposition: contentDisposition } : {}),
            ...(USE_SSE ? { ServerSideEncryption: 'AES256' } : {}),
            StorageClass: 'STANDARD',
          },
          // Optional concurrency/tuning:
          queueSize: 4,       // concurrent parts
          partSize: 8 * 1024 * 1024, // 8MB
          leavePartsOnError: false,
        });

        await uploader.done();
        console.log(`Uploaded: s3://${S3_BUCKET}/${key}`);
      } catch (err) {
        console.error(`Failed for ${recordingId}: ${err.message}`);
      }
    }
  } catch (err) {
    console.error(err);
    process.exit(1);
  }
})();

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.
I