Video download API for cURL & Bash: YouTube, TikTok, Instagram and more
Sometimes a shell script is all you need — a cron job, a CI step, a quick archive. CrawlFetch works with plain curl and jq: one call to resolve, one call to download.
What you need
- A CrawlFetch API key — create one free (1,000 free credits on signup plus 500 a month, no card).
- curl 7.68+ and jq.
- The key stored in an environment variable named
CRAWLFETCH_KEY, never in source control.
Resolve a video URL in cURL & Bash
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.
#!/usr/bin/env bashset -euo pipefailVIDEO_URL="${1:-https://youtu.be/aqz-KE-bpKQ}"curl -sfG "https://api.crawlfetch.com/v1/resolve" \--data-urlencode "url=$VIDEO_URL" \--data-urlencode "format=mp4" \--data-urlencode "res=1080p" \-H "Authorization: Bearer $CRAWLFETCH_KEY" > video.jsonjq -r '.title, .bestQuality, (.formats | length | tostring) + " formats"' video.json
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.
ID=$(jq -r .id video.json)curl -fL --retry 3 -o "$ID.mp4" "$(jq -r .downloadUrl video.json)"# list every format with its sizejq -r '.formats[] | [.label, .container, .kind, (.sizeMb|tostring)+" MB"] | @tsv' video.json
Handle errors and rate limits
-f makes curl exit non-zero on HTTP errors so set -e stops the script. Add --retry 3 --retry-all-errors for transient 500s; to honour 429 delays, capture headers with -D headers.txt and sleep for the retry-after value.
| Status | Meaning | What to do |
|---|---|---|
| 400 | Missing or malformed url parameter | Validate the URL before calling; don't retry |
| 401 | Missing, invalid or revoked API key | Check the key and the env var it is read from |
| 404 | Video is private, removed or region-locked | Tell the user; retrying will not help |
| 429 | Quota or rate limit exceeded | Wait for the Retry-After header, then retry |
| 500 | Upstream manifest fetch failed | Safe 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 —
downloadUrlstays valid for roughly six. - Use
-G --data-urlencodeso URLs with&or?are encoded correctly.
FAQ
How do I download a whole list of URLs?
Loop over a file: while read -r u; do ./resolve.sh "$u"; done < urls.txt, and stay within your rate limit. See bulk downloads.
Does this work on Windows?
Windows 10+ ships curl.exe. For a native approach see the PowerShell guide.
Related
Last updated 2026-10-25