Getting Started with Mux Public API

Mux Public API is a powerful tool that makes it easy for developers to manage video content on their websites and applications. In this blog post, you'll learn everything you need to get started using Mux Public API, including a rundown of the different endpoints and some example code in JavaScript.

Endpoints

Mux Public API has several different endpoints that you can use to manage your video content. Some of the most important ones are:

  • Video: This endpoint allows you to upload, manage, and delete videos. You can also use it to get information about videos, such as their duration and resolution.

  • Playback IDs: This endpoint allows you to create and manage playback IDs, which are unique identifiers that allow you to embed videos in your website or application.

  • Live Streams: This endpoint allows you to create and manage live streams, which are video streams that are broadcast in real-time.

Example Code in JavaScript

Here are some example code snippets in JavaScript that demonstrate how to use some of the endpoints in Mux Public API:

Uploading a Video

To upload a video using Mux Public API, you can use the following code:

const fetch = require('node-fetch');

const API_KEY = 'your-api-key';
const API_SECRET = 'your-api-secret';

const endpoint = 'https://api.mux.com/video/v1/assets';

const file_url = 'https://example.com/video.mp4';
const file_name = 'video.mp4';

const data = {
  input: file_url,
  playback_policy: 'public',
  mp4_support: 'standard',
  passthrough: { original_filename: file_name },
};

fetch(endpoint, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    Authorization: `Basic ${Buffer.from(
      `${API_KEY}:${API_SECRET}`
    ).toString('base64')}`,
  },
  body: JSON.stringify(data),
})
  .then((response) => response.json())
  .then((data) => console.log(data));

Creating a Playback ID

To create a playback ID for a video, you can use the following code:

const fetch = require('node-fetch');

const API_KEY = 'your-api-key';
const API_SECRET = 'your-api-secret';

const asset_id = 'your-asset-id';

const endpoint = `https://api.mux.com/video/v1/assets/${asset_id}/playback-ids`;

const data = {
  policy: 'public',
  new_asset_settings: {
    passthrough: { asset_id: asset_id },
  },
};

fetch(endpoint, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    Authorization: `Basic ${Buffer.from(
      `${API_KEY}:${API_SECRET}`
    ).toString('base64')}`,
  },
  body: JSON.stringify(data),
})
  .then((response) => response.json())
  .then((data) => console.log(data));

Creating a Live Stream

To create a live stream using Mux Public API, you can use the following code:

const fetch = require('node-fetch');

const API_KEY = 'your-api-key';
const API_SECRET = 'your-api-secret';

const endpoint = 'https://api.mux.com/video/v1/live-streams';

const data = {
  playback_policy: 'public',
  new_asset_settings: {
    playback_policy: 'public',
    passthrough: {
      "name": "My Live Stream"
    },
  },
};

fetch(endpoint, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    Authorization: `Basic ${Buffer.from(
      `${API_KEY}:${API_SECRET}`
    ).toString('base64')}`,
  },
  body: JSON.stringify(data),
})
  .then((response) => response.json())
  .then((data) => console.log(data));

Conclusion

Mux Public API is a powerful tool that can help you manage your video content with ease. With its various endpoints and easy-to-use interface, you can quickly upload, manage, and delete videos, create playback IDs, and even broadcast live streams. With the example code provided in this blog post, you can get started using Mux Public API in no time!

Related APIs

Public APIs — A directory of free and public apis

Built by @mddanishyusuf