> ## 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 進行影片生成

> 透過儲存 task ID、檢查任務狀態，以及處理回呼傳送失敗，使用輪詢與 webhook 追蹤 CometAPI 影片生成。

將輪詢作為影片生成的基準方式，因為每個非同步工作都會回傳可供查詢的 task ID。只有在所選的影片端點支援 callback URL 時才加入 webhook，並持續將輪詢作為遺漏回呼或處理供應商特定回呼傳送情況的事實來源。

## 建立影片任務

以下請求會建立一個最小化的影片任務，並儲存回傳的 ID。只有在所選模型頁面有說明這些欄位時，才加入 duration、resolution 或 callback 欄位。

```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"
```

回應會包含 task 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 並未為每一種影片模型定義單一通用的回呼 payload。請將回呼視為供應商特定的直通事件，儲存原始請求主體，並透過輪詢對齊最終狀態。

以下 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);
```

回呼 payload 通常會包含任務識別與狀態欄位，但實際巢狀結構取決於所選模型或供應商：

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

## 常見錯誤

| Error             | Fix                                                                  |
| ----------------- | -------------------------------------------------------------------- |
| 回呼遺失              | 依 task ID 持續輪詢，直到你的應用程式已儲存終態。                                        |
| 重複回呼              | 依 task ID 讓回呼處理具備冪等性。                                                |
| 回呼遭拒              | 先快速回傳 `2xx` 回應，再於背景處理該工作。                                            |
| 供應商特定 payload 不匹配 | 儲存原始回呼 payload，並在你的應用程式中將其正規化。                                       |
| 缺少 `video_url`    | 將 `video_url` 視為可選欄位，並在可用時搭配輪詢以及模型特定結果欄位或 `/v1/videos/{id}/content`。 |

## 相關連結

* [影片模型](/api/video)
* [建立影片](/api/video/sora-2/create)
* [取得影片](/api/video/sora-2/retrieve)
* [模型頁面](/zh-Hant/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、檢查任務狀態，以及處理回呼傳遞失敗，對 CometAPI 影片生成使用輪詢與 webhook。",
        "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>
