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

# 结合 Comfy Router 使用 Grok Imagine Video 1.5 Preview

> 通过 Comfy Router 调用 xai/grok-imagine-video-1.5-preview：端点、请求结构以及 Router 返回的响应。

`xai/grok-imagine-video-1.5-preview` 的 API 参考文档，该模型由 Comfy Router 提供，源自 xAI。

## 快速开始

在[你的 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：** `xai/grok-imagine-video-1.5-preview`

**端点：** `POST https://api.comfy.org/v2/models/xai/grok-imagine-video-1.5-preview`

<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(
              "xai/grok-imagine-video-1.5-preview",
              {
                  "duration": 4,
                  "prompt": "a single red maple leaf falling onto still water, slow motion",
              },
          )

      print(result)
      ```

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

      // 从环境中读取 COMFY_API_KEY。
      // SDK 会自动创建一个幂等键，并在自动重试时复用它。
      const { data } = await comfy.models.run("xai/grok-imagine-video-1.5-preview", {
        duration: 4,
        prompt: "a single red maple leaf falling onto still water, slow motion",
      });

      console.log(data);
      ```

      ```bash cURL theme={null}
      curl https://api.comfy.org/v2/models/xai/grok-imagine-video-1.5-preview \
        -H "X-API-Key: $COMFY_API_KEY" \
        -H "Idempotency-Key: $(uuidgen)" \
        -H "Content-Type: application/json" \
        -d "{\"duration\": 4, \"prompt\": \"a single red maple leaf falling onto still water, slow motion\"}"
      ```
    </CodeGroup>
  </Tab>

  <Tab title="入队并稍后收集">
    将相同的请求体发送到 `POST https://api.comfy.org/v2/models/xai/grok-imagine-video-1.5-preview/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(
                  "xai/grok-imagine-video-1.5-preview",
                  {
                      "duration": 4,
                      "prompt": "a single red maple leaf falling onto still water, slow motion",
                  },
              )
              print("request_id:", handle.request_id)  # 配合模型 ID，这就是另一个进程所需的全部信息

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

              # 提供商自身的负载，与 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("xai/grok-imagine-video-1.5-preview", {
        duration: 4,
        prompt: "a single red maple leaf falling onto still water, slow motion",
      });
      console.log("requestId:", handle.requestId); // 配合模型 ID，这就是另一个进程所需的全部信息

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

      // 与 models.run() 返回的结果相同。失败或已取消的请求会在此处 reject。
      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/xai/grok-imagine-video-1.5-preview/requests \
        -H "X-API-Key: $COMFY_API_KEY" \
        -H "Idempotency-Key: $(uuidgen)" \
        -H "Content-Type: application/json" \
        -d "{\"duration\": 4, \"prompt\": \"a single red maple leaf falling onto still water, slow motion\"}"

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

      # 3. 收集。完成后返回 200 和模型的原生输出，仍在运行时返回 202 和状态响应体。
      curl https://api.comfy.org/v2/models/xai/grok-imagine-video-1.5-preview/requests/$REQUEST_ID \
        -H "X-API-Key: $COMFY_API_KEY"
      ```
    </CodeGroup>
  </Tab>
</Tabs>

## Schema

### 输入

<ParamField body="aspect_ratio" type="string" default="&#x22;16:9&#x22;">
  已生成视频的宽高比

  可能的值：`1:1`、`16:9`、`9:16`、`4:3`、`3:4`、`3:2`、`2:3`
</ParamField>

<ParamField body="duration" type="integer" default="8">
  视频时长，单位为秒。范围 \[1, 15]。默认 8。

  范围：`1` 到 `15`
</ParamField>

<ParamField body="image" type="object">
  xAI 端点使用的输入图像对象
</ParamField>

<ParamField body="image.type" type="string">
  图像输入的类型

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

<ParamField body="image.url" type="string" required>
  输入图像的 URL（公开 URL 或 base64 编码的 data URI）
</ParamField>

<ParamField body="model" type="string">
  要使用的模型。支持：grok-imagine-video（默认）、grok-imagine-video-1.5-preview、grok-imagine-video-1.5。已弃用的 grok-imagine-video-beta ID 是 grok-imagine-video 的别名。
</ParamField>

<ParamField body="output" type="object">
  已生成视频的可选输出目标
</ParamField>

<ParamField body="prompt" type="string" required>
  用于视频生成的提示词。最多 4,096 个字符。
</ParamField>

<ParamField body="reference_images" type="object[]">
  用于引导视频生成的一张或多张参考图像（参考生视频模式）。与 image 互斥。Router 仅转发 application/json，该对象和 XAIImageObject 都不接受 file\_id，因此此路由不支持基于文件的图像输入。
</ParamField>

<ParamField body="reference_images[].url" type="string" required>
  参考图像的 URL。支持 HTTPS URL（公开）或 base64 编码的 data URL（例如 data:image/jpeg;base64,...）。
</ParamField>

<ParamField body="resolution" type="string">
  输出视频的分辨率
</ParamField>

<ParamField body="size" type="string">
  输出视频的尺寸
</ParamField>

<ParamField body="user" type="string">
  代表最终用户的唯一标识符
</ParamField>

本页内容根据 Router 在 `GET /v2/models/xai/grok-imagine-video-1.5-preview/openapi.json` 提供的 schema 生成，在请求到达提供商之前，Router 校验调用时依据的也是这份同一文档。

### 输出

<ResponseField name="block_reason" type="string">
  如果请求被输入审核拦截，则包含拦截原因
</ResponseField>

<ResponseField name="model" type="string">
  用于生成视频的模型
</ResponseField>

<ResponseField name="status" type="string">
  延迟请求的状态："pending" 或 "done"

  可能的值：`pending`、`done`
</ResponseField>

<ResponseField name="usage" type="object">
  视频生成请求的用量信息
</ResponseField>

<ResponseField name="usage.cost_in_usd_ticks" type="integer">
  此请求的费用，以 USD tick 表示。1 美分等于 100,000,000 ticks，因此 1 美元等于 10,000,000,000 ticks。
</ResponseField>

<ResponseField name="video" type="object">
  来自 xAI 的已生成视频
</ResponseField>

<ResponseField name="video.duration" type="integer">
  已生成视频的时长，单位为秒
</ResponseField>

<ResponseField name="video.respect_moderation" type="boolean">
  模型生成的视频是否遵守审核规则
</ResponseField>

<ResponseField name="video.url" type="string">
  已生成视频的下载 URL。Router 会将视频重新托管到 Comfy 存储并重写此字段，因此它通常是 Comfy 签名的 URL，有效期最长 24 小时。签发时按 24 小时签名，并从 23 小时的备忘中重放，因此稍后的轮询可能返回剩余有效期仅 1 小时的链接。当无法执行重新托管时，该字段会保留 xAI 自己的短期 URL。可为空（NULLABLE）：`url` 为空的成功响应并不代表生成已完成。无论哪种情况，链接都会过期，因此请下载视频，而不要保存该 URL。
</ResponseField>

## 示例

### 输入

```json theme={null}
{
  "duration": 4,
  "prompt": "a single red maple leaf falling onto still water, slow motion"
}
```

### 输出

```json theme={null}
{
  "model": "grok-imagine-video-1.5",
  "status": "done",
  "usage": {
    "cost_in_usd_ticks": 3500000000
  },
  "video": {
    "duration": 4,
    "respect_moderation": true,
    "url": "https://example.invalid/xai/grok-imagine-video/generated.mp4"
  }
}
```

## 发布前须知

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>
