Rate and Comment on a Video

Learn how to comment and rate a video using Youtube Data API.

Commenting on and rating a video is a good way to increase user engagement. It’s also a good way for the content creator to estimate whether their content is making any impact on their audience or not.

In this lesson, we’ll learn to interact with a video by rating or commenting on it with the help of YouTube Data API.

Rate a video

A user can use the rate method to post their rating about the video by sending a POST request to the following URL:

https://www.googleapis.com/youtube/v3/videos/rate

Here are the two parameters that we can use to call the endpoint:

Request parameters

Name

Type

Category

Description

id

String

Required

Specifies the video user wants to rate.

rating

String

Required

Specifies the rating user wants to give to the video. The possible values are like, dislike, and none.


Let’s give a thumbs-up to our video on the channel. Click the “Run” button to execute the following code:

Press + to interact
// Defining import libraries here
import fetch from 'node-fetch';
// Define endpoint URL here
const endpointUrl = new URL('https://www.googleapis.com/youtube/v3/videos/rate');
// Define Header Parameters here
const headerParameters = {
Authorization: 'Bearer {{ACCESS_TOKEN}}',
Accept: 'application/json',
};
// Define Query Parameters here
const bodyParameters = JSON.stringify({
id: '{{VIDEO_ID}}',
rating: 'like',
});
// Setting API call options
const options = {
method: 'POST',
headers: headerParameters,
body: bodyParameters,
};
// Function to make API call
async function rateVideo() {
try {
const response = await fetch(endpointUrl, options);
// Custom function for printing the API response
printResponse(response.status);
} catch (error) {
// Custom function for printing the error message
printError(error);
}
}
// Calling function to make API call
rateVideo();
  • Lines 13–16: We define the bodyParameters variable in which we define two parameters: id and rating. The id parameter is used to define the ID of the video that the user wants to rate and the rating parameter allows the user to pass their rating.

  • Line 24: We define an async function with the name of rateVideo() to call the endpoint.

In the case of successful execution of the above code, it will return the response code 204.

Comment on a video

In order to post a comment, we use the insert method of the CommentThreads resource, which will be used to create the top-level comment on a video.

Press + to interact

We'll send the POST request to the following URL to post a comment on a video:

https://www.googleapis.com/youtube/v3/commentThreads

Request parameters

Name

Type

Category

Description

part

String

Required

This parameter serves two functions. It specifies the snippet properties that will be set by the user, as well as the properties that will be included in the API response, which are id, snippet, and replies.

snippet.channelId

String

Required

Used to provide the channel ID.

snippet.topLevelComment.snippet.textOriginal

String

Required

Used to provide the comment user wants to post.

snippet.videoId

String

Optional

Used to provide the video ID.

We need to add our comment text in the snippet.topLevelComment.snippet.textOriginal parameter in the below widget.

Click the “Run” button on the below widget to add a comment to the video.

Press + to interact
// Defining import libraries here
import fetch from 'node-fetch';
// Define endpoint URL here
const endpointUrl = new URL('https://www.googleapis.com/youtube/v3/commentThreads');
// Define Header Parameters here
const headerParameters = {
Authorization: 'Bearer {{ACCESS_TOKEN}}',
Accept: 'application/json',
};
// Define Query Parameters here
const queryParameters = new URLSearchParams({
part: 'snippet',
});
// Define Body Parameters here
const bodyParameters = JSON.stringify({
snippet: {
channelId: '{{CHANNEL_ID}}',
videoId: '{{VIDEO_ID}}',
topLevelComment: {
snippet: {
textOriginal: 'This is a sample comment',
},
},
},
});
// Setting API call options
const options = {
method: 'POST',
headers: headerParameters,
body: bodyParameters,
};
// Function to make API call
async function postComment() {
try {
endpointUrl.search = queryParameters;
const response = await fetch(endpointUrl, options);
// Custom function for printing the API response
printResponse(response);
} catch (error) {
// Custom function for printing the error message
printError(error);
}
}
// Calling function to make API call
postComment();

Let’s understand the above program by breaking it down:

  • Lines 8–11: We initialize the headerParameters variable in which we pass the required headers.

  • Lines 17–27: We define the snippet.channelId and snippet.videoId in bodyParameters. Additionally, we pass our comment in snippet.topLevelComment.snippet.textOriginal.

  • Line 37: We define an async function named postComment() to post a comment on the video.

