Cookies
Videos the default clients can’t play without an account need the cookie of an age-verified account. To get it:
- Install Get cookies.txt LOCALLY for Chrome or Firefox, and allow it in private windows.
- Open a private window, sign in to youtube.com, and export with the extension. It saves
www.youtube.com_cookies.txt. - Close the private window without signing out. Signing out invalidates the cookie.
Pass the file’s text as cookie. Ytdl keeps its youtube.com cookies and ignores the rest:
import { readFileSync } from "node:fs";
const yt = new Ytdl({ cookie: readFileSync("www.youtube.com_cookies.txt", "utf8") });A Cookie header works too, for example from a .env file, quoted because it contains ; and =:
YT_COOKIE="SAPISID=AbCdEf/GhIjKl; LOGIN_INFO=AFmmF2sw...; __Secure-3PAPISID=AbCdEf/GhIjKl; ..."const yt = new Ytdl({ cookie: process.env.YT_COOKIE });An export or header without SAPISID or __Secure-3PAPISID is signed out, and the constructor throws INVALID_ARGUMENT. A cookie YouTube no longer accepts fails the lookup with AUTH_FAILED; export a fresh one.