A YouTube Download API for AI Agents, Without the Bot Checks
Why your agent hits "Sign in to confirm you're not a bot"
Claude Code, Codex, Cursor, n8n and most agent frameworks run their tools on a server or in a sandbox. When the agent decides to download a YouTube video, it usually installs yt-dlp and runs it from there. YouTube gives requests from data-centre IP addresses far less trust than requests from home connections, so the agent gets a sign-in check, a 403 or a 429 instead of a video. We explain the error itself in this fix guide.
The usual workaround, exporting your browser cookies so yt-dlp looks signed in, is a bad fit for agents:
- It hands your Google account to an autonomous loop. The cookies sit in a file the agent can read, log or paste into a prompt.
- It can get the account restricted. Heavy automated downloading from a signed-in session is exactly what YouTube rate-limits, sometimes for days.
- It breaks silently. Cookies expire, yt-dlp needs updates when YouTube changes, and the agent spends tokens retrying a command that can't work.
What an agent needs from a download tool
Agents work best with tools that are boring: one call, a predictable result, and errors that say what to do next. For YouTube downloads that means:
| Need | yt-dlp inside the agent | Download API |
|---|---|---|
| Works from a cloud sandbox | Often blocked by bot checks | Yes, downloads run on the provider's side |
| Secrets the agent holds | Your Google cookies | One API key with no account access |
| Result | A file on the sandbox disk, or a stack trace | A download link, size, codecs and resolution |
| Errors | Free-text output the model has to interpret | Numbered codes: private, deleted, geo-blocked, quota |
| Upkeep | Update yt-dlp when YouTube changes | None on your side |
How the API works
Two endpoints. Start a job with the video URL, then poll until it's done. Every call takes your key in the X-API-Key header.
curl -X POST https://api.videodl.app/api/start-download \
-H "X-API-Key: $VIDEODL_API_KEY" \
-H "Content-Type: application/json" \
-d '{"url": "https://www.youtube.com/watch?v=VIDEO_ID", "resolution": "1080"}'
curl "https://api.videodl.app/api/download-status?worker_id=WORKER_ID" \
-H "X-API-Key: $VIDEODL_API_KEY"
The first call returns {"worker_id": "...", "status": "queued"}. The status call moves through queued, downloading and finally downloaded, with a download_url, the resolution, the codecs and an expires_in time, or error with an error_code. While a job runs, the response includes retry_after, the number of seconds to wait before the next check.
Give your agent the tool
Option 1: an MCP server
If your agent supports the Model Context Protocol (Claude Code, Claude Desktop, Cursor, Codex and many others do), add a small MCP server and the agent discovers the tool on its own. We wrote a tested 90-line server and the setup for each client in YouTube download MCP server for Claude Code, Cursor and Codex.
Option 2: a function-calling tool
For your own agent built on a model API or an agent framework, wrap the two calls in one function. It waits up to wait_seconds and returns either a link, an error code, or a job ID to check later, so the agent never blocks forever. Building in n8n instead? The same two calls work with the HTTP Request node; see how to download YouTube videos in n8n.
import os
import time
import requests
API = "https://api.videodl.app"
HEADERS = {"X-API-Key": os.environ["VIDEODL_API_KEY"]}
def download_youtube_video(url, resolution="1080", audio_only=False, wait_seconds=300):
body = {"url": url}
if audio_only:
body.update({"audio_only": True, "audio_format": "mp3"})
else:
body["resolution"] = resolution
r = requests.post(f"{API}/api/start-download", json=body, headers=HEADERS, timeout=30)
data = r.json()
if r.status_code != 202:
return {"status": "error", "error_code": data.get("error_code"), "error": data.get("error")}
deadline = time.time() + wait_seconds
while time.time() < deadline:
job = requests.get(f"{API}/api/download-status", params={"worker_id": data["worker_id"]},
headers=HEADERS, timeout=30).json()
if job["status"] == "downloaded":
return {"status": "downloaded", "title": job.get("title"),
"download_url": job["download_url"], "expires_in": job.get("expires_in")}
if job["status"] == "error":
return {"status": "error", "error_code": job.get("error_code"),
"error_label": job.get("error_label")}
time.sleep(job.get("retry_after", 3))
return {"status": "still_running", "worker_id": data["worker_id"]}
Then describe it to the model. This is the Anthropic Messages API shape. For OpenAI function tools, the same JSON Schema goes under parameters instead of input_schema.
{
"name": "download_youtube_video",
"description": "Download a public YouTube video and return a temporary download link. Use audio_only for transcription. If status is still_running, report the worker_id.",
"input_schema": {
"type": "object",
"properties": {
"url": {"type": "string", "description": "YouTube video URL or 11-character video ID"},
"resolution": {"type": "string", "enum": ["360", "480", "720", "1080", "2160"]},
"audio_only": {"type": "boolean", "description": "Return an MP3 of the audio only"}
},
"required": ["url"]
}
}
Teach the agent what errors mean
The biggest win over a raw yt-dlp call is that failures come back as codes. Put a short version of this table in your system prompt so the agent stops retrying videos that can't be downloaded:
| Code | Meaning | What the agent should do |
|---|---|---|
| 1, 2, 3 | Deleted, unavailable or private | Tell the user. Don't retry |
| 4, 60 | Geo-restricted | Tell the user. Retrying won't help |
| 6, 7, 8, 11 | Age-restricted, members-only, Premium or sign-in required | Tell the user. These need an account we don't use |
| 5, 9 | Live now, or a scheduled stream that hasn't started | Try again after the stream ends |
| 55 | Too many requests | Wait for retry_after seconds, then retry |
| 51, 54 | Monthly video or bandwidth quota used | Stop and tell the user |
The full list is in YouTube download API error codes. Temporary failures, like a blocked download server, are retried for you on other servers before the job reports an error, so the agent doesn't need its own retry logic for those.
Settings that save tokens and bandwidth
- Transcribing or summarising? Send
"audio_only": true. An MP3 is a fraction of the video size. See YouTube to MP3 by API. - Only need the words? Ask for
"captions": truewith"skip_download": trueto get subtitles and metadata without the video. See downloading YouTube subtitles by API. - Handing the file to another tool? Set a resolution cap like
"720", and ask for H.264 if the next tool is picky. See H.264-only downloads. - Large batches? Connect S3, GCS, R2 or Google Drive in the dashboard, and files land in your storage instead of a link that expires in about an hour.
Use it on content you're allowed to use
An agent makes it easy to download a lot very quickly. The API only fetches publicly available videos, and our acceptable use policy applies to agents the same as to people: use videos you own, have permission for, or are licensed to use.
FAQ
Why does my AI agent get "Sign in to confirm you're not a bot" from YouTube?
Agents run tools on cloud servers, and YouTube trusts data-centre IP addresses less than home connections. Downloads from those IPs often get a sign-in check, a 403 or a 429. Using a download API moves the download off the agent's server.
Should I give my agent my YouTube cookies to fix bot checks?
We don't recommend it. Cookies give the agent access to your Google account, automated downloads from a signed-in session can get the account rate-limited, and cookies expire. An API key with no account access is safer.
Can Claude Code or Cursor download YouTube videos?
Yes, with a tool. Add an MCP server that calls a download API, and the agent can download videos by URL and get back a link, without running yt-dlp on its own machine.
How does an agent know when a download is finished?
It polls the status endpoint with the job's worker_id. The response says queued, downloading, downloaded or error, and includes retry_after, the seconds to wait before checking again.
How much does the VideoDL API cost for an agent?
Starter is $12 a month for 200 videos. Most teams use a custom plan sized to their volume, video length, resolution and rate. There's a free trial.
Give your agent a YouTube tool that works
One API key, no cookies, no bot checks. Videos, audio, captions and metadata from $12/month. Start with 100 free videos.
Keep reading
All articles →
YouTube download MCP server for Claude Code, Cursor and Codex
Add a YouTube download tool to Claude Code, Cursor or Codex with a short MCP server. Tested Python code, setup for each client, and long-download handling.
Fix "Sign in to confirm you're not a bot"
Why YouTube shows 'Sign in to confirm you're not a bot' when downloading, and how to fix it — usually an IP flagged as a bot. Change your IP and slow requests.
YouTube download API error codes and how to handle them
Every VideoDL API error code explained: private, deleted, geo-blocked, age-restricted, rate limits and quotas. Which to retry, which to skip, with tested code.