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/GyanByte/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

Nodejs - Json object schema validation with Joi

Nodejs - Json object schema validation with Joi

Introduction In this post, I will show how to validate your json schema…

Mongoose - Using CRUD operations in mongodb in nodejs

Mongoose - Using CRUD operations in mongodb in nodejs

MongoDB CRUD Operations Mongoose provides a simple schema based solution to…

How to check whether a website link has your URL backlink or not - NodeJs implementation

How to check whether a website link has your URL backlink or not - NodeJs implementation

Introduction I got my seo backlink work done from a freelancer. It was like 300…

How to connect to mysql from nodejs, with ES6 promise

How to connect to mysql from nodejs, with ES6 promise

Introduction I had to develop a small automation to query some old mysql data…

How to Fetch JSON of all Videos of a Youtube Channel

How to Fetch JSON of all Videos of a Youtube Channel

Youtube APIs are great way to fetch details about video or channels. I’ve…

How to get Youtube Video Thumbnail Images

How to get Youtube Video Thumbnail Images

If your youtube video looks like:https://www.youtube.com/watch?v=g0kFl7sBdDQ…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) �— Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…