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

# 使用 Pika 2.5 文生视频与 Comfy Router

> 通过 Comfy Router 调用 pika/pika-2.5-text-to-video：endpoint、请求形状以及 Router 返回的响应。

`pika/pika-2.5-text-to-video` 的 API 参考，由 Comfy Router 从 Pika 提供。

**输入：** 文本 · **输出：** 视频

## 快速开始

在[你的 Comfy 工作区](https://platform.comfy.org/profile/api-keys?onboarding=router)中创建密钥，并将其导出为 `COMFY_API_KEY`。Python 和 TypeScript 代码片段使用 Comfy SDK（`pip install comfy-sdk` 和 `npm install @comfyorg/sdk`）；cURL 代码片段是通过原始 HTTP 进行的同一调用。

**模型 ID：** `pika/pika-2.5-text-to-video`

**端点：** `POST https://api.comfy.org/v2/models/pika/pika-2.5-text-to-video`

<Tabs defaultTabIndex={1}>
  <Tab title="等待结果">
    <CodeGroup>
      ```python Python theme={null}
      from comfy_sdk import Comfy

      # 从环境中读取 COMFY_API_KEY。
      # SDK 会自动创建幂等键，并在自动重试时复用它。
      with Comfy() as client:
          result = client.models.run(
              "pika/pika-2.5-text-to-video",
              {
                  "duration_s": 5,
                  "prompt": "A paper boat drifting down a rain-soaked city street at dusk",
                  "resolution": "720p",
              },
          )

      print(result)
      ```

      ```typescript TypeScript theme={null}
      import { comfy } from "@comfyorg/sdk";

      // 从环境中读取 COMFY_API_KEY。
      // SDK 会自动创建幂等键，并在自动重试时复用它。
      const { data } = await comfy.models.run("pika/pika-2.5-text-to-video", {
        duration_s: 5,
        prompt: "A paper boat drifting down a rain-soaked city street at dusk",
        resolution: "720p",
      });

      console.log(data);
      ```

      ```bash cURL theme={null}
      curl https://api.comfy.org/v2/models/pika/pika-2.5-text-to-video \
        -H "X-API-Key: $COMFY_API_KEY" \
        -H "Idempotency-Key: $(uuidgen)" \
        -H "Content-Type: application/json" \
        -d "{\"duration_s\": 5, \"prompt\": \"A paper boat drifting down a rain-soaked city street at dusk\", \"resolution\": \"720p\"}"
      ```
    </CodeGroup>
  </Tab>

  <Tab title="排队并稍后收集">
    相同的请求体，发送到 `POST https://api.comfy.org/v2/models/pika/pika-2.5-text-to-video/requests`。运行一经接纳，Router 便会返回 `201` 和 `request_id`，结果就绪后即可收集，无论是从当前进程还是其他进程。[队列投递](/zh/development/comfy-router/queue) 介绍了状态、取消和结果收集。

    <CodeGroup>
      ```python Python theme={null}
      import asyncio
      from comfy_sdk import AsyncComfy

      # 从环境中读取 COMFY_API_KEY。
      # 每次 submit() 调用都会生成自己的 Idempotency-Key，并在自动重试时复用它。
      async def main():
          async with AsyncComfy() as client:
              handle = await client.models.submit(
                  "pika/pika-2.5-text-to-video",
                  {
                      "duration_s": 5,
                      "prompt": "A paper boat drifting down a rain-soaked city street at dusk",
                      "resolution": "720p",
                  },
              )
              print("request_id:", handle.request_id)  # 与模型 ID 一起，就是另一个进程所需的全部信息

              # 轮询直到请求完成，等待服务器指定的 Retry-After。
              async for update in handle.iter_events():
                  print(update.status, update.queue_position)

              # 提供商自己的 payload，与 models.run() 返回的值相同。
              # 失败或已取消的请求会在此处抛出类型化的 Router 错误。
              result = await handle.get()

          print(result)

      asyncio.run(main())
      ```

      ```typescript TypeScript theme={null}
      import { comfy } from "@comfyorg/sdk";

      // 从环境中读取 COMFY_API_KEY。
      // 每次 submit() 调用都会生成自己的 Idempotency-Key，并在自动重试时复用它。
      const handle = await comfy.models.submit("pika/pika-2.5-text-to-video", {
        duration_s: 5,
        prompt: "A paper boat drifting down a rain-soaked city street at dusk",
        resolution: "720p",
      });
      console.log("requestId:", handle.requestId); // 与模型 ID 一起，就是另一个进程所需的全部信息

      // 轮询直到请求完成，等待服务器指定的 Retry-After。
      for await (const update of handle.events()) {
        console.log(update.status, update.queuePosition);
      }

      // 与 models.run() 返回的结果相同。失败或已取消的请求会在此处拒绝。
      const result = await handle.get();

      console.log(result.data);
      ```

      ```bash cURL theme={null}
      # 1. 提交。Router 返回 201，并包含 request_id、status_url、response_url 和 cancel_url。
      curl https://api.comfy.org/v2/models/pika/pika-2.5-text-to-video/requests \
        -H "X-API-Key: $COMFY_API_KEY" \
        -H "Idempotency-Key: $(uuidgen)" \
        -H "Content-Type: application/json" \
        -d "{\"duration_s\": 5, \"prompt\": \"A paper boat drifting down a rain-soaked city street at dusk\", \"resolution\": \"720p\"}"

      # 2. 轮询直到状态为 COMPLETED，每次响应中等待其指定的 Retry-After 秒数。
      REQUEST_ID="<request_id from the 201 body>"
      curl -i https://api.comfy.org/v2/models/pika/pika-2.5-text-to-video/requests/$REQUEST_ID/status \
        -H "X-API-Key: $COMFY_API_KEY"

      # 3. 收集。200 表示模型的原始输出，202 表示仍在运行时返回状态正文。
      curl https://api.comfy.org/v2/models/pika/pika-2.5-text-to-video/requests/$REQUEST_ID \
        -H "X-API-Key: $COMFY_API_KEY"
      ```
    </CodeGroup>
  </Tab>
</Tabs>

## Schema

### 输入

<ParamField body="duration_s" type="integer" default="5">
  视频长度，以秒为单位。文生视频仅生成五秒的片段。

  可能的值：`5`
</ParamField>

<ParamField body="negative_prompt" type="string">
  描述视频中应避免出现的内容的文本。
</ParamField>

<ParamField body="prompt" type="string" required>
  描述要生成的视频的文本提示词。
</ParamField>

<ParamField body="resolution" type="string" default="&#x22;1080p&#x22;">
  输出分辨率。

  可能的值：`720p`、`1080p`
</ParamField>

<ParamField body="seed" type="integer">
  用于可复现生成的种子。省略它（或发送 null）即可使用随机种子。
</ParamField>

该内容由 Router 在 `GET /v2/models/pika/pika-2.5-text-to-video/openapi.json` 提供的 schema 生成，也是它在请求到达提供商之前用于校验调用的同一份文档。

### 输出

<ResponseField name="error" type="object">
  Pika 的结构化失败记录，在 `failed` 任务上设置。Router 会以自己的错误响应作答失败任务，因此 Router 返回的响应体不会携带它；之所以声明它，是因为它是 Pika 任务文档的一部分。
</ResponseField>

<ResponseField name="error.code" type="string" required>
  可能的值：`invalid_input`、`content_moderation`、`provider_error`、`provider_timeout`、`provider_unavailable`、`rate_limited`、`insufficient_balance`、`membership_required`、`cycle_limit_exceeded`、`admission_suspended`、`usage_limit_exceeded`、`timed_out`、`internal`
</ResponseField>

<ResponseField name="error.message" type="string" required />

<ResponseField name="id" type="string" required>
  Pika 的任务ID。
</ResponseField>

<ResponseField name="model_id" type="string">
  该任务所运行的 Pika 目录条目。
</ResponseField>

<ResponseField name="output" type="object">
  已完成任务的媒体，在 `status` 为 `completed` 时设置。`media_type` 指明由哪个字段承载它：`image` 填充 `images`，`video` 填充 `video`，`videos` 填充 `videos`。
</ResponseField>

<ResponseField name="output.images" type="object[]">
  已生成的图像，适用于图像模型。
</ResponseField>

<ResponseField name="output.images[].content_type" type="string">
  文件的 MIME 类型。
</ResponseField>

<ResponseField name="output.images[].height" type="integer">
  高度，以像素为单位。
</ResponseField>

<ResponseField name="output.images[].url" type="string">
  已生成文件的下载 URL。
</ResponseField>

<ResponseField name="output.images[].width" type="integer">
  宽度，以像素为单位。
</ResponseField>

<ResponseField name="output.media_type" type="string">
  可能的值：`image`、`video`、`videos`
</ResponseField>

<ResponseField name="output.seed" type="integer">
  视频任务实际使用的种子（在有报告时提供）。
</ResponseField>

<ResponseField name="output.video" type="object">
  一个已生成的媒体文件。
</ResponseField>

<ResponseField name="output.video.content_type" type="string">
  文件的 MIME 类型。
</ResponseField>

<ResponseField name="output.video.height" type="integer">
  高度，以像素为单位。
</ResponseField>

<ResponseField name="output.video.url" type="string">
  已生成文件的下载 URL。
</ResponseField>

<ResponseField name="output.video.width" type="integer">
  宽度，以像素为单位。
</ResponseField>

<ResponseField name="output.videos" type="object[]">
  已生成的视频，用于多视频交付。
</ResponseField>

<ResponseField name="output.videos[].content_type" type="string">
  文件的 MIME 类型。
</ResponseField>

<ResponseField name="output.videos[].height" type="integer">
  高度，以像素为单位。
</ResponseField>

<ResponseField name="output.videos[].url" type="string">
  已生成文件的下载 URL。
</ResponseField>

<ResponseField name="output.videos[].width" type="integer">
  宽度，以像素为单位。
</ResponseField>

<ResponseField name="runtime_s" type="number">
  生成所耗的秒数，从派发到终态。为尽力估算；可能为 null。
</ResponseField>

<ResponseField name="status" type="string" required>
  生命周期状态。Router 返回的响应体中该值始终为 `completed`；`failed` 任务则以 Router 的错误响应作答。

  可能的值：`queued`、`running`、`completed`、`failed`
</ResponseField>

<ResponseField name="usage" type="object">
  提供商报告的任务用量单位。键是稀疏的；只包含任务所报告的单位。
</ResponseField>

## 示例

### 输入

```json theme={null}
{
  "duration_s": 5,
  "prompt": "A paper boat drifting down a rain-soaked city street at dusk",
  "resolution": "720p"
}
```

### 输出

```json theme={null}
{
  "id": "6f1c2a9e-3b47-4d8a-9e21-7c5b0f4d8a13",
  "model_id": "pika/pika-2.5/text-to-video",
  "output": {
    "media_type": "video",
    "seed": 42,
    "video": {
      "content_type": "video/mp4",
      "height": 720,
      "url": "https://example.invalid/pika/pika-2.5-text-to-video/generated.mp4",
      "width": 1280
    }
  },
  "status": "completed"
}
```

## 发布前须知

SDK 会生成 `Idempotency-Key` 并在自动重试中复用它。手动重试时，请复用原始 key。Router 最长可保持连接 10 分钟。

请求失败时，Router 会发送 `X-Comfy-Error-Type` 响应头说明原因。`422` 表示 Router 在调用提供商之前就拒绝了输入，`413` 表示请求体超出了 Router 可接受的大小。已生成的资源请及时下载，因为[结果 URL 会过期](/zh/development/comfy-router/reference#结果资产)。

上文任何字段描述中提到的尺寸限制，都是提供商对该字段自身的限定，引自提供商的规范。Router 会对整个请求体另行设置上限，base64 编码的媒体内容也计入其中：参见[请求体大小](/zh/development/comfy-router/limitations)。

本页记录的是通过 Comfy Router 调用的某一个合作伙伴模型。同一个 `comfy-sdk` / `@comfyorg/sdk` 包还提供第二个客户端，用于在 Comfy Cloud 上运行完整的 ComfyUI 工作流图：`Comfy(api_key=...)` / `new Comfy({ apiKey })`，并带有 `client.workflows`、`client.assets` 和 `client.jobs`。请参阅 [Comfy SDKs](/zh/development/api-development/sdks)。

<CardGroup cols={3}>
  <Card title="请求头" icon="list" href="/zh/development/comfy-router/headers">
    身份验证、幂等性、请求 ID、错误分类、重试节奏、消费限额。
  </Card>

  <Card title="使用 Router API" icon="code" href="/zh/development/comfy-router/api">
    模型发现、验证错误、重试与计费。
  </Card>

  <Card title="限制" icon="triangle-exclamation" href="/zh/development/comfy-router/limitations">
    Router 目前不支持的功能，以及替代方案。
  </Card>
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.