Production-ready TypeScript code using the REST API, with your key from the dashboard
production-sourcing.ts Copyconst 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;
}
// Gather source footage in the highest quality for the edit
async function collectSourceFootage(sources: { shot: string; url: string }[]) {
for (const source of sources) {
const item = await download(source.url, { format: "mp4", quality: "best" });
console.log(`${source.shot}: ${item.download_url}`);
}
}
collectSourceFootage([{ shot: "B-roll city", url: "https://vimeo.com/123456" }]);