Video download API for TypeScript: YouTube, TikTok, Instagram and more
A typed client catches mistakes like reading video.url instead of video.downloadUrl at compile time. This guide defines the CrawlFetch response types once and wraps the API in a small, dependency-free TypeScript client you can drop into any Node, Bun, Deno or edge runtime.
What you need
- A CrawlFetch API key — create one free (1,000 free credits on signup plus 500 a month, no card).
- TypeScript 5.x.
- A runtime with global
fetch(Node 18+, Bun, Deno, Cloudflare Workers). - The key stored in an environment variable named
CRAWLFETCH_KEY, never in source control.
Resolve a video URL in TypeScript
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.
export interface MediaFormat {id: string;label: string; // "1080p", "audio"container: string; // "mp4", "webm", "m4a", "opus"kind: "video" | "audio";sizeMb: number;url: string;}export interface ResolvedVideo {id: string;title: string;channel: string;durationSec: number;thumbnail: string | null;bestQuality: string;formats: MediaFormat[];downloadUrl: string;audioUrl: string;}export class ApiError extends Error {constructor(public status: number, public retryAfter?: number) {super("CrawlFetch API error " + status);}}export async function resolve(url: string,opts: { format?: string; res?: string } = {},): Promise<ResolvedVideo> {const qs = new URLSearchParams({ url, ...opts });const r = await fetch("https://api.crawlfetch.com/v1/resolve?" + qs, {headers: { Authorization: "Bearer " + process.env.CRAWLFETCH_KEY },});if (!r.ok) {const retry = Number(r.headers.get("retry-after")) || undefined;throw new ApiError(r.status, retry);}return (await r.json()) as ResolvedVideo;}
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.
import { ApiError, resolve } from "./crawlfetch";try {const video = await resolve("https://youtu.be/aqz-KE-bpKQ", { format: "mp4", res: "720p" });const audio = video.formats.filter((f) => f.kind === "audio");console.log(video.title, "→", video.downloadUrl, audio.length, "audio formats");} catch (err) {if (err instanceof ApiError && err.status === 429) {console.warn("rate limited, retry in", err.retryAfter, "s");} else {throw err;}}
Handle errors and rate limits
Model failures as a typed ApiError carrying the status and Retry-After, as above, so callers can branch on err.status without string matching. Narrow unknown in catch with instanceof before reading fields.
| 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. - Validate the response at the boundary (e.g. with zod) if you persist it — types alone don't check runtime data.
- Mark the client module server-only in frameworks like Next.js so the key never reaches a client bundle.
FAQ
Are the response types official?
They mirror the documented response in the API reference. Copy them into your project; they are intentionally small.
Can I use this client in the browser?
No — it would expose your API key. Call it from a server route and return only what the browser needs. See calling the API from browser JavaScript.
Related
Last updated 2026-10-25