A full-featured wrapper around the InnerTube API, which is what YouTube itself uses.
WIP- Documentation for YouTube.js 2.0.0
Table of Contents
InnerTube is an API used across all YouTube clients, it was created to simplify1 the internal structure of the platform in a way that updates, tweaks, and experiments can be easily made. This library handles all the low-level communication with Innertube, providing a simple, fast, and efficient way to interact with YouTube programmatically.
If you have any questions or need help, feel free to contact us on our chat server here.
YouTube.js runs on Node.js, Deno and in modern browsers.
It requires a runtime with the following features:
fetch
- On Node we use undici's fetch implementation which requires Node.js 16.8+. You may provide your own fetch implementation if you need to use an older version. See providing your own fetch implementation for more information.
- The
Response
object returned by fetch must thus be spec compliant and return aReadableStream
object if you want to use theVideoInfo#download
method. (Implementations likenode-fetch
returns a non-standardReadable
object.)
EventTarget
andCustomEvent
required.
# NPM
npm install youtubei.js@latest
# Yarn
yarn add youtubei.js@latest
# Git (edge version)
npm install github:LuanRT/YouTube.js
TODO: Deno install instructions (esm.sh possibly?)
Create an InnerTube instance:
// const { Innertube } = require('youtubei.js');
import { Innertube } from 'youtubei.js';
const youtube = await Innertube.create();
To use YouTube.js in the browser you must proxy requests through your own server. You can see our simple reference implementation in Deno in examples/browser/proxy/deno.ts
.
You may provide your own fetch implementation to be used by YouTube.js. Which we will use here to modify and send the requests to through our proxy. See examples/browser/web
for an simple example using Vite.
// Pre-bundled version for the web
import { Innertube } from 'youtubei.js/bundle/browser';
await Innertube.create({
fetch: async (input: RequestInfo | URL, init?: RequestInit) => {
// Modify the request
// and send it to the proxy
// fetch the url
return fetch(request, init);
}
});
YouTube.js supports streaming of videos in the browser by converting YouTube's streaming data into a MPEG-DASH manifest.
The example below uses dash.js
to play the video.
import { Innertube } from 'youtubei.js';
import dashjs from 'dashjs';
const youtube = await Innertube.create({ /* setup - see above */ });
// get the video info
const videoInfo = await youtube.getInfo('videoId');
// now convert to a dash manifest
// again - to be able to stream the video in the browser - we must proxy the requests through our own server
// to do this, we provide a method to transform the urls before writing them to the manifest
const manifest = videoInfo.toDash(url => {
// modify the url
// and return it
return url;
});
const uri = "data:application/dash+xml;charset=utf-8;base64," + btoa(manifest);
const videoElement = document.getElementById('video_player');
const player = dashjs.MediaPlayer().create();
player.initialize(videoElement, uri, true);
Our browser example in examples/browser/web
provides a full working example.
You may provide your own fetch implementation to be used by YouTube.js. This may be useful in some cases to modify the requests before they are sent and transform the responses before they are returned (eg. for proxies).
// provide a fetch implementation
const yt = await Innertube.create({
fetch: async (input: RequestInfo | URL, init?: RequestInit) => {
// make the request with your own fetch implementation
// and return the response
return new Response(
/* ... */
);
}
});
To improve performance, you may wish to cache the transformed player instance which we use to decode the streaming urls.
Our cache uses the node:fs
module in Node-like environments, Deno.writeFile
in Deno and indexedDB
in browsers.
import { Innertube, UniversalCache } from 'youtubei.js';
// By default, cache stores files in the OS temp directory (or indexedDB in browsers).
const yt = await Innertube.create({
cache: new UniversalCache()
});
// You may wish to make the cache persistent (on Node and Deno)
const yt = await Innertube.create({
cache: new UniversalCache(
// Enables persistent caching
true,
// Path to the cache directory, will create the directory if it doesn't exist
'./.cache'
)
});
-
Innertube
- .getInfo(video_id)
- .getBasicInfo(video_id)
- .search(query, filters?)
- .getSearchSuggestions(query)
- .getComments(video_id, sort_by?)
- .getHomeFeed()
- .getLibrary()
- .getHistory()
- .getTrending()
- .getSubscriptionsFeed()
- .getChannel(id)
- .getNotifications()
- .getUnseenNotificationsCount()
- .getPlaylist(id)
- .getStreamingData(video_id, options)
- .download(video_id, options?)
Retrieves video info, including playback data and even layout elements such as menus, buttons etc β all nicely parsed.
Returns: Promise.<VideoInfo>
Param | Type | Description |
---|---|---|
video_id | string |
The id of the video |
Methods & Getters:
-
<info>#like()
- Likes the video.
-
<info>#dislike()
- Dislikes the video.
-
<info>#removeLike()
- Removes like/dislike.
-
<info>#filters
- Returns filters that can be applied to the watch next feed.
-
<info>#selectFilter(name)
- Applies given filter to the watch next feed and returns a new instance of
VideoInfo
.
- Applies given filter to the watch next feed and returns a new instance of
-
<info>#getWatchNextContinuation()
- Retrieves next batch of items for the watch next feed.
-
<info>#page
- Returns original InnerTube response (sanitized).
Suitable for cases where you only need basic video metadata, much faster than getInfo()
.
Returns: Promise.<VideoInfo>
Param | Type | Description |
---|---|---|
video_id | string |
The id of the video |
Searches the given query on YouTube.
Returns: Promise.<Search>
Param | Type | Description |
---|---|---|
query | string |
The search query |
filters | SearchFilters |
Search filters |
Methods & Getters:
-
<search>#selectRefinementCard(SearchRefinementCard | string)
- Applies given refinement card and returns a new Search instance.
-
<search>#refinement_card_queries
- Returns available refinement cards, this is a simplified version of the
refinement_cards
object.
- Returns available refinement cards, this is a simplified version of the
-
<search>#getContinuation()
- Retrieves next batch of results.
Retrieves search suggestions for given query.
Returns: Promise.<string[]>
Param | Type | Description |
---|---|---|
query | string |
The search query |
Retrieves comments for given video.
Returns: Promise.<Comments>
Param | Type | Description |
---|---|---|
video_id | string |
The video id |
sort_by | string |
Can be: TOP_COMMENTS or NEWEST_FIRST |
Retrieves YouTube's home feed.
Returns: Promise.<FilterableFeed>
Retrieves the account's library.
Returns: Promise.<Library>
Retrieves watch history.
Returns: Promise.<History>
Retrieves trending content.
Returns: Promise.<TabbedFeed>
Retrieves subscriptions feed.
Returns: Promise.<Feed>
Retrieves contents for a given channel.
Returns: Promise.<Channel>
Param | Type | Description |
---|---|---|
id | string |
Channel id |
Retrieves notifications.
Returns: Promise.<NotificationsMenu>
Retrieves unseen notifications count.
Returns: Promise.<number>
Retrieves playlist contents.
Returns: Promise.<Playlist>
Param | Type | Description |
---|---|---|
id | string |
Playlist id |
Returns deciphered streaming data.
Returns: Promise.<object>
Param | Type | Description |
---|---|---|
video_id | string |
Video id |
options | FormatOptions |
Format options |
Downloads a given video.
Returns: Promise.<ReadableStream<Uint8Array>>
Param | Type | Description |
---|---|---|
video_id | string |
Video id |
options | DownloadOptions |
Download options |
Contributions, issues and feature requests are welcome. Feel free to check issues page if you want to contribute.
LuanRT - @lrt_nooneknows - [email protected]
Project Link: https://github.com/LuanRT/YouTube.js
This project is not affiliated with, endorsed, or sponsored by YouTube or any of their affiliates or subsidiaries. All trademarks, logos and brand names are the property of their respective owners, and are used only to directly describe the services being provided, as such, any usage of trademarks to refer to such services is considered nominative use.
Should you have any questions or concerns please contact me directly via email.
Distributed under the MIT License.