快速开始
下面以 seedance2.0 为例,生成一段 5 秒、720p 的视频。准备好 API Key 后,选择 cURL 或 Node.js 运行示例。
开始生成图片
图片使用同步接口,查看 GPT-Image 的 cURL 与 Node.js 完整示例。
1. 准备 API Key
登录控制台,在“钱包”查看余额,再到“API 密钥”创建密钥。将下方 VIDEO_API_KEY 的值换成你的密钥。
export VIDEO_API_BASE="https://ling-light.com"
export VIDEO_API_KEY="sk-your-api-key"
# 每个新业务请求生成一次;重试时沿用同一个值。
export VIDEO_REQUEST_ID="$(uuidgen)"cURL 示例可直接在终端运行。Node.js 示例需要 22 或更高版本,使用内置 fetch,无需安装 SDK;保存为 .mjs 文件后,在设置环境变量的终端中执行 node 文件名.mjs。请将 API Key 保存在服务端。
2. 确认模型
下方示例使用 seedance2.0,时长 5 秒、分辨率 720p。你也可以查询当前密钥可用的模型,替换 model,并按模型要求调整时长和分辨率。
模型列表与价格
查看模型说明、分辨率和价格。
curl "$VIDEO_API_BASE/v1/models" \
--header "Authorization: Bearer $VIDEO_API_KEY"const baseUrl = process.env.VIDEO_API_BASE
const apiKey = process.env.VIDEO_API_KEY
if (!baseUrl || !apiKey) throw new Error('Set VIDEO_API_BASE and VIDEO_API_KEY first')
async function call(path, init = {}) {
const response = await fetch(new URL(path, baseUrl), {
...init,
signal: AbortSignal.timeout(30_000),
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(`HTTP ${response.status}: ${body.message || body.error?.message || 'Request failed'}`)
}
return body
}
console.log(await call('/v1/models'))3. 创建第一条视频任务
curl --request POST "$VIDEO_API_BASE/v1/videos" \
--header "Authorization: Bearer $VIDEO_API_KEY" \
--header "Content-Type: application/json" \
--header "Idempotency-Key: $VIDEO_REQUEST_ID" \
--data '{
"model": "seedance2.0",
"prompt": "一只橘猫坐在窗台上看雨,镜头缓慢推近",
"seconds": 5,
"resolution": "720p",
"aspect_ratio": "16:9"
}'const baseUrl = process.env.VIDEO_API_BASE
const apiKey = process.env.VIDEO_API_KEY
if (!baseUrl || !apiKey) throw new Error('Set VIDEO_API_BASE and VIDEO_API_KEY first')
async function call(path, init = {}) {
const response = await fetch(new URL(path, baseUrl), {
...init,
signal: AbortSignal.timeout(30_000),
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(`HTTP ${response.status}: ${body.message || body.error?.message || 'Request failed'}`)
}
return body
}
// 重试同一业务请求时,沿用此 ID 和完全相同的请求体。
const requestId = process.env.VIDEO_REQUEST_ID
if (!requestId) throw new Error('Set VIDEO_REQUEST_ID first')
const task = await call('/v1/videos', {
method: 'POST',
headers: { 'Idempotency-Key': requestId },
body: JSON.stringify({
"model": "seedance2.0",
"prompt": "一只橘猫坐在窗台上看雨,镜头缓慢推近",
"seconds": 5,
"resolution": "720p",
"aspect_ratio": "16:9"
}),
})
console.log(task){
"id": "task_xxxxxxxxxxxxxxxx",
"object": "video",
"model": "seedance2.0",
"status": "queued",
"progress": 0,
"seconds": "5",
"metadata": {
"resolution": "720p",
"aspect_ratio": "16:9"
}
}将返回的 id 保存到 VIDEO_TASK_ID,再运行下方查询示例。超时重试时,请沿用本次的 VIDEO_REQUEST_ID 和请求体;创建新任务时再生成新的 VIDEO_REQUEST_ID。
4. 等待任务完成
# 替换为创建任务响应中的 id。
export VIDEO_TASK_ID="task_xxxxxxxxxxxxxxxx"
# 每 3–5 秒执行一次,直到状态为 completed 或 failed。
curl "$VIDEO_API_BASE/v1/videos/$VIDEO_TASK_ID" \
--header "Authorization: Bearer $VIDEO_API_KEY"const baseUrl = process.env.VIDEO_API_BASE
const apiKey = process.env.VIDEO_API_KEY
if (!baseUrl || !apiKey) throw new Error('Set VIDEO_API_BASE and VIDEO_API_KEY first')
async function call(path, init = {}) {
const response = await fetch(new URL(path, baseUrl), {
...init,
signal: AbortSignal.timeout(30_000),
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(`HTTP ${response.status}: ${body.message || body.error?.message || 'Request failed'}`)
}
return body
}
const taskId = process.env.VIDEO_TASK_ID
if (!taskId) throw new Error('Set VIDEO_TASK_ID to the id from the create response')
const deadline = Date.now() + 20 * 60_000
let task
while (Date.now() < deadline) {
task = await call(`/v1/videos/${encodeURIComponent(taskId)}`)
if (task.status === 'failed') throw new Error(task.error?.message || 'Video generation failed')
if (task.status === 'completed') break
await new Promise(resolve => setTimeout(resolve, 4000))
}
if (task?.status !== 'completed') {
throw new Error('Polling stopped after 20 minutes. Resume querying the same task ID; do not create another task.')
}
console.log(task.metadata?.url){
"id": "task_xxxxxxxxxxxxxxxx",
"object": "video",
"model": "seedance2.0",
"status": "completed",
"progress": 100,
"seconds": "5",
"metadata": {
"resolution": "720p",
"aspect_ratio": "16:9",
"url": "https://media.example.com/videos/task_xxxxxxxxxxxxxxxx.mp4"
}
}5. 获取视频
状态为 completed 时,视频地址位于 metadata.url;状态为 failed 时,停止查询并查看 error.message。公共视频直链无需 API Key;如果返回本站 /v1/videos/{id}/content 地址,请在服务端携带创建任务时的 API Key 请求。需要保留的视频,请及时下载。