Rate and Comment on a Video
Learn how to comment and rate a video using Youtube Data API.
We'll cover the following
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 |
| String | Required | Specifies the video user wants to rate. |
| String | Required | Specifies the rating user wants to give to the video. The possible values are |
Let’s give a thumbs-up to our video on the channel. Click the “Run” button to execute the following code:
// Defining import libraries hereimport fetch from 'node-fetch';// Define endpoint URL hereconst endpointUrl = new URL('https://www.googleapis.com/youtube/v3/videos/rate');// Define Header Parameters hereconst headerParameters = {Authorization: 'Bearer {{ACCESS_TOKEN}}',Accept: 'application/json',};// Define Query Parameters hereconst bodyParameters = JSON.stringify({id: '{{VIDEO_ID}}',rating: 'like',});// Setting API call optionsconst options = {method: 'POST',headers: headerParameters,body: bodyParameters,};// Function to make API callasync function rateVideo() {try {const response = await fetch(endpointUrl, options);// Custom function for printing the API responseprintResponse(response.status);} catch (error) {// Custom function for printing the error messageprintError(error);}}// Calling function to make API callrateVideo();
Lines 13–16: We define the
bodyParameters
variable in which we define two parameters:id
andrating
. Theid
parameter is used to define the ID of the video that the user wants to rate and therating
parameter allows the user to pass their rating.Line 24: We define an
async
function with the name ofrateVideo()
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.
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 |
| String | Required | This parameter serves two functions. It specifies the |
| String | Required | Used to provide the channel ID. |
| String | Required | Used to provide the comment user wants to post. |
| 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.
// Defining import libraries hereimport fetch from 'node-fetch';// Define endpoint URL hereconst endpointUrl = new URL('https://www.googleapis.com/youtube/v3/commentThreads');// Define Header Parameters hereconst headerParameters = {Authorization: 'Bearer {{ACCESS_TOKEN}}',Accept: 'application/json',};// Define Query Parameters hereconst queryParameters = new URLSearchParams({part: 'snippet',});// Define Body Parameters hereconst bodyParameters = JSON.stringify({snippet: {channelId: '{{CHANNEL_ID}}',videoId: '{{VIDEO_ID}}',topLevelComment: {snippet: {textOriginal: 'This is a sample comment',},},},});// Setting API call optionsconst options = {method: 'POST',headers: headerParameters,body: bodyParameters,};// Function to make API callasync function postComment() {try {endpointUrl.search = queryParameters;const response = await fetch(endpointUrl, options);// Custom function for printing the API responseprintResponse(response);} catch (error) {// Custom function for printing the error messageprintError(error);}}// Calling function to make API callpostComment();
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
andsnippet.videoId
inbodyParameters
. Additionally, we pass our comment insnippet.topLevelComment.snippet.textOriginal
.Line 37: We define an
async
function namedpostComment()
to post a comment on the video.
The following table contains some of the important response fields:
Response fields
Name | Type | Description |
| String | Reflects the resource type of the API call. |
| ETag | Contains the resource ETag. |
| String | Contains the comment ID. |
| 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 |
| String | Required | This parameter specifies a comma-separated list of one or more resource properties to be included in the API response. These are |
| String | Optional | Used to filter the comment threads that are about a specific channel. |
| String | Optional | Specifies comma-separated IDs of the comment threads to list thier properties in the API response. |
| String | Optional | Filters the comment threads of a specific video. |
| String | Optional | Filters all the comment threads of a specific channel. |
| Integer | Optional | Fixes the number of results that will be fetched. |
| 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
, orallThreadsRelatedToChannelId
.
Click the “Run” button to list the comments on the videos.
// Defining import libraries hereimport fetch from 'node-fetch';// Define endpoint URL hereconst endpointUrl = new URL('https://www.googleapis.com/youtube/v3/commentThreads');// Define Header Parameters hereconst headerParameters = {Authorization: 'Bearer {{ACCESS_TOKEN}}',Accept: 'application/json',};// Define Query Parameters hereconst queryParameters = new URLSearchParams({part: 'snippet,replies',videoId: '{{VIDEO_ID}}',});// Setting API call optionsconst options = {method: 'GET',headers: headerParameters,};// Function to make API callasync function listComments() {try {endpointUrl.search = queryParameters;const response = await fetch(endpointUrl, options);// Printing responseprintResponse(response);} catch (error) {// Printing error messageprintError(error);}}// Calling function to make API calllistComments();
Let’s understand the above code by breaking it down:
Lines 14–17: We define the
queryParameters
in which we define thepart
andvideoId
parameters.Line 26: We define an
async
function aslistComment()
to retrieve comments from the video.
The following table contains some of the important response fields:
Response fields
Name | Type | Description |
| String | Reflects the resource type of the API call. |
| ETag | Contains the resource ETag. |
| String | Used to fetch the next page in the response result. |
| String | Contains the comment text. |
| String | Used to fetch the previous page in the response result. |
| Object | Contains the response page information. |
| 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.
// Importing libraries hereimport fetch from 'node-fetch';// Define endpoint URL hereconst endpointUrl = new URL('https://www.googleapis.com/youtube/v3/comments');// Define Header Parameters hereconst headerParameters = {Authorization: 'Bearer {{ACCESS_TOKEN}}',Accept: 'application/json',};const queryParameters = new URLSearchParams({id: '{{COMMENT_ID}}',});// Setting API call optionsconst options = {method: 'DELETE',headers: headerParameters,};// Function to make API callasync function deleteComment() {try {endpointUrl.search = queryParameters;const response = await fetch(endpointUrl, options);// Custom function for printing the API responseprintResponse(`${response.status}:${response.statusText}`);} catch (error) {// Custom function for printing the error messageprintError(error);}}// Calling function to make API calldeleteComment();
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 asDELETE
, and the header in which we passed theheaderParameters
variable.
In case of successful execution of the above code, it will return the response code 204
.