Login

Node.js SDK

FastPix Node.js SDK, written in TypeScript, provides an easy way to integrate with the FastPix platform. It enables communication with the FastPix APIs, allowing you to manage media uploads, live streaming, and simulcasting to multiple platforms.


Key features:

The FastPix Node.js SDK provides the following features:

1. Media APIs:

The Media API allows you to upload and manage media assets on the FastPix platform.

  • Upload media: Easily upload media files from URLs or devices to the FastPix platform.
  • Manage media: Perform various media operations such as listing, fetching, updating, and deleting media assets.
  • Playback IDs: Create and manage playback IDs to access your media.

2. Live streaming APIs:

  • Create & manage live streams: Stream live events by creating, listing, updating, and deleting live streams.
  • Control stream access: Generate playback IDs to control who can access your live streams.
  • Simulcast: Stream content to multiple platforms simultaneously with ease.

For more detailed information, refer to the FastPix API Reference.


Prerequisites

Before you start using the FastPix Node.js SDK, make sure you have the following:

  1. To authenticate with the FastPix APIs, you'll need an Access Token and a Secret Key. You can generate these credentials by following the steps in the Authentication with Access Tokens guide.
  2. Node.js (version >= 18): This SDK is compatible with Node.js version 18 or higher on the server side.

Step 1: Installation

To install the SDK, simply use NPM or your preferred package manager. Run the following command:

npm install @fastpix/fastpix-node

Step 2: Import the SDK

You can import the SDK using either ES Modules (import) or CommonJS (require), depending on your project configuration.

Using import (ES Modules):

If your project supports ES Modules, you can import the SDK like this:

import Client from "@fastpix/fastpix-node"; 

Using require (CommonJS):

If you're working with CommonJS modules, use require:

const Client = require("@fastpix/fastpix-node").default; 

Step 3: Initialization

Initialize FastPix Node.js SDK with AccessToken and SecretKey.

import Client from "@fastpix/fastpix-node";

const fastpix = new Client({
  accessTokenId: "your-access-token-id",
  secretKey: "your-secret-key",
});

Example usage:

// Using import (ES Modules)

import Client from "@fastpix/fastpix-node";

// or using require (CommonJS)
// const FastPix = require("@fastpix/fastpix-node").default;
// Initialize the FastPix SDK with your Access Token ID and Secret Key

const fastpix = new Client({
  accessTokenId: "your-access-token-id", // Replace with your Access Token ID
  secretKey: "your-secret-key", // Replace with your Secret Key
});

async function main() {

  // Create a request payload for uploading media from a URL
  const uploadUrlRequest = {
    inputs: [
      {
        type: "video",
        url: "https://static.fastpix.io/sample.mp4",
      },
    ],
    metadata: {
      video_title: "Big_Buck_Bunny",
    },

    accessPolicy: "public",
  };

  const response = await fastpix.uploadMediaFromUrl(uploadUrlRequest);
  console.log("Media Id:", response.data.id);
}

main();



1. Media operations (Video on demand)


1.1. Media uploads to FastPix

Upload media from URL:

Use the uploadMediaFromUrl method to upload media directly from a URL. For detailed configuration operation, refer to the Create media from URL API documentation.


// Define the request payload for uploading media from a URL.

const mediaFromUrlRequest = {
  inputs: [
    {
      type: "video", // Specifies the type of media being uploaded (e.g., "video").
      url: "https://static.fastpix.io/sample.mp4", // URL of the media file to be uploaded.
    },
  ],

  metadata: {
    video_title: "Big_Buck_Bunny", // Metadata to associate with the media, such as its title.
  },
  accessPolicy: "public", // Access policy for the media ("public" or "private").
};

const mediaFromUrlResponse =
  await fastpix.uploadMediaFromUrl(mediaFromUrlRequest);

console.log("Upload Response:", mediaFromUrlResponse);

Upload media directly from local device:

