tutorials|March 04, 2018|2 min read

Youtube API in NodeJs, Usage with Example

TL;DR

Use the youtube-api-es6 npm module to search videos, list playlist/channel videos, and fetch video details via the YouTube Data API in an ES6 promise-based workflow.

Youtube API in NodeJs, Usage with Example

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: ```js var opts = { maxResults: 10, //channelId: 'UCNNxPxH_zIPxvWy5QMFkruA', part: 'snippet',
// playlistId: 'xxx',
type: 'video'

};


<h3>getVideoDetail(videoId, detailOptions)</h3>
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:
```js
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

Related Posts

Python - Some useful Pytest Commands

Python - Some useful Pytest Commands

Introduction In this post, we will explore some useful command line options for…

How to Solve Circular Import Error in Python

How to Solve Circular Import Error in Python

Introduction To give some context, I have two python files. (Both in same folder…

List all the Node ids which do not have images from my domain

List all the Node ids which do not have images from my domain

I use drupal-7 in my website. I used to write articles and put images in that…

How to Fix Drupal Mysql error - Communication link failure: 1153 Got a packet bigger than 'max_allowed_packet' bytes

How to Fix Drupal Mysql error - Communication link failure: 1153 Got a packet bigger than 'max_allowed_packet' bytes

Introduction While this topic may applicable to all mysql/mariadb users who…

A Practical Guide for better understanding Git Diff

A Practical Guide for better understanding Git Diff

Introduction In this guide, We will get basic understanding of various options…

Azure Functions - How to trigger an email on any change in storage container blob

Azure Functions - How to trigger an email on any change in storage container blob

Introduction In Azure, assuming you are having a storage container. And, you…

Latest Posts