YouTube.js

A wrapper around YouTube's internal API — reverse engineering InnerTube

README

YouTube.js


Description


InnerTube is an API used by all YouTube clients. It was created to simplify the deployment of new features and experiments across the platform [^1]. This library manages all low-level communication with InnerTube, providing a simple and efficient way to interact with YouTube programmatically. Its design aims to closely emulate an actual client, including the parsing of API responses.

If you have any questions or need help, feel free to reach out to us on our [Discord server][discord] or open an issue here.

Getting Started


Prerequisites

YouTube.js runs on Node.js, Deno, and modern browsers.

It requires a runtime with the following features:
- [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)
  - On Node, we use undici's fetch implementation, which requires Node.js 16.8+. If you need to use an older version, you may provide your own fetch implementation. See providing your own fetch implementation for more information.
  - The Response object returned by fetch must thus be spec compliant and return a ReadableStream object if you want to use the VideoInfo#download method. (Implementations like node-fetch returns a non-standard Readable object.)
- [EventTarget](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget) and [CustomEvent](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent) are required.

Installation


  1. ```bash
  2. # NPM
  3. npm install youtubei.js@latest

  4. # Yarn
  5. yarn add youtubei.js@latest

  6. # Git (edge version)
  7. npm install github:LuanRT/YouTube.js
  8. ```

When using Deno, you can import YouTube.js directly from deno.land:
  1. ```ts
  2. import { Innertube } from 'https://deno.land/x/youtubei/deno.ts';
  3. ```

Usage

Create an InnerTube instance:
  1. ```ts
  2. // const { Innertube } = require('youtubei.js');
  3. import { Innertube } from 'youtubei.js';
  4. const youtube = await Innertube.create(/* options */);
  5. ```

Initialization Options

Click to expand

OptionTypeDescriptionDefault
------------
`lang``string`Language.`en`
`location``string`Geolocation.`US`
`account_index``number`The`0`
`visitor_data``string`Setting`undefined`
`retrieve_player``boolean`Specifies`true`
`enable_safety_mode``boolean`Specifies`false`
`generate_session_locally``boolean`Specifies`false`
`device_category``DeviceCategory`Platform`DESKTOP`
`client_type``ClientType`InnerTube`WEB`
`timezone``string`The`*`
`cache``ICache`Used`undefined`
`cookie``string`YouTube`undefined`
`fetch``FetchFunction`Fetch`fetch`


Browser Usage

To use YouTube.js in the browser, you must proxy requests through your own server. You can see our simple reference implementation in Deno at [examples/browser/proxy/deno.ts](https://github.com/LuanRT/YouTube.js/tree/main/examples/browser/proxy/deno.ts).

You may provide your own fetch implementation to be used by YouTube.js, which we will use to modify and send the requests through a proxy. See [examples/browser/web](https://github.com/LuanRT/YouTube.js/tree/main/examples/browser/web) for a simple example using Vite.

  1. ```ts
  2. // Multiple exports are available for the web.
  3. // Unbundled ESM version
  4. import { Innertube } from 'youtubei.js/web';
  5. // Bundled ESM version
  6. // import { Innertube } from 'youtubei.js/web.bundle';
  7. // Production Bundled ESM version
  8. // import { Innertube } from 'youtubei.js/web.bundle.min';
  9. await Innertube.create({
  10.   fetch: async (input: RequestInfo | URL, init?: RequestInit) => {
  11.     // Modify the request
  12.     // and send it to the proxy

  13.     // fetch the URL
  14.     return fetch(request, init);
  15.   }
  16. });
  17. ```

Streaming

YouTube.js supports streaming of videos in the browser by converting YouTube's streaming data into an MPEG-DASH manifest.

The example below uses [dash.js](https://github.com/Dash-Industry-Forum/dash.js) to play the video.

  1. ```ts
  2. import { Innertube } from 'youtubei.js/web';
  3. import dashjs from 'dashjs';

  4. const youtube = await Innertube.create({ /* setup - see above */ });

  5. // Get the video info
  6. const videoInfo = await youtube.getInfo('videoId');

  7. // now convert to a dash manifest
  8. // again - to be able to stream the video in the browser - we must proxy the requests through our own server
  9. // to do this, we provide a method to transform the URLs before writing them to the manifest
  10. const manifest = await videoInfo.toDash(url => {
  11.   // modify the url
  12.   // and return it
  13.   return url;
  14. });

  15. const uri = "data:application/dash+xml;charset=utf-8;base64," + btoa(manifest);

  16. const videoElement = document.getElementById('video_player');

  17. const player = dashjs.MediaPlayer().create();
  18. player.initialize(videoElement, uri, true);
  19. ```

A fully working example can be found in [examples/browser/web](https://github.com/LuanRT/YouTube.js/blob/main/examples/browser/web).


Providing your own fetch implementation

You may provide your own fetch implementation to be used by YouTube.js. This can be useful in some cases to modify the requests before they are sent and transform the responses before they are returned (eg. for proxies).
  1. ```ts
  2. // provide a fetch implementation
  3. const yt = await Innertube.create({
  4.   fetch: async (input: RequestInfo | URL, init?: RequestInit) => {
  5.     // make the request with your own fetch implementation
  6.     // and return the response
  7.     return new Response(
  8.       /* ... */
  9.     );
  10.   }
  11. });
  12. ```