This tutorial will guide you through the process of fetching comments for a specific YouTube video using the YouTube Data API.
rapidAPIKey
with your actual API key.const url = 'https://youtube342.p.rapidapi.com/comments?part=snippet&parentId=UgzDE2tasfmrYLyNkGt4AaABAg';
url
variable contains the API endpoint URL for fetching comments.part
parameter specifies the fields to include in the response, in this case, we want the snippet
field which contains the comment details.parentId
parameter is the ID of the comment thread or video for which you want to retrieve comments. Replace UgzDE2tasfmrYLyNkGt4AaABAg
with the actual ID of the video or comment thread you want to fetch comments for.const options = {
method: 'GET',
headers: {
'X-RapidAPI-Key': rapidAPIKey,
'X-RapidAPI-Host': 'youtube342.p.rapidapi.com'
}
};
options
object contains the configuration for the API request.method
property is set to 'GET'
since we are making a GET request to fetch comments.headers
property includes the necessary headers for authentication and specifying the RapidAPI host.try {
const response = await fetch(url, options);
const result = await response.text();
console.log(result);
} catch (error) {
console.error(error);
}
fetch
function to make the API request, passing the url
and options
as arguments.await
keyword is used to wait for the response from the API.response.text()
and store it in the result
variable.result
is then logged to the console using console.log()
.catch
block and logged to the console using console.error()
.