Mux Data
AnalyticsGetting started with Mux Data API
Mux Data API is a powerful tool for analyzing video performance and gathering insights. In this article, we will explain how you can use Mux Data API with JavaScript and provide some examples of how to use the API.
API documentation
Before we proceed, let's take a look at the Mux Data API documentation here. This documentation provides full details about API endpoints, authentication, parameters and responses.
Setting up the environment
To use Mux Data API in JavaScript, you first need to authenticate and set up your environment. Here is a sample code to set up your environment:
const mux = require('mux-node');
const { Video } = mux;
// Replace with your values.
const accessTokenId = 'YOUR_ACCESS_TOKEN_ID';
const accessTokenSecret = 'YOUR_ACCESS_TOKEN_SECRET';
mux.Video.init({
accessKeyId: accessTokenId,
secretAccessKey: accessTokenSecret
});
Retrieving video views
You can use the Mux Data API to retrieve video views for a given period of time. Here is an example code that fetches video views for the past 7 days:
const startDate = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
const endDate = new Date();
Video.Views.list('VIDEO_ID', {
start_time: startDate.toISOString(),
end_time: endDate.toISOString()
}).then((views) => {
console.log(views);
}).catch((error) => {
console.error(error);
});
Make sure to replace VIDEO_ID
with the ID of the video you want to fetch the views for.
Retrieving player errors
You can use the Mux Data API to retrieve player errors for a given period of time. Here is an example code that fetches player errors for the past 24 hours:
const startDate = new Date(Date.now() - 24 * 60 * 60 * 1000);
const endDate = new Date();
Video.Errors.list('VIDEO_ID', {
start_time: startDate.toISOString(),
end_time: endDate.toISOString()
}).then((errors) => {
console.log(errors);
}).catch((error) => {
console.error(error);
});
Make sure to replace VIDEO_ID
with the ID of the video you want to fetch the errors for.
Conclusion
In this article, we learned how to use the Mux Data API with JavaScript. We covered setting up the environment and provided examples of how to retrieve video views and player errors. Make sure to check out the Mux Data API documentation for further details and endpoints.