Use the video API from Node.js
This example runs on a Node.js server. The API key is read from an environment variable and is not exposed to the browser or written to task output.
Complete example
const baseUrl = process.env.VIDEO_API_BASE
const apiKey = process.env.VIDEO_API_KEY
async function call(path, init = {}) {
const response = await fetch(`${baseUrl}${path}`, {
...init,
headers: {
Authorization: `Bearer ${apiKey}`,
Accept: 'application/json',
...(init.body ? { 'Content-Type': 'application/json' } : {}),
...init.headers,
},
})
const body = await response.json()
if (!response.ok || body.error) {
throw new Error(JSON.stringify(body))
}
return body
}
export async function createAndWait() {
let task = await call('/v1/videos', {
method: 'POST',
headers: { 'Idempotency-Key': crypto.randomUUID() },
body: JSON.stringify({
model: 'seedance2.0',
prompt: 'An orange cat watching rain from a windowsill, slow camera push-in',
seconds: 5,
resolution: '720p',
aspect_ratio: '16:9',
}),
})
while (task.status !== 'completed' && task.status !== 'failed') {
await new Promise((resolve) => setTimeout(resolve, 4000))
task = await call(`/v1/videos/${task.id}`)
}
if (task.status === 'failed') {
throw new Error(task.error?.message || 'video generation failed')
}
return task
}Integration checklist
- Keep VIDEO_API_BASE and VIDEO_API_KEY in server-side configuration.
- Use /v1/models to confirm available models for the key.
- Generate a UUID idempotency key for each business submission.
- Poll every 3 to 5 seconds until completed or failed.
- Download metadata.url and store the result in durable storage.
Never expose the key in a browser
This example is for Node.js server code. Calling the model API directly from a browser exposes the key to users and is not a production pattern.
Task states and errors
Review idempotency, rate limits, and response codes.
API reference
Return to request fields, modes, and security.