Use the uploadMediaFromDevice method to fetch the signedUrl and to make use of it to upload media directly from a local device via PUT HTTP Method. For detailed configuration options, refer to the Upload media from device API documentation.


// Define the request payload for uploading media from a device.

const mediaFromDeviceRequest = {
  corsOrigin: "*", // Specifies the allowed origin for CORS (Cross-Origin Resource Sharing). "*" allows all origins.

  pushMediaSettings: {
    accessPolicy: "private", // Sets the access policy for the uploaded media (e.g., "private" or "public").
    optimizeAudio: true, // Enables audio optimization for the uploaded media.
    maxResolution: "1080p", // Specifies the maximum resolution allowed for the uploaded media.
  },
};

const mediaFromDeviceResponse = await fastpix.uploadMediaFromDevice(
  mediaFromDeviceRequest,
);

console.log("Upload Response:", mediaFromDeviceResponse);

1.2. Media management

Get list of all media:

Use getAllMediaAssets to fetch a list of all media assets. You can customize the query by modifying parameters such as limit, offset and orderBy. For more details on listing media assets, refer to the Get list of all media API documentation.


// Define the parameters for fetching media assets in a separate variable.

const mediaQueryParams = {
  limit: 20, // Number of assets to fetch in one request (between 1 and 50)
  offset: 5, // Pagination starting position
  orderBy: "asc", // Sorting order of the assets ("asc" for ascending)
};

const mediaAssets = await fastpix.getAllMediaAssets(mediaQueryParams);

console.log("Fetched Media Assets:", mediaAssets);

Get media asset by ID:

Use the getMediaAssetById method to retrieve a media asset by its ID. Simply provide the mediaId of the media asset to fetch its details. For more information, refer to the Get a media by ID API documentation.


// Define the parameter for fetching a specific media asset by ID.
const mediaAssetRequestParams = {
  mediaId: "mediaId", // Unique identifier for the media asset to be retrieved.
};

const getMediaAsset = await fastpix.getMediaAssetById(mediaAssetRequestParams);
console.log("Retrieved media asset by ID:", getMediaAsset);

Update media asset:

Use the updateMediaAsset method to modify the metadata or other properties of a specific media asset. Provide the mediaId of the asset and the updated metadata. For more information, refer to the Update a media by ID API documentation.


// Define the parameter for specifying the media asset to be updated.

const mediaAssetToUpdate = {
  mediaId: "media-id", // Unique identifier for the media asset to update.
};

// Define the payload with the updates to be applied to the media asset.

const updatePayload = {
  metadata: {
    key: "value", // Replace "key" and "value" with actual metadata entries.
    category: "nature", // Example of another metadata entry.
  },
};

const updateMediaAsset = await fastpix.updateMediaAsset(
  mediaAssetToUpdate,
  updatePayload,
);

console.log("Updated Media Asset:", updateMediaAsset);

Delete media asset:

Use the deleteMediaAsset method to remove a media asset by its ID. Simply provide the mediaId of the asset you wish to delete. For further details, refer to the Delete a media by ID API documentation.


// Define the parameter for specifying the media asset to be deleted.
const mediaAssetToDelete = {
  mediaId: "media-id", // Unique identifier for the media asset to delete.
};

const deleteMediaAsset = await fastpix.deleteMediaAsset(mediaAssetToDelete);
console.log("Deleted Media Asset:", deleteMediaAsset);

1.3. Manage media playback

Generate media playback ID:

Use the generateMediaPlaybackId method to create a playback ID for a specific media asset. Provide the mediaId and configure options like the accessPolicy. For detailed configuration options, refer to the Create a playback ID API documentation.


// Define the mediaId and accessPolicy dynamically

const mediaPlaybackRequest = {
  mediaId: "media-id", // Unique identifier for the media asset.
};

const playbackOptions = {
  accessPolicy: "public", // Can be 'public' or 'private'.
};

