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;
}
// Collect competitor ad creatives into your swipe file
async function collectCompetitorAds(campaigns: { brand: string; url: string }[]) {
for (const campaign of campaigns) {
const item = await download(campaign.url, { quality: "1080p" });
console.log(`${campaign.brand}: ${item.title} → ${item.download_url}`);
}
}
collectCompetitorAds([{ brand: "Competitor", url: "https://facebook.com/watch?v=123" }]);