Next.js: download YouTube, TikTok and Instagram videos
In Next.js the CrawlFetch key belongs on the server. A Route Handler (or your own backend) resolves the URL and returns only the fields the page needs; the browser then downloads from the returned link.
How it fits together
The browser posts a video URL to /api/resolve; the handler calls GET /v1/resolve with your secret key and returns title and downloadUrl. Nothing secret reaches the client bundle.
Next.js example
export async function GET(req: Request) {const url = new URL(req.url).searchParams.get("url");if (!url) return Response.json({ error: "url is required" }, { status: 400 });const r = await fetch("https://api.crawlfetch.com/v1/resolve?" + new URLSearchParams({ url }), {headers: { Authorization: "Bearer " + process.env.CRAWLFETCH_KEY },cache: "no-store",});const video = await r.json();return Response.json({ title: video.title, thumbnail: video.thumbnail, downloadUrl: video.downloadUrl },{ status: r.status },);}
Notes for Next.js
- Store
CRAWLFETCH_KEYwithout theNEXT_PUBLIC_prefix so it is never inlined into client JavaScript. - Add your own rate limit (per IP or per user) on the handler — otherwise anyone can spend your quota.
- If your architecture keeps APIs in a separate backend, put the same call in that service and fetch it from Next.js.
FAQ
Can I call the API from a Client Component?
No — that would ship your key to every visitor. Always go through a server-side handler.
Should I cache the response?
Short-term caching is fine; downloadUrl stays valid for about six hours.
Related
Last updated 2026-10-25