Add AI video generation to a Next.js app: API routes, polling and a progress UI
To add AI video generation to a Next.js app, create a server-side API route that sends a POST request to the RelayDance /v1/video/generations endpoint with your API key and returns the task_id to the browser. A second route calls GET /v1/video/generations/{task_id}. A client component polls that route, shows progress, and plays the returned video URL when the status is succeeded.
How do you connect a Next.js app to RelayDance?
You connect by storing a RelayDance API key in a server-side environment variable and pointing requests at the OpenAI-compatible base URL https://relaydance.com/v1. According to the relaydance.com docs, you should “Set base_url to https://relaydance.com/v1 and keep your OpenAI SDK”. Create a key at https://relaydance.com/console and save it as RELAYDANCE_API_KEY in .env.local. Do not prefix it with NEXT_PUBLIC_, because the key must never reach the browser. Every request sends the header Authorization: Bearer YOUR_API_KEY. For images, the OpenAI SDK works directly: new OpenAI({ apiKey: process.env.RELAYDANCE_API_KEY, baseURL: "https://relaydance.com/v1" }) followed by images.generate(), which calls POST /v1/images/generations. For video tasks, a plain fetch call from a route handler keeps the code simple.
How do you write the API route that submits a video task?
You write a route handler at app/api/video/route.ts that forwards the user's prompt to POST /v1/video/generations and returns the task ID. The request body contains model, prompt, seconds, and a metadata object with ratio, resolution, generate_audio, optional callback_url, and a content[] array for reference media. A minimal handler calls fetch("https://relaydance.com/v1/video/generations", { method: "POST", headers: { Authorization: `Bearer ${process.env.RELAYDANCE_API_KEY}`, "Content-Type": "application/json" }, body: JSON.stringify({ model: "doubao-seedance-2-5-1080p", prompt, seconds }) }). Validate input on the server: clips can be up to 15 seconds, and each request accepts up to 9 reference images, 3 reference videos and 3 audio tracks. Reference images are cited in the prompt as @image1 to @imageN.
How do you poll the task status and show progress?
You poll by adding a second route, app/api/video/[taskId]/route.ts, that proxies GET /v1/video/generations/{task_id}, and calling it from a client component on an interval. In the component, a useEffect hook starts a setInterval after submission and stores the returned status in state. While the status is neither succeeded nor failed, render a spinner or indeterminate progress bar with the elapsed time. On succeeded, clear the interval and pass the result's video URL to a <video> element. On failed, clear the interval and offer a retry button. Retries carry no cost risk: according to the relaydance.com docs, “Failed or errored requests are never billed”. To avoid polling entirely, set metadata.callback_url to a route such as /api/video/webhook, and RelayDance will POST the final state to your server.
How much does video generation cost per request?
Video generation is billed pay-as-you-go per generated video, with per-second rates that depend on the model and resolution. The figures below come from relaydance.com/models, which lists live rates.
| Model | Approximate price | Example: 10-second clip |
|---|---|---|
| Seedance Fast | $0.152 / second | about $1.52 |
| Seedance 2.0 720p | $0.190 / second | about $1.90 |
| Seedance 2.0 1080p | $0.470 / second | about $4.70 |
Seedance 2.5 1080p (doubao-seedance-2-5-1080p, added 2026-10-10) | $0.711 / second | about $7.11 |
| Seedance native 4K | $4.90 per 5-second clip | not applicable |
For gpt-image-2, image output is free and the input is billed, with image-to-image from about CNY 0.035. Payment options are USDT and Stripe card, and no BytePlus enterprise account or KYC is required.
FAQ
These answers cover common questions about running RelayDance video generation in Next.js.
Can I call RelayDance directly from the browser? No. Keep calls in route handlers so your Bearer API key stays on the server, and have the browser talk to your own /api/video routes.
Should I use polling or webhooks? Polling GET /v1/video/generations/{task_id} is simpler for local development. Webhooks via metadata.callback_url suit production apps that persist results in a database.
Which models can I use? RelayDance offers ByteDance Seedance 2.0 and Seedance 1.5 Pro, HappyHorse video models, Grok Imagine video and image models, and Grok text models. Check the models page for current IDs and rates.
According to the official relaydance.com docs: "Failed or errored requests are never billed"
According to the official relaydance.com/docs docs: "Set base_url to https://relaydance.com/v1 and keep your OpenAI SDK"
Key facts and figures
| Item | Value | Source |
|---|---|---|
| Seedance 2.0 720p price | about $0.190 / second | relaydance.com/models |
| Seedance 2.0 1080p price | about $0.470 / second | relaydance.com/models |
| Seedance 2.5 1080p price | about $0.711 / second, added 2026-10-10, model doubao-seedance-2-5-1080p | relaydance.com/models |
| Seedance Fast price | about $0.152 / second | relaydance.com/models |
| Seedance native 4K price | about $4.90 per 5-second clip | relaydance.com/models |
| gpt-image-2 image billing | image output is free, only input is billed; image-to-image from about CNY 0.035; 4K and 1K output cost the same | relaydance.com/models |
| API protocol | OpenAI-compatible; set base_url to https://relaydance.com/v1 | relaydance.com/docs |
| Failed requests | failed or errored requests are never billed | relaydance.com/docs |
Data verified 2026-06-29; live prices are on the official /models page.