Introduction
This post is about the usage of nodejs module: youtube-api, which is to query youtube for videos. There are options to search youtube videos, list all videos from a playlist, list all videos from a channel. Also, you can fetch descriptive details about a video.
This library is ES6, promise compatible.
How to Download
From npmjs.com, look for youtube-api-es6: NpmJs Youtube API ES6
Or, in your package.json file, include: youtube-api-es6 by running commmand:
npm install youtube-api-es6
Quick Examples
To fetch details of a single Video
const youtubeService = require('youtube-api-es6').youtubeService;
const youtubeConfig = {
key: 'Your key'
};
return youtubeService.init(youtubeConfig)
.then(function() {
return youtubeService.getVideoDetail('a7hGVtz8syM');
})
.then(function(res) {
console.log(JSON.stringify(res, null, 3));
});
Output
For sample output response: See Youtube API Response
Usage of library
Initialization
First, you need to initialize the library
const youtubeService = require('youtube-api-es6').youtubeService;
const youtubeConfig = {
key: 'your key'
};
youtubeService.init(youtubeConfig);
APIs available
fetchAllVideosFromChannel(channelId)
Returns snippet level details of all the videos in this channel. You can then use another API: getVideoDetail to fetch details about that video, and getAllCommentsForVideo to get all comments for that video.
search(params)
Search for videos. Example:
var opts = {
maxResults: 10,
//channelId: 'UCNNxPxH_zIPxvWy5QMFkruA',
part: 'snippet',
// playlistId: 'xxx',
type: 'video'
};
getVideoDetail(videoId, detailOptions)
Get video details. Second parameter is optional: detailOptions, and by default it returns details for levels: snippet,contentDetails,topicDetails,statistics.
You can also pass which level of details you want in this parameter. Each level should be passed comma separated.
Example:
getVideoDetail(1234, 'snippet,statistics');
listPlaylist(playlistId)
List all the videos from a playlist. Details of video will be snippet level.
getAllCommentsForVideo(videoId)
Get all comments recursively for a video. Comments includes all parent level and child level comments.
Need Support or Report an Issue
Goto: https://github.com/GyanBlog/youtube-api-nodejs/issues
Youtube API reference
For youtube api reference, please visit: Youtube Official Rest APIs page
Some Examples
To get thumbnail of a video
Use api:
getVideoDetail(videoId, 'snippet')
const youtubeService = require('youtube-api-es6').youtubeService;
const youtubeConfig = {
key: 'Your key'
};
return youtubeService.init(youtubeConfig)
.then(function() {
return youtubeService.getVideoDetail('a7hGVtz8syM', 'snippet');
})
.then(function(res) {
console.log(JSON.stringify(res, null, 3));
});
Response
To see JSON response, see Youtube JSON Response