Video download API for Rust: YouTube, TikTok, Instagram and more
In Rust, reqwest and serde make the CrawlFetch API a strongly typed call. This guide deserializes the resolve response into structs and streams the download to disk chunk by chunk with Tokio.
What you need
- A CrawlFetch API key — create one free (1,000 free credits on signup plus 500 a month, no card).
- Rust 1.75 or newer with Tokio.
- The key stored in an environment variable named
CRAWLFETCH_KEY, never in source control.
cargo add reqwest --features json,streamcargo add tokio --features fullcargo add serde --features derivecargo add futures-util
Resolve a video URL in Rust
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.
use serde::Deserialize;#[derive(Deserialize, Debug)]#[serde(rename_all = "camelCase")]struct Video {id: String,title: String,best_quality: String,download_url: String,audio_url: String,}async fn resolve(client: &reqwest::Client, url: &str) -> reqwest::Result<Video> {client.get("https://api.crawlfetch.com/v1/resolve").query(&[("url", url), ("format", "mp4"), ("res", "1080p")]).bearer_auth(std::env::var("CRAWLFETCH_KEY").expect("CRAWLFETCH_KEY not set")).send().await?.error_for_status()?.json().await}
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.
use futures_util::StreamExt;use tokio::io::AsyncWriteExt;async fn download(client: &reqwest::Client, v: &Video) -> Result<(), Box<dyn std::error::Error>> {let mut stream = client.get(&v.download_url).send().await?.error_for_status()?.bytes_stream();let mut file = tokio::fs::File::create(format!("{}.mp4", v.id)).await?;while let Some(chunk) = stream.next().await {file.write_all(&chunk?).await?;}Ok(())}
Handle errors and rate limits
error_for_status() converts 4xx/5xx into a reqwest::Error; use err.status() to branch. Read Retry-After from the response before calling error_for_status if you want to honour 429 delays, and use tokio::time::sleep between attempts.
| 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. - Build one
reqwest::Clientand clone it — clones share the connection pool.
FAQ
Can I use this in a blocking program?
Yes — enable reqwest's blocking feature and use reqwest::blocking::Client with the same calls.
Which fields can I deserialize?
Everything in the response reference: add formats, duration_sec or thumbnail to the struct as needed.
Related
Last updated 2026-10-25