const playbackIdResponse = await fastpix.generateMediaPlaybackId(
  mediaPlaybackRequest, // Pass the mediaId
  playbackOptions, // Pass the accessPolicy
);

console.log("Playback ID Creation Response:", playbackIdResponse);

Delete media playback ID:

Use the deleteMediaPlaybackId method to remove a playback ID for a specific media asset. You must provide both the mediaId and the playbackId to delete it. For detailed configuration options, refer to the Delete a playback ID API documentation.




2. Live stream operations

2.1 Start live stream

Use the initiateLiveStream method to start a live stream with your desired configurations. For detailed configuration options, refer to the Create a new stream API documentation.


const liveStreamRequest = {
  playbackSettings: {
    accessPolicy: "public", // Defines the access level of the live stream (public or private)
  },

  inputMediaSettings: {
    maxResolution: "1080p", // Set the maximum resolution of the live stream
    reconnectWindow: 1800, // Set the duration for reconnecting the stream in seconds
    mediaPolicy: "private", // Define media policy (private or public)
    metadata: {
      liveStream: "fp_livestream", // Custom metadata for the live stream
    },
    enableDvrMode: true, // Enable DVR mode to allow viewers to rewind the live stream
  },
};

// Initiating the live stream
const generateLiveStream = await fastpix.initiateLiveStream(liveStreamRequest);
console.log("Live Stream initiated successfully:", generateLiveStream);

2.2 Live stream management

Get list of all live streams:

Use the getAllLiveStreams method to retrieve a list of all live streams. Customize the query by adjusting parameters such as limit, offset, and orderBy. For detailed configuration options, refer to the Get all live streams API documentation.


const getAllLiveStreamPagination = {
  limit: 10, // Limit the number of live streams retrieved.
  offset: 1, // Skip a specified number of streams for pagination.
  orderBy: "asc", // Order the results based on the specified criteria ("asc" or "desc").
};

const getAllLiveStreams = await fastpix.getAllLiveStreams(
  getAllLiveStreamPagination,
);
console.log("All Live Streams:", getAllLiveStreams);

Get live stream by Id:

Use the getLiveStreamById method to fetch a specific live stream by its ID. Provide the streamId of the stream you want to retrieve. For more details, refer to the Get stream by ID API documentation.


const getLiveStreamById = await fastpix.getLiveStreamById({ 

streamId: "a09f3e958c16ed00e85bfe798abd9845", // Replace with actual stream ID 
}); 

console.log("Live Stream Details:", getLiveStreamById); 

Update live stream:

Use the updateLiveStream method to modify the configuration of a live stream. Provide the streamId and specify the fields you wish to update. For more details, refer to the Update Live Stream API documentation.


const updateLiveStreamRequest = {
  metadata: {
    livestream_name: "Game_streaming",
  },
  reconnectWindow: 100,
};

const updateLiveStream = await fastpix.updateLiveStream(
  { streamId: "a09f3e958c16ed00e85bfe798abd9845" }, // Provide the stream ID for the live stream to update

  updateLiveStreamRequest,
);

console.log("Updated Live Stream:", updateLiveStream);

Delete live stream:

Use the deleteLiveStream method to remove a live stream. Provide the streamId of the stream you want to delete. For more details, refer to the Delete Live Stream API documentation.


const deleteLiveStream = await fastpix.deleteLiveStream({ 
	streamId: "a09f3e958c16ed00e85bfe798abd9845", // Provide the stream ID of the live stream to delete 
}); 

console.log("Deleted Live Stream:", deleteLiveStream); 

2.3. Manage live stream playback

Generate live stream playback ID:

Use the generateLiveStreamPlaybackId method to create a playback ID for a live stream. Replace streamId with the actual ID of the stream and specify the desired accessPolicy. For more details, refer to the Create a playback ID API documentation.


const generateLiveStreamPlaybackId = await fastpix.generateLiveStreamPlaybackId(
  { streamId: "a09f3e958c16ed00e85bfe798abd9845" }, // Pass the stream ID for which the playback ID is to be generated

  { accessPolicy: "public" }, // This can be "public" or "private" based on your needs
);

