Skip to main content
NODE.JS

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.

Last updated

Complete example

create-and-wait.jsjs
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

  1. Keep VIDEO_API_BASE and VIDEO_API_KEY in server-side configuration.
  2. Use /v1/models to confirm available models for the key.
  3. Generate a UUID idempotency key for each business submission.
  4. Poll every 3 to 5 seconds until completed or failed.
  5. Download metadata.url and store the result in durable storage.
Task states and errors
Review idempotency, rate limits, and response codes.
API reference
Return to request fields, modes, and security.