Fetching Metadata from Youtube API

Saturday, March 18th 2023

The YouTube API requires an authentication token to access user data, and the API key provided by Google Cloud Platform. Assuming you have these credentials, you can use the following JavaScript code to get all videos metadata from a given account:

// Import the Google API client library import { google } from 'googleapis'; // Create a new client with authentication credentials const auth = new google.auth.OAuth2({ clientId: 'YOUR_CLIENT_ID', clientSecret: 'YOUR_CLIENT_SECRET', redirectUri: 'YOUR_REDIRECT_URI', access_token: 'USER_ACCESS_TOKEN', refresh_token: 'USER_REFRESH_TOKEN', }); // Create a new instance of the YouTube API const youtube = google.youtube({ version: 'v3', auth, }); // Get all videos metadata from the given account youtube.search.list({ part: 'id,snippet', channelId: 'tukmol', maxResults: 50, }, (err, res) => { if (err) { console.error(err); return; } const videos = res.data.items; videos.forEach((video) => { console.log(video.snippet.title); console.log(video.snippet.description); }); });

This code uses the Google API client library to authenticate and authorize access to the YouTube API. It then creates a new instance of the YouTube API, and uses the search.list method to get all videos metadata from the given account. The code logs the title and description of each video to the console. You can modify the code to suit your needs, such as increasing the maxResults parameter to get more videos, or modifying the search parameters to filter the videos by specific criteria.

Pull individual metadata per videoId

// Set the video ID const videoId = 'VIDEO_ID'; // Get the video metadata youtube.videos.list({ part: 'id,snippet', id: videoId, }, (err, res) => { if (err) { console.error(err); return; } const video = res.data.items[0]; console.log(video.snippet.title); console.log(video.snippet.description);

This code uses the Google API client library to authenticate and authorize access to the YouTube API. It then creates a new instance of the YouTube API, and uses the videos.list method to retrieve metadata for the specified video ID. The code logs the title and description of the video to the console. You can modify the code to retrieve additional metadata by adding more fields to the part parameter, or modifying the search parameters to filter the videos by specific criteria.


Other metadata

The YouTube Data API provides a wide range of metadata fields that you can retrieve for a video using the videos.list method. Some of the most commonly used fields include:

  • id: The unique video ID.
  • snippet.title: The video title.
  • snippet.description: The video description.
  • snippet.tags: An array of tags associated with the video.
  • snippet.channelId: The ID of the channel that uploaded the video.
  • snippet.channelTitle: The title of the channel that uploaded the video.
  • snippet.publishedAt: The date and time the video was published.
  • snippet.thumbnails: A collection of thumbnail images for the video.

In addition to these fields, there are many other fields available that provide additional metadata about the video, such as its duration, view count, like count, and comment count. You can find a full list of the available fields in the YouTube Data API documentation.