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

# 结合 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)中创建密钥，并将其导出为 `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>
  <Tab title="Wait for the result">
    <CodeGroup>
      ```python Python theme={null}
      from comfy_sdk import Comfy

      # Reads COMFY_API_KEY from the environment.
      # The SDK automatically creates an idempotency key and reuses it for automatic retries.
      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";

      // Reads COMFY_API_KEY from the environment.
      // The SDK automatically creates an idempotency key and reuses it for automatic retries.
      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="Queue and collect later">
    <CodeGroup>
      ```python Python theme={null}
      from comfy_sdk import Comfy

      # Reads COMFY_API_KEY from the environment.
      # Each submit() call mints its own Idempotency-Key and reuses it for automatic retries.
      with Comfy() as client:
          handle = 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)  # with the model ID, all another process needs

          # Poll until the request completes, waiting the Retry-After the server names.
          for update in handle.iter_events():
              print(update.status, update.queue_position)

          # The provider's own payload, the same value models.run() returns.
          # A request that failed or was cancelled raises the typed Router error here.
          result = handle.get()

      print(result)
      ```

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

      // Reads COMFY_API_KEY from the environment.
      // Each submit() call mints its own Idempotency-Key and reuses it for automatic retries.
      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); // with the model ID, all another process needs

      // Poll until the request completes, waiting the Retry-After the server names.
      for await (const update of handle.events()) {
        console.log(update.status, update.queuePosition);
      }

      // The same result models.run() returns. A request that failed or was cancelled rejects here.
      const result = await handle.get();

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

      ```bash cURL theme={null}
      # 1. Submit. Router answers 201 with request_id, status_url, response_url and 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. Poll until status is COMPLETED, waiting the Retry-After seconds each response names.
      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. Collect. 200 with the model's native output, 202 with the status body while it is still running.
      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

### Input

<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 校验调用时依据的也是这份同一文档。

### Output

<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 在调用提供商之前就拒绝了输入。生成的资源请及时下载，因为[结果链接会过期](/zh/development/comfy-router/reference#结果资产)。

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

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

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