crawlfetch

Video download API for Python: YouTube, TikTok, Instagram and more

Python is the most common language for YouTube download scripts, and also the one where libraries like pytube break most often. This guide calls the CrawlFetch REST API with requests, so there is no scraping code to maintain: one GET returns metadata, every format and a direct download URL.

What you need

  • A CrawlFetch API key — create one free (1,000 free credits on signup plus 500 a month, no card).
  • Python 3.9 or newer.
  • The requests package.
  • The key stored in an environment variable named CRAWLFETCH_KEY, never in source control.
pip install requests

Resolve a video URL in Python

Call GET /v1/resolve with the video URL. Watch, Shorts, youtu.be and embed URLs are all accepted. Optional format (mp4, webm, m4a, opus) and res (360p–2160p) parameters pick the file; the API falls back to the closest available resolution.

import os
import requests
API = "https://api.crawlfetch.com"
KEY = os.environ["CRAWLFETCH_KEY"]
def resolve(url: str, fmt: str = "mp4", res: str = "1080p") -> dict:
r = requests.get(
f"{API}/v1/resolve",
params={"url": url, "format": fmt, "res": res},
headers={"Authorization": f"Bearer {KEY}"},
timeout=15,
)
r.raise_for_status()
return r.json()
video = resolve("https://youtu.be/aqz-KE-bpKQ")
print(video["title"], video["bestQuality"], len(video["formats"]), "formats")

Download the file

downloadUrl is a direct download link on the CrawlFetch domain — a plain file URL, not a stream manifest or player page. Fetch it like any other file and stream the body to disk (or to your own storage) rather than buffering whole videos in memory.

def download(video: dict, path: str) -> None:
with requests.get(video["downloadUrl"], stream=True, timeout=60) as r:
r.raise_for_status()
with open(path, "wb") as f:
for chunk in r.iter_content(chunk_size=1 << 20): # 1 MiB
f.write(chunk)
download(video, f"{video['id']}.mp4")

Handle errors and rate limits

raise_for_status() turns 4xx/5xx into requests.HTTPError; inspect err.response.status_code. For 429, sleep for int(r.headers['Retry-After']) seconds. For 500, retry with backoff — urllib3.util.Retry mounted on a requests.Session does this in a few lines.

StatusMeaningWhat to do
400Missing or malformed url parameterValidate the URL before calling; don't retry
401Missing, invalid or revoked API keyCheck the key and the env var it is read from
404Video is private, removed or region-lockedTell the user; retrying will not help
429Quota or rate limit exceededWait for the Retry-After header, then retry
500Upstream manifest fetch failedSafe to retry with exponential backoff

Production checklist

  • Call the API from your server, not from browser or mobile code that would expose the key.
  • Cache the resolve response for up to a few hours — downloadUrl stays valid for roughly six.
  • Reuse one requests.Session() so connections are pooled across calls.
  • For async apps (FastAPI, aiohttp), use httpx.AsyncClient with the same parameters.

FAQ

Is this a replacement for pytube or yt-dlp in Python?

For the job of getting a download URL, yes. Instead of parsing YouTube pages locally, your code makes one HTTPS request. See pytube alternative and yt-dlp vs an API for the trade-offs.

Can I download only the audio in Python?

Yes. Pass format=m4a (or opus), or read audioUrl from any resolve response. Audio-only formats are included on Starter, Pro and Scale plans.

How do I convert the result to MP3 in Python?

Download the m4a stream, then run ffmpeg -i in.m4a -codec:a libmp3lame -q:a 2 out.mp3 via subprocess.run. See YouTube to MP3 API.

Related

Last updated 2026-10-25

Add video downloads to your product today.

1,000 credits on signup plus 500 every month. That is roughly 180 YouTube clips to start, with no card.

Create free account