> ## 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.

# Sora 2 API クイックスタート: CometAPI で動画を生成する

> CometAPI を使って Sora 2 の動画ジョブを作成し、ステータスをポーリングして、完了した動画コンテンツを curl、Python、または Node.js でダウンロードします。

## 作成するもの

Sora 2 の動画ジョブを送信し、返された動画 ID を保存し、ジョブが完了するまでポーリングして、完成した動画コンテンツをダウンロードします。

## 前提条件

* `COMETAPI_KEY` に保存された CometAPI API キー
* `requests` を使用する Python 3.10+、または Node.js 18+
* ポーリング用のサーバーサイドワーカーまたはジョブキュー

## API キー、ベース URL、認証

Sora ジョブの作成:

```text theme={null}
POST https://api.cometapi.com/v1/videos
```

ステータスのポーリング:

```text theme={null}
GET https://api.cometapi.com/v1/videos/<video_id>
```

完了したコンテンツのダウンロード:

```text theme={null}
GET https://api.cometapi.com/v1/videos/<video_id>/content
```

Bearer トークンで認証します:

```text theme={null}
Authorization: Bearer $COMETAPI_KEY
```

## コード例

以下のタブから、cURL、Python、Node.js のコピー可能な例を使用できます。

<CodeGroup>
  ```bash cURL theme={null}
  curl https://api.cometapi.com/v1/videos \
    -H "Authorization: Bearer $COMETAPI_KEY" \
    -F model=sora-2 \
    -F "prompt=A paper boat drifts across a calm pond at sunrise" \
    -F seconds=4 \
    -F size=1280x720

  curl "https://api.cometapi.com/v1/videos/<video_id>" \
    -H "Authorization: Bearer $COMETAPI_KEY"

  curl "https://api.cometapi.com/v1/videos/<video_id>/content" \
    -H "Authorization: Bearer $COMETAPI_KEY" \
    --output sora-result.mp4
  ```

  ```python Python theme={null}
  import os
  import time
  from pathlib import Path

  import requests

  api_key = os.environ["COMETAPI_KEY"]
  headers = {"Authorization": f"Bearer {api_key}"}

  create_response = requests.post(
      "https://api.cometapi.com/v1/videos",
      headers=headers,
      data={
          "model": "sora-2",
          "prompt": "A paper boat drifts across a calm pond at sunrise",
          "seconds": "4",
          "size": "1280x720",
      },
      timeout=60,
  )
  create_response.raise_for_status()
  video_id = create_response.json()["id"]

  for _ in range(60):
      status_response = requests.get(
          f"https://api.cometapi.com/v1/videos/{video_id}",
          headers=headers,
          timeout=30,
      )
      status_response.raise_for_status()
      status = status_response.json()
      if status["status"] == "completed":
          content_response = requests.get(
              f"https://api.cometapi.com/v1/videos/{video_id}/content",
              headers=headers,
              timeout=120,
          )
          content_response.raise_for_status()
          Path("sora-result.mp4").write_bytes(content_response.content)
          break
      if status["status"] == "failed":
          raise RuntimeError(status)
      time.sleep(5)
  else:
      raise TimeoutError("Sora job did not finish in time")
  ```

  ```javascript Node.js theme={null}
  import fs from "node:fs/promises";

  const form = new FormData();
  form.append("model", "sora-2");
  form.append("prompt", "A paper boat drifts across a calm pond at sunrise");
  form.append("seconds", "4");
  form.append("size", "1280x720");

  const createResponse = await fetch("https://api.cometapi.com/v1/videos", {
    method: "POST",
    headers: { Authorization: `Bearer ${process.env.COMETAPI_KEY}` },
    body: form,
  });

  if (!createResponse.ok) {
    throw new Error(await createResponse.text());
  }

  const { id } = await createResponse.json();

  for (let attempt = 0; attempt < 60; attempt += 1) {
    const statusResponse = await fetch(`https://api.cometapi.com/v1/videos/${id}`, {
      headers: { Authorization: `Bearer ${process.env.COMETAPI_KEY}` },
    });

    if (!statusResponse.ok) {
      throw new Error(await statusResponse.text());
    }

    const status = await statusResponse.json();
    if (status.status === "completed") {
      const contentResponse = await fetch(
        `https://api.cometapi.com/v1/videos/${id}/content`,
        { headers: { Authorization: `Bearer ${process.env.COMETAPI_KEY}` } },
      );
      const videoBuffer = Buffer.from(await contentResponse.arrayBuffer());
      await fs.writeFile("sora-result.mp4", videoBuffer);
      break;
    }
    if (status.status === "failed") {
      throw new Error(JSON.stringify(status));
    }
    await new Promise((resolve) => setTimeout(resolve, 5000));
  }
  ```
</CodeGroup>

## フローの説明

Sora の生成は非同期です。create エンドポイントは video ID と初期 status を返します。`status` が `completed` または `failed` になるまで `GET /v1/videos/<video_id>` をポーリングしてください。ジョブが完了したら、`GET /v1/videos/<video_id>/content` でファイルをダウンロードします。

正確な `WxH` サイズを使用してください。Sora API リファレンスには標準的な横長および縦長のサイズが記載されており、Pro model ワークフロー向けにはより大きい Pro サイズも用意されています。

## 共通パラメータ

| Parameter         | 用途                                                     |
| ----------------- | ------------------------------------------------------ |
| `model`           | Sora の model ID。リファレンスの例では `sora-2` を使用しています。          |
| `prompt`          | 動画用のテキストプロンプト（Prompt）。                                 |
| `seconds`         | クリップの長さ。API リファレンスでは `4`、`8`、`12`、`16`、`20` が記載されています。 |
| `size`            | `1280x720` や `720x1280` のような、正確な `WxH` 出力サイズ。          |
| `input_reference` | 先頭フレームのワークフローで使用する任意の参照画像ファイル。                         |

## トラブルシューティング / FAQ

<AccordionGroup>
  <Accordion title="作成リクエストが失敗する">
    multipart form data を使用してください。リファレンスにある Sora の作成リクエストでは、JSON body ではなくフォームフィールドを使用します。
  </Accordion>

  <Accordion title="コンテンツのダウンロードが失敗する">
    status endpoint が `completed` を返してからコンテンツをダウンロードしてください。生成完了したファイルは自分のストレージに保存してください。
  </Accordion>

  <Accordion title="Pro サイズが動作しない">
    より大きい Pro サイズは、Pro model のワークフローでのみ使用してください。最初のリクエストでは `1280x720` から始めてください。
  </Accordion>
</AccordionGroup>

## 次のステップ

* [Sora 2 動画を作成する API リファレンス](/api/video/sora-2/create)を読む。
* [Sora 2 動画を取得する](/api/video/sora-2/retrieve) でポーリングする。
* [Sora 2 動画コンテンツを取得する](/api/video/sora-2/retrieve-content) でダウンロードする。
* 利用可能な動画モデルを [Models](/ja/overview/models) で確認する。
* [動画生成でポーリングと webhook を使用する](/ja/guides/webhook-and-polling-for-video-generation)を確認する。
* [モデルを呼び出す前にリクエストコストを見積もる](/ja/guides/how-to-estimate-cost-before-calling-a-model) でタスクコストを見積もる。
