Video download API for PowerShell: YouTube, TikTok, Instagram and more
PowerShell's Invoke-RestMethod parses JSON into objects automatically, which makes CrawlFetch a two-liner on Windows, macOS or Linux (PowerShell 7).
What you need
- A CrawlFetch API key — create one free (1,000 free credits on signup plus 500 a month, no card).
- PowerShell 7 (Windows PowerShell 5.1 also works).
- The key stored in an environment variable named
CRAWLFETCH_KEY, never in source control.
Resolve a video URL in PowerShell
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.
param([string]$Url = "https://youtu.be/aqz-KE-bpKQ")$query = @{ url = $Url; format = "mp4"; res = "1080p" }$headers = @{ Authorization = "Bearer $env:CRAWLFETCH_KEY" }$video = Invoke-RestMethod -Uri "https://api.crawlfetch.com/v1/resolve" -Body $query -Headers $headers"{0} ({1}) — {2} formats" -f $video.title, $video.bestQuality, $video.formats.Count$video.formats | Format-Table label, container, kind, sizeMb
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.
$ProgressPreference = "SilentlyContinue" # much faster downloads in 5.1Invoke-WebRequest -Uri $video.downloadUrl -OutFile "$($video.id).mp4"
Handle errors and rate limits
Wrap the call in try { } catch { } and read $_.Exception.Response.StatusCode. PowerShell 7 can retry for you: Invoke-RestMethod -MaximumRetryCount 3 -RetryIntervalSec 5.
| 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. - Store the key with
[Environment]::SetEnvironmentVariable("CRAWLFETCH_KEY", "…", "User")instead of in the script.
FAQ
Why is Invoke-WebRequest slow?
In Windows PowerShell 5.1 the progress bar slows downloads dramatically; set $ProgressPreference = 'SilentlyContinue'.
Related
Last updated 2026-10-25