Configure Cookies Support
masterTo access private videos, rentals, or YouTube Premium content, you must provide cookies. You can export cookies from your browser using the EditThisCookie extension and pass them to ytdl.createAgent.
Important Security Notes:
- Use a dedicated account for this purpose.
- Do not use the YouTube 'logout' button; instead, delete browser cookies or use incognito mode to avoid expiring the session prematurely.
- Ensure the account is only using one IP address at a time to keep cookies alive longer.
You can pass cookies as an array of objects or read them from a JSON file.
const ytdl = require("@distube/ytdl-core");
const fs = require("fs");
// Option 1: Using a hardcoded array (not recommended for production)
const cookies = [
{ name: "cookie1", value: "COOKIE1_HERE" },
{ name: "cookie2", value: "COOKIE2_HERE" },
];
const agent = ytdl.createAgent(cookies);
// Option 2: Reading from a JSON file
const agentFromFile = ytdl.createAgent(JSON.parse(fs.readFileSync("cookies.json")));
// Use the agent in requests
ytdl.getInfo("http://www.youtube.com/watch?v=aqz-KE-bpKQ", { agent: agentFromFile });