This tutorial shows how to retrieve the title and view count of a YouTube video using the YouTube Data API.
Make a request to the YouTube Data API endpoint:
const response = await fetch(url, {
url: '${API_HOST}/videos',
params: {
part: 'snippet,contentDetails,statistics',
id: 'Ks-_Mh1QhMc'
},
headers: { 'X-RapidAPI-Key': RAPID_API_KEY }
});
url
to the videos endpointpart
parameter to include snippet, content details, and statisticsid
Parse the JSON response:
const result = await response.json();
Extract the video title and view count from the response:
const title = result.items[0].snippet.title;
const views = result.items[0].statistics.viewCount;
The video title is located under items[0].snippet.title
and the view count under items[0].statistics.viewCount
in the response object.
That’s it! You have now retrieved the title and view count for the specified YouTube video using a simple API request in JavaScript.