<

Getting Started with Recording

Plivo provides you the ability to record both outgoing calls from and incoming calls to Plivo. You can choose to record the entire call or record only specific portions of the call. The recording can be initiated by using either an API or using Plivo XML.

We already know how to make calls and how to handle incoming calls, we will now look at recording an entire call using Plivo XML and using the API.

Recording a call using the API

You can record an ongoing call using the Record API of calls. If you need to record the call from start, call the Record API immediately after the conference is created. It is recommended to use a Record XML element if you are creating your conference using an XML.

Recording a conference using the API

You can record an ongoing call using the Record API of conferences. If you need to record the call from start, call the Record API before rendering the XML on your answer_url.

Recording a call using Plivo XML

To record a call, we just need to add an record element to the XML which is used to control the call flow. This will ensure that the call will be recorded.

For example, consider the following XML.

1
2
3
4
5
6
<Response>
    <Record action ="http://c3bb61b6.ngrok.io/record_action/" redirect="false" startOnDialAnswer="true" maxLength="14400" />
    <Dial callerId="1415xxxxxxxx">
        <Number>number_to_be_called</Number>
    </Dial>
</Response>

Once this XML is successfully fetched from the answer_url it will be executed. The Record element records the call that is being dialled by the Dial tag.

Accessing created recordings

The recording URL will be POSTed to the URL specified in the action attribute of Record XML or the API request as soon as the recording is completed. You can use this URL to access the recording.

You can also retrieve the created recordings at a later point of time in two ways.

  • From Recordings section in the Plivo Dashboard
  • Using our Recordings API Endpoint. This allows you to list and manage all the recordings in your account.

To retrieve recordings, if authentication is disabled on the recording URL (default)

Use the recording URL to fetch the recording file. The recording file is of either a .mp3 or a .wav file format. The recording will be streamed as a response to your request.

Possible error code(s)

  • 403 - Recording does not exist or is deleted.

Example of the recording URL

https://media.plivo.com/v1/Account/{auth_id}/Recording/{recording_id}/

Code Samples

            
curl -X GET \
  https://media.plivo.com/AUTH_ID/Recording/RECORDING_UUID.mp3 \
  -H 'cache-control: no-cache'
            
          
            
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
  .url("https://media.plivo.com/AUTH_ID/Recording/RECORDING_UUID.mp3")
  .get()
  .build();
Response response = client.newCall(request).execute();
FileOutputStream fos = new FileOutputStream("/var/tmp/file.mp3");
fos.write(response.body().bytes());
fos.close();
            
          
            
import requests
url = "https://media.plivo.com/AUTH_ID/Recording/RECORDING_UUID.mp3"
r = requests.get(url, stream=True)
with open(local_filename, 'wb') as f:
    for chunk in r.iter_content(chunk_size=1024):
        if chunk: # filter out keep-alive new chunks
            f.write(chunk)
    return local_filename
            
          
            
<?php
$request = new HttpRequest();
$request->setUrl('https://media.plivo.com/AUTH_ID/Recording/RECORDING_UUID.mp3');
$request->setMethod(HTTP_METH_GET);
try {
  $response = $request->send();
  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}
          
        
          
var request = require("request");
var fs = require('fs');
var url = "https://media.plivo.com/AUTH_ID/Recording/RECORDING_UUID.mp3";
var file = fs.createWriteStream("file.mp3");
var request = http.get(url, function(response) {
    response.pipe(file);
    file.on('finish', function() {
        file.close(cb); // close() is async, call cb after close completes.
        });
    }).on('error', function(err) { // Handle errors
        fs.unlink(dest); // Delete the file async. (But we don't check the result)
        if (cb) cb(err.message);
    });
};
            
          
            
require 'open-uri'
download = open(https://media.plivo.com/AUTH_ID/Recording/RECORDING_UUID.mp3)
IO.copy_stream(download, '~/my_file.mp3')
            
          

To retrieve recordings, if authentication is enabled on the recording URL

Use the recording URL to fetch the recording file. Make sure to use your AUTH ID and AUTH TOKEN for authentication. To access recordings on a sub-account, you must use the sub-account credentials for authentication. Once authenticated, the recording will be streamed as a response to your request.

Possible error codes

  • 401 - Unauthorized. Authentication failed.
  • 403 - Recording does not exist or is deleted.

Code Samples

            
curl -i --user AUTH_ID:AUTH_TOKEN \
https://media.plivo.com/v1/Account/AUTH_ID/Recording/RECORDING.mp3 \
-H 'cache-control: no-cache'
            
          
            
OkHttpClient client = new OkHttpClient();
String authString = AUTH_ID + ":" + AUTH_TOKEN;
String authStringEnc = new BASE64Encoder().encode(authString.getBytes());
Request request = new Request.Builder()

.url("https://media.plivo.com/v1/Account/AUTH_ID/Recording/RECORDING.mp3")
  .get()
  .addHeader("Authorization", "Basic " + authStringEnc)
  .build();

Response response = client.newCall(request).execute();
FileOutputStream fos = new FileOutputStream("sample.mp3");
fos.write(response.body().bytes());
fos.close();
            
          
            
import requests
def download_file():    
  url = "https://media.plivo.com/v1/Account/AUTH_ID/Recording/RECORDING.mp3"
  local_filename = url.split('/')[-1]    
  r = requests.get(url, stream=True, auth=('AUTH_ID', 'AUTH_TOKEN'))
  with open(local_filename, 'wb') as f:
    for chunk in r.iter_content(chunk_size=1024):
      if chunk: # filter out keep-alive new chunks
        f.write(chunk)
  return local_filename
if __name__ == "__main__":
  download_file()
            
          
            
<?PHP
$request = new HttpRequest();
$username = "AUTH_ID";
$password = "AUTH_TOKEN";
$request->setUrl('https://media.plivo.com/v1/Account/AUTH_ID/Recording/RECORDING.mp3');
$request->setMethod(HTTP_METH_GET);
$request->setHeaders(array(
  'header' => "Authorization: Basic " . base64_encode("$username:$password")                 
));
try {
  $response = $request->send();

  if ($r->getResponseCode() == 200) {
    file_put_contents('sample.mp3', $r->getResponseBody());
    }
} catch (HttpException $ex) {
    echo $ex;
}
          
        
          
var fs = require('fs'),
  request = require('request');

request
  .get('https://media.plivo.com/v1/Account/AUTH_ID/Recording/RECORDING.mp3', {
  'auth': {
    'user': 'AUTH_ID',
    'pass': 'AUTH_TOKEN',
  }
})
  .on('error', function(err) {
    // handle error
  })
  .pipe(fs.createWriteStream('sample.mp3'));
            
          
            
require 'open-uri'
  File.open('sample.mp3', "wb") do |file|
    file.write open('https://media.plivo.com/v1/Account/MAMTK2MGFHNTVINWQYZT/Recording/98b8aece-93d6-11e8-9476-06f687d7a22c.mp3',
      :http_basic_authentication => ['MAMTK2MGFHNTVINWQYZT', 'N2FmNzdhMTc2ZmY5MWEyNzhhMDk1YWEwODM4NzIx']).read
    puts 'file is downloaded'
end
            
          

You can enable authentication for accessing the recording URL using Plivo Console.

To enable authentication

  1. Login to your Plivo Console account.
  2. On the top navigation bar, click Voice, and then click Settings.
  3. On the Setting page, click Recordings.
  4. Under the Basic Auth for Recording URLs section, check the Enabled checkbox. Note: By default, the Enabled checkbox is unchecked. Your basic authentication on your recording URL is now enabled.