tutorials2 Min Read

Youtube API in NodeJs, Usage with Example

Gorav Singal

March 04, 2018

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

AI Video Generation in 2025 — Models, Costs, and How to Build a Cost-Effective Pipeline

AI Video Generation in 2025 — Models, Costs, and How to Build a Cost-Effective Pipeline

AI video generation went from “cool demo” to “usable in production” in 2024-202…

AI Models in 2025 — Cost, Capabilities, and Which One to Use

AI Models in 2025 — Cost, Capabilities, and Which One to Use

Choosing the right AI model is one of the most impactful decisions you’ll make…

AI Image Generation in 2025 — Models, Costs, and How to Optimize Spend

AI Image Generation in 2025 — Models, Costs, and How to Optimize Spend

Generating one image with AI costs between $0.002 and $0.12. That might sound…

AI Coding Assistants in 2025 — Every Tool Compared, and Which One to Actually Use

AI Coding Assistants in 2025 — Every Tool Compared, and Which One to Actually Use

Two years ago, AI coding meant one thing: GitHub Copilot autocompleting your…

AI Agents Demystified — It's Just Automation With a Better Brain

AI Agents Demystified — It's Just Automation With a Better Brain

Let’s cut through the noise. If you read Twitter or LinkedIn, you’d think “AI…

Supply Chain Security — Protecting Your Software Pipeline

Supply Chain Security — Protecting Your Software Pipeline

In 2024, a single malicious contributor nearly compromised every Linux system on…