~/tutorials/44-youtube-api-nodejs-usage-example.md

Youtube API in NodeJs, Usage with Example

2 min readMarch 04, 2018by Gorav Singal#youtube-api#nodejs#nodejs-youtube-module
// 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

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

WebSockets with Socket.io in Node.js

WebSockets with Socket.io in Node.js

WebSocket vs HTTP Traditional HTTP follows a request/response model — the client…

Testing Node.js — Unit, Integration, and E2E

Testing Node.js — Unit, Integration, and E2E

Testing Strategy A solid testing strategy follows the testing pyramid — many…

Redis — Caching, Sessions, Pub/Sub in Node.js

Redis — Caching, Sessions, Pub/Sub in Node.js

Why Redis for Node.js Redis is an in-memory data store that serves as a cache…

Database Integration — PostgreSQL with Node.js

Database Integration — PostgreSQL with Node.js

Choosing Your PostgreSQL Client Node.js has three main approaches to working…

Performance Optimization and Profiling in Node.js

Performance Optimization and Profiling in Node.js

Profiling First, Optimize Second Never optimize blindly. Always profile to find…

Node.js Architecture — Event Loop Deep Dive

Node.js Architecture — Event Loop Deep Dive

Why the Event Loop Matters Node.js runs JavaScript on a single thread, yet…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…