Videos on a Page

Learn how to upload videos and then fetch uploaded videos on a page.

Videos

Videos
Videos

The video feature on Facebook allows public figures, businesses, and organizations to share video content with their viewers/followers. Page owners can use videos to share announcements, updates, promotions, or other types of content. Videos can be created and managed by page admins and editors with the appropriate permissions. These videos are interactive; users can like, comment, and share them, and they are available on the page's feed once published. Facebook's functionality enhances user engagement by allowing users to watch videos without leaving the platform, autoplaying recommended videos, and other interactive elements, allowing pages to attract more users.

The base URL for this endpoint is:

https://graph.facebook.com/v16.0/{page_id}/videos

Request parameters

The request parameters for the above endpoint are given below.

Parameter

Type

Category

Description

access_token

String

Mandatory

This is the token that we received in response after making the call to the accounts edge of the `user` object.

fields

String

Optional

These are the fields we want in the response from the API call.

page_id

String

Mandatory

This is the page's ID. Note that this is a path parameter.

title

String

Optional

This is the title of the video.

description

String

Optional

This is the description of the video.

video_file

String

Mandatory

This is the video file. This can be either a file path or a binary file.

To publish a new video on the page using the Facebook Graph API, we can make a POST request to the video edge of the page object using the page's ID and a page access token that has the publish_video permission. The code below uses the endpoint above to publish a new video on the page. Click the “Run” button to see the response.

Press + to interact
// Importing libraries here
import fetch from "node-fetch";
import fs from 'fs';
import {FormData, Blob} from "formdata-node"
// Define endpoint URL here
const endpointUrl = new URL("https://graph.facebook.com/v16.0/{{PAGE_ID}}/videos");
const headerParameters = {
contentType: "application/json",
};
// Video Path
const videoFile = fs.readFileSync('Welcome_to_Educative.mp4');
// Define Query Parameters here
const queryParameters = new URLSearchParams({
access_token: '{{PAGE_ACCESS_TOKEN}}',
});
const body = new FormData();
body.append('description', 'This is a sample video');
body.append('video_file', new Blob([videoFile]), 'video.mp4');
// Setting API call options
const options = {
method: "POST",
headers: headerParameters,
body: body
};
// Function to make API call
async function postVideoOnPage() {
try {
endpointUrl.search = queryParameters;
const response = await fetch(endpointUrl, options);
// Printing response
printResponse(response);
} catch (error) {
// Printing error message
printError(error);
}
}
// Calling function to make API call
postVideoOnPage();

In the code widget above:

  • Line 7: We define the endpoint URL in the endpointUrl variable.

  • Line 14: We read the video file using the readFileSync function of the fs library.

  • Lines 17–19: We add the access_token in the queryParameters variable.

  • Lines 21–23: We create a FormData and append the description and video_file to it.

  • Line 36: We use the fetch function to make the API call.

Response fields

The response fields for the above endpoint are given below.

Name

Type

Description

id

String

This is the ID of the video.

description

String

This is the description of the video.

from

Object

This is the object containing the ID and name of the user or page that posted the video.

title

String

This is the title of the video.

source

String

This is the video file's URL.

created_time

String

This is the time the video was created. The time is in ISO 8601 format.

updated_time

String

This is the time the video was updated. The time is in ISO 8601 format.

Note: To get videos uploaded on a page, remove the body and change the method to GET in the options variable.