Polling for results
A minimal, correct polling loop.
There's no webhook or server-sent event yet — polling Retrieve a video generation is the only way to know when a generation finishes. This guide is a minimal loop that does it correctly: backs off, has a hard timeout, and distinguishes retryable errors from ones that mean stop.
async function waitForVideo(id, { timeoutMs = 5 * 60_000, intervalMs = 3000 } = {}) {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
const res = await fetch(`https://api.swishx.com/v1/videos/${id}`, {
headers: { Authorization: `Bearer ${process.env.SWISHX_API_KEY}` },
});
if (!res.ok && res.status !== 429) {
throw new Error(`Unexpected response: ${res.status}`);
}
const video = await res.json();
if (video.status === "succeeded") return video;
if (video.status === "failed" || video.status === "canceled") {
throw new Error(video.error?.message ?? `Generation ${video.status}`);
}
await new Promise((r) => setTimeout(r, intervalMs));
}
throw new Error(`Timed out waiting for ${id}`);
}Why 3 seconds
Most generations move through all six pipeline stages in under two minutes. Polling faster than every ~2–3 seconds mostly burns your rate limit budget without getting you the result any sooner — see Rate limits for the exact per-minute cap.
Don't poll forever
Always set a deadline. A generation stuck in processing well past a few minutes is unusual enough to be worth
surfacing as an error in your own system, not silently retried indefinitely.