Production-ready TypeScript code using the REST API, with your key from the dashboard
const API = "https://videodownloadapi.com/api/v1";
const headers = {
Authorization: `Bearer ${process.env.VIDEO_API_KEY}`,
"Content-Type": "application/json",
};
type Download = { download_id: string; status: string; title: string; download_url?: string };
async function download(url: string, options: { format?: string; quality?: string } = {}) {
const res = await fetch(`${API}/downloads`, {
method: "POST",
headers,
body: JSON.stringify({ url, ...options }),
});
if (res.status === 402) throw new Error("Not enough minutes");
if (res.status === 429) throw new Error("Too many requests: retry in a few seconds");
let item: Download = await res.json();
// Poll until the file is ready (or failed: minutes are refunded automatically)
while (item.status !== "ready" && item.status !== "failed") {
await new Promise((resolve) => setTimeout(resolve, 3000));
item = await fetch(`${API}/downloads/${item.download_id}`, { headers }).then((r) => r.json());
}
if (item.status === "failed") throw new Error(`Download failed: ${url}`);
return item;
}
import { createHash } from "node:crypto";
// Preserve a video as evidence, with a SHA-256 fingerprint of the file
async function preserveEvidence(caseId: string, videoUrl: string) {
const item = await download(videoUrl, { quality: "best" });
const file = Buffer.from(await (await fetch(item.download_url!)).arrayBuffer());
const sha256 = createHash("sha256").update(file).digest("hex");
console.log({ caseId, source: videoUrl, title: item.title, sha256, collectedAt: new Date() });
}
preserveEvidence("CASE-2026-001", "https://x.com/user/status/123");