The following table contains some of the important response fields:

Response fields

Name

Type

Description

kind

String

Reflects the resource type of the API call.

etag

ETag

Contains the resource ETag.

id

String

Contains the comment ID.

snippet

Object

Contains the comment thread details.

List the comments

We can also list all the comments on the particular video or channel by sending a GET request to the following URL:

https://www.googleapis.com/youtube/v3/commentThreads

Here are some parameters that we can use to call the endpoint:

Request parameters

Name

Type

Category

Description

part

String

Required

This parameter specifies a comma-separated list of one or more resource properties to be included in the API response. These are id, snippet, and replies.

channelId

String

Optional

Used to filter the comment threads that are about a specific channel.

id

String

Optional

Specifies comma-separated IDs of the comment threads to list thier properties in the API response.

videoId

String

Optional

Filters the comment threads of a specific video.

allThreadsRelatedToChannelId

String

Optional

Filters all the comment threads of a specific channel.

maxResult

Integer

Optional

Fixes the number of results that will be fetched.

pageToken

String

Optional

Specifies the page in the response.

Note: In order to call this endpoint, you have to define one of the parameters from videoId, channelId, id, or allThreadsRelatedToChannelId.

Click the “Run” button to list the comments on the videos.

Press + to interact
// Defining import libraries here
import fetch from 'node-fetch';
// Define endpoint URL here
const endpointUrl = new URL('https://www.googleapis.com/youtube/v3/commentThreads');
// Define Header Parameters here
const headerParameters = {
Authorization: 'Bearer {{ACCESS_TOKEN}}',
Accept: 'application/json',
};
// Define Query Parameters here
const queryParameters = new URLSearchParams({
part: 'snippet,replies',
videoId: '{{VIDEO_ID}}',
});
// Setting API call options
const options = {
method: 'GET',
headers: headerParameters,
};
// Function to make API call
async function listComments() {
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
listComments();

Let’s understand the above code by breaking it down:

  • Lines 14–17: We define the queryParameters in which we define the part and videoId parameters.

  • Line 26: We define an async function as listComment() to retrieve comments from the video.

The following table contains some of the important response fields:

Response fields

Name

Type

Description

kind

String

Reflects the resource type of the API call.

etag

ETag

Contains the resource ETag.

nextPageToken

String

Used to fetch the next page in the response result.

textDisplay

String

Contains the comment text.

prevPageToken

String

Used to fetch the previous page in the response result.

pageInfo

Object

Contains the response page information.

items[]

List

Contains the list of the result that matches the request requirement.

Delete a comment

In this part of the lesson, we’ll use the delete method of the Comments resource to delete the comment by sending the DELETE request to the following URL:

https://www.googleapis.com/youtube/v3/comments

In order to delete a comment on a video, we only need the ID of the comment that we want to delete. Let’s delete the comment we posted on our video earlier. Click the “Run” button to delete a comment.

Press + to interact
// Importing libraries here
import fetch from 'node-fetch';
// Define endpoint URL here
const endpointUrl = new URL('https://www.googleapis.com/youtube/v3/comments');
// Define Header Parameters here
const headerParameters = {
Authorization: 'Bearer {{ACCESS_TOKEN}}',
Accept: 'application/json',
};
const queryParameters = new URLSearchParams({
id: '{{COMMENT_ID}}',
});
// Setting API call options
const options = {
method: 'DELETE',
headers: headerParameters,
};
// Function to make API call
async function deleteComment() {
try {
endpointUrl.search = queryParameters;
const response = await fetch(endpointUrl, options);
// Custom function for printing the API response
printResponse(`${response.status}:${response.statusText}`);
} catch (error) {
// Custom function for printing the error message
printError(error);
}
}
// Calling function to make API call
deleteComment();

Let’s understand the above code by breaking it down:

  • Line 13: We define the id parameter in which we pass the comment ID that we want to delete.

  • Lines 16–19: We define an options variable in which we defined the request type as DELETE, and the header in which we passed the headerParameters variable.

In case of successful execution of the above code, it will return the response code 204.