Download Recordings Using .NET

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 .NET 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
60
61
62
63
64
65
66
/**
 * Example script for downloading recording files
 */
using System;
using System.Collections.Generic;
using Plivo;
using Plivo.Exception;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;

namespace PlivoExamples
{
    internal class Program
    {
        private const string AUTH_ID = "<auth_id>";
        private const string AUTH_TOKEN = "<auth_token>";
        public static async Task Main(string[] args)
        {
            var api = new PlivoApi(AUTH_ID,AUTH_TOKEN);
            try
            {
                var response = api.Recording.List(
                    addTime_Gt: DateTime.Parse("2023-04-01 00:00:00"),
                    addTime_Lt: DateTime.Parse("2023-04-30 00:00:00"),
                    limit:5,
                    offset:0
                );

                Console.WriteLine($"Found {response.Objects.Count} recordings.");

                // Directory where the recordings will be saved
                string dir = "./recordings";

                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }

                foreach (var recording in response.Objects)
                {
                    string recordingUrl = recording.RecordingUrl;
                    string recordingId = recording.RecordingId;
                    string format = recording.RecordingFormat;

                    Console.WriteLine("Downloading recording: " + recordingUrl);

                    string outputFilePath = Path.Combine(dir, recordingId + "." + format);

                    // Download the file
                    using (var httpClient = new HttpClient())
                    {
                        var fileBytes = await httpClient.GetByteArrayAsync(recordingUrl);
                        await File.WriteAllBytesAsync(outputFilePath, fileBytes);
                    }

                    Console.WriteLine("Downloaded file to: " + outputFilePath);
                }
            }
            catch (PlivoRestException e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
        }
    }
}

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.