console.log("Generated Live Stream Playback ID:", generateLiveStreamPlaybackId);

Delete live stream playback ID:

Use the deleteLiveStreamPlaybackId method to remove a specific playback ID for a live stream. Provide both the streamId of the stream and the playbackId you wish to delete. For more details, refer to the Delete a playback ID API documentation.


const deleteLiveStreamPlaybackId = await fastpix.deleteLiveStreamPlaybackId({
  streamId: "a09f3e958c16ed00e85bfe798abd9845", // Replace with actual stream ID
  playbackId: "632029b4-7c53-4dcf-a4d3-1884c29e90f8", // Replace with actual playback ID
});

console.log("Deleted Live Stream Playback ID:", deleteLiveStreamPlaybackId);

Get live stream playback policy:

Use the getLiveStreamPlaybackPolicy method to retrieve the playback policy for a specific live stream playback ID. Provide the streamId and playbackId to fetch the policy details. For more information, refer to the Get stream's playback ID API documentation.


2.4. Simulcast live stream

Initiate live stream simulcast:

Use the initiateLiveStreamSimulcast method to create a new simulcast for a live stream. Provide the streamId along with the simulcast payload, including the URL and stream key. For more details, refer to the Create a simulcast API documentation.


const simulcastPayload = {
  url: "rtmps://live.fastpix.io:443/live",
  streamKey:
    "46c3457fa8a579b2d4da64125a2b6e83ka09f3e958c16ed00e85bfe798abd9845", // Replace with actual stream key
};

const generateSimulcast = await fastpix.initiateLiveStreamSimulcast(
  {
    streamId: "a09f3e958c16ed00e85bfe798abd9845", // Replace with actual stream ID
  },
  simulcastPayload,
);

console.log("Generate Simulcast:", generateSimulcast);

Get live stream simulcast:

Use the getLiveStreamSimulcast method to retrieve details of a specific simulcast for a live stream. Provide the streamId and simulcastId of the simulcast you wish to fetch. For more details, refer to the Get a specific simulcast of a stream API documentation.


const getLiveSimulcast = await fastpix.getLiveStreamSimulcast({
  streamId: "a09f3e958c16ed00e85bfe798abd9845", // Replace with actual stream ID
  simulcastId: "7269209ff0299319b6321c9a6e7850ff", // Replace with actual simulcast ID
});

console.log("Live Stream Simulcast Details:", getLiveSimulcast);

Update live stream simulcast:

Use the updateLiveStreamSimulcast method to modify the configuration of a simulcast stream. Provide the streamId, simulcastId, and the fields you wish to update. For more details, refer to the Update a specific simulcast of a stream API documentation.


const updateLiveSimulcast = await fastpix.updateLiveStreamSimulcast(
  {
    streamId: "a09f3e958c16ed00e85bfe798abd9845", // Replace with actual stream ID
    simulcastId: "7269209ff0299319b6321c9a6e7850ff", // Replace with actual simulcast ID
  },

  {
    isEnabled: false, // Disable the simulcast stream (set to true to enable)
    metadata: {
      simulcast2: "media", // Update the metadata as needed
    },
  },
);

console.log("Updated Live Stream Simulcast:", updateLiveSimulcast);

Delete live stream simulcast:

Use the deleteLiveStreamSimulcast method to remove a specific simulcast from a live stream. Provide the streamId and simulcastId of the simulcast you wish to delete. For more details, refer to the Delete a simulcast API documentation.


const deleteLiveSimulcast = await fastpix.deleteLiveStreamSimulcast({
  streamId: "a09f3e958c16ed00e85bfe798abd9845", // Replace with actual stream ID
  simulcastId: "7269209ff0299319b6321c9a6e7850ff", // Replace with actual simulcast ID
});

console.log("Deleted Live Stream Simulcast:", deleteLiveSimulcast);