Video download API for Node.js: YouTube, TikTok, Instagram and more
Node.js projects have historically relied on ytdl-core and its forks, which break whenever YouTube changes its player. With CrawlFetch you use the built-in fetch (Node 18+) against a stable REST endpoint and stream the result to disk with node:stream — no native dependencies, no ffmpeg required to get the URL.
What you need
- A CrawlFetch API key — create one free (1,000 free credits on signup plus 500 a month, no card).
- Node.js 18 or newer (global
fetchand web streams). - The key stored in an environment variable named
CRAWLFETCH_KEY, never in source control.
Resolve a video URL in Node.js
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.
const API = "https://api.crawlfetch.com";export async function resolve(url, { format = "mp4", res = "1080p" } = {}) {const qs = new URLSearchParams({ url, format, res });const r = await fetch(API + "/v1/resolve?" + qs, {headers: { Authorization: "Bearer " + process.env.CRAWLFETCH_KEY },});if (!r.ok) throw new Error("resolve failed with " + r.status);return r.json();}const video = await resolve("https://youtu.be/aqz-KE-bpKQ");console.log(video.title, video.bestQuality, video.formats.length);
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 { createWriteStream } from "node:fs";import { Readable } from "node:stream";import { pipeline } from "node:stream/promises";export async function download(video, path) {const r = await fetch(video.downloadUrl);if (!r.ok || !r.body) throw new Error("download failed with " + r.status);await pipeline(Readable.fromWeb(r.body), createWriteStream(path));}await download(video, video.id + ".mp4");
Handle errors and rate limits
fetch does not throw on HTTP errors, so always check r.ok. On 429 read r.headers.get("retry-after") and wait that many seconds; on 500 retry with exponential backoff. Use AbortSignal.timeout(15000) as the signal option so a slow request never hangs a worker.
| 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. - Keep the key in
process.envand load it with--env-file=.env(Node 20.6+) or your platform's secret store. - When proxying downloads to users, pipe the stream — don't
await r.arrayBuffer()on multi-GB files.
FAQ
What should I use instead of ytdl-core?
If you only need metadata and a download URL, a hosted API removes the moving parts entirely. See ytdl-core alternative.
Is there an npm package?
The REST API needs nothing beyond fetch. The examples on this page are the whole integration.
Does this work with Bun or Deno?
Yes — both ship a standard fetch. See the Deno guide.
Related
Last updated 2026-10-25