Skip to main content

Obtaining call recordings

· 3 min read
Viacheslav Tkachenko
Viacheslav Tkachenko
Software Engineer

This guide will walk you through creating a task to download items using an API and retrieving the task status every N seconds.

Prerequisites​

  • Ensure that Node.js is installed (v18+ includes the native fetch API).
  • You have access to the Wildix API endpoints.

Step 1: Create a Task to Download Recordings​

To start downloading items, you need to create a task using the createDownloadRecordingsTask API endpoint.

API Documentation​

Refer to the official Create Download Recordings Task API documentation.

Example Code​

The following code demonstrates how to use the fetch function to create a download task.

// Replace these with your actual API credentials
const PBX = 'https://{pbx}.wildixin.com';
const API_TOKEN = 'your-api-token';

async function createDownloadTask() {
const url = `${PBX}/api/v1/PBX/recordings/download/`;

const requestBody = {
"from": 1727867140,
"to": 1727867140
};

const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${API_TOKEN}`
},
body: JSON.stringify(requestBody)
});

if (!response.ok) {
throw new Error(`Failed to create task: ${response.statusText}`);
}

const data = await response.json();
console.log('Task Created:', data);
return data; // Return task ID or other necessary information
}

// Create the download task
createDownloadTask().catch(console.error);

Expected Response​

Upon a successful task creation, you should receive a response with details about the task, such as the task ID, which you will use in the next step.


Step 2: Retrieve Task Status Every N Seconds​

Once the task is created, you can check its status using the getStatusRecordingsTask API endpoint. You need to poll the API every N seconds to check the task's completion status.

API Documentation​

Refer to the official Get Status of Recordings Task API documentation.

Example Code​

Here’s an example of how to check the task status repeatedly until it's completed.

const TASK_ID = 'your-task-id'; // Replace with the actual task ID from the createDownloadTask response

async function getTaskStatus(taskId) {
const url = `${PBX}/api/v1/PBX/recordings/download/${taskId}`;

const response = await fetch(url, {
method: 'GET',
headers: {
'Authorization': `Bearer ${API_TOKEN}`
}
});

if (!response.ok) {
throw new Error(`Failed to retrieve task status: ${response.statusText}`);
}

const data = await response.json();
console.log('Task Status:', data);
return data;
}

// Polling function to check task status every N seconds
async function pollTaskStatus(taskId, intervalSeconds) {
const interval = intervalSeconds * 1000;

const poll = setInterval(async () => {
const statusData = await getTaskStatus(taskId);

// Assuming the API returns a status field, modify as per actual API response
if (statusData.result.status === 'success') {
console.log('Task completed successfully');
clearInterval(poll);
} else if (statusData.result.status === 'error') {
console.log('Task failed');
clearInterval(poll);
} else {
console.log('Task is still in progress...');
}
}, interval);
}

// Poll the task status every 5 seconds
pollTaskStatus(TASK_ID, 5).catch(console.error);

Notes​

  • In the polling function, the status of the task is checked every 5 seconds. You can adjust the polling interval by changing the intervalSeconds value.
  • The task is considered completed or failed based on the status field in the response. Adjust the conditions based on the actual response from the API.