> ## Documentation Index
> Fetch the complete documentation index at: https://apidoc.cometapi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 使用轮询和 webhook 进行视频生成

> 通过存储任务 ID、检查任务状态以及处理回调投递失败，使用轮询和 webhook 跟踪 CometAPI 视频生成。

将轮询作为视频生成的基础方案，因为每个异步任务都会返回一个可供查询的任务 ID。只有当所选视频端点支持回调 URL 时才添加 webhook，并继续将轮询作为漏发回调或提供商特定回调投递情况下的事实来源。

## 创建视频任务

以下请求会创建一个最小化的视频任务，并存储返回的 ID。仅当所选模型页面文档说明了这些字段时，才添加时长、分辨率或回调字段。

```bash theme={null}
curl https://api.cometapi.com/v1/videos \
  -H "Authorization: Bearer $COMETAPI_KEY" \
  -F "model=doubao-seedance-2-0" \
  -F "prompt=A cinematic shot of a paper airplane crossing a desk"
```

响应中包含任务 ID 和状态：

```json theme={null}
{
  "id": "task_example",
  "task_id": "task_example",
  "object": "video",
  "model": "doubao-seedance-2-0",
  "status": "queued",
  "progress": 0,
  "created_at": 1779872000
}
```

## 轮询状态

以下请求会检查视频任务状态：

```bash theme={null}
curl https://api.cometapi.com/v1/videos/task_example \
  -H "Authorization: Bearer $COMETAPI_KEY"
```

响应会随着任务推进而变化。完成后的响应在模型适配器提供结果 URL 时可以包含 `video_url`；否则，请使用模型特定的结果字段，或者在该模型支持代理下载时使用 `/v1/videos/{id}/content` 内容路由。

```json theme={null}
{
  "id": "task_example",
  "object": "video",
  "model": "doubao-seedance-2-0",
  "status": "completed",
  "progress": 100,
  "completed_at": 1779872300,
  "video_url": "<generated-video-url>"
}
```

## 接收 webhook

CometAPI 并未为每一种视频模型定义统一的回调负载。应将回调视为提供商特定的透传事件，存储原始请求体，并通过轮询核对最终状态。

以下 Express 处理程序会接收视频回调并存储事件：

```javascript theme={null}
import express from "express";

const app = express();
app.use(express.json({ limit: "2mb" }));

app.post("/cometapi/video-webhook", async (request, response) => {
  const event = request.body;

  console.log("Task ID:", event.task_id || event.id);
  console.log("Status:", event.status);

  response.status(200).json({ received: true });
});

app.listen(3000);
```

回调负载通常包含任务标识和状态字段，但具体嵌套结构取决于所选模型或提供商：

```json theme={null}
{
  "task_id": "task_example",
  "status": "completed",
  "progress": 100,
  "result": {
    "video_url": "https://example.com/result.mp4"
  }
}
```

## 常见错误

| Error          | Fix                                                                     |
| -------------- | ----------------------------------------------------------------------- |
| 回调丢失           | 按任务 ID 轮询，直到你的应用已存储终态。                                                  |
| 重复回调           | 通过任务 ID 让回调处理具备幂等性。                                                     |
| 回调被拒绝          | 尽快返回 `2xx` 响应，然后在后台处理任务。                                                |
| 提供商特定负载不匹配     | 存储原始回调负载，并在你的应用中将其标准化。                                                  |
| 缺少 `video_url` | 将 `video_url` 视为可选字段，并在可用时结合轮询以及模型特定结果字段或 `/v1/videos/{id}/content` 使用。 |

## 相关链接

* [视频模型](/api/video)
* [创建视频](/api/video/sora-2/create)
* [获取视频](/api/video/sora-2/retrieve)
* [模型页面](/zh-Hans/overview/models)
* [模型目录](https://www.cometapi.com/models/)
* [定价](https://www.cometapi.com/pricing/)

<script type="application/ld+json">
  {`
    {
    "@context": "https://schema.org",
    "@graph": [
      {
        "@type": "TechArticle",
        "@id": "https://apidoc.cometapi.com/guides/webhook-and-polling-for-video-generation",
        "headline": "如何使用 webhook 和轮询进行视频生成？",
        "description": "通过存储任务 ID、检查任务状态以及处理回调投递失败，使用轮询和 webhook 进行 CometAPI 视频生成。",
        "url": "https://apidoc.cometapi.com/guides/webhook-and-polling-for-video-generation",
        "author": {
          "@type": "Organization",
          "name": "CometAPI"
        },
        "publisher": {
          "@type": "Organization",
          "name": "CometAPI",
          "url": "https://www.cometapi.com"
        }
      },
      {
        "@type": "BreadcrumbList",
        "itemListElement": [
          {
            "@type": "ListItem",
            "position": 1,
            "name": "CometAPI 文档",
            "item": "https://apidoc.cometapi.com/"
          },
          {
            "@type": "ListItem",
            "position": 2,
            "name": "指南",
            "item": "https://apidoc.cometapi.com/guides/use-cometapi-with-openai-sdk"
          },
          {
            "@type": "ListItem",
            "position": 3,
            "name": "如何使用 webhook 和轮询进行视频生成？",
            "item": "https://apidoc.cometapi.com/guides/webhook-and-polling-for-video-generation"
          }
        ]
      }
    ]
    }
    `}
</script>
