搭配 Comfy Router 使用 Grok Imagine Video
通过 Comfy Router 调用 xai/grok-imagine-video:端点、请求形状以及 Router 返回的响应。
搭配 Comfy Router 使用 Grok Imagine Video
通过 Comfy Router 调用 xai/grok-imagine-video:端点、请求形状以及 Router 返回的响应。
xai/grok-imagine-video 的 API 参考,由 Comfy Router 提供,来自 xAI。
输入: 文本, 图像 · 输出: 视频
快速开始
在你的 Comfy 工作区中创建密钥,并将其导出为 COMFY_API_KEY。Python 和 TypeScript 代码片段使用 Comfy SDK(pip install comfy-sdk 和 npm install @comfyorg/sdk);cURL 代码片段是通过原始 HTTP 进行的相同调用。
模型 ID: xai/grok-imagine-video
端点: POST https://api.comfy.org/v2/models/xai/grok-imagine-video
[tab] 等待结果 ``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", { "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", { 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 \ -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\"}"``
[tab] 排队并稍后收集 相同的请求体,发送到POST https://api.comfy.org/v2/models/xai/grok-imagine-video/requests。运行一经准入,Router 就会立即返回201和一个request_id,结果就绪后即可从当前进程或另一个进程收集。队列投递介绍了状态查询、取消和结果收集。 ``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", { "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", { 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() 返回的结果相同。请求失败或被取消时会在这里被拒绝。 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/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/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/requests/$REQUEST_ID \ -H "X-API-Key: $COMFY_API_KEY"``
Schema
输入
aspect\_ratio string (default: "16:9")
生成视频的宽高比
可选值:1:1、16:9、9:16、4:3、3:4、3:2、2:3
duration integer (default: 8)
视频时长,单位为秒。范围 \[1, 15]。默认 8。
范围:1 到 15
image object
用于 xAI 端点的输入图像对象
image.type string
图像输入的类型
可选值:image_url
image.url string required
输入图像的 URL(公开 URL 或 base64 编码的 data URI)
model string
要使用的模型。支持:grok-imagine-video(默认)、grok-imagine-video-1.5-preview、grok-imagine-video-1.5、grok-imagine-video-1.5-lite。已弃用的 grok-imagine-video-beta ID 会作为别名指向 grok-imagine-video。
output object
生成视频的可选输出目标
prompt string required
用于视频生成的提示词。最多 4,096 个字符。
reference\_images object[]
一张或多张参考图像,用于引导视频生成(参考生视频模式)。与 image 互斥。Router 仅转发 application/json,且此对象与 XAIImageObject 均不接受 file\_id,因此本路由不支持基于文件的图像输入。
reference\_images\[].url string required
参考图像的 URL。支持 HTTPS URL(公开)或 base64 编码的 data URL(例如 data:image/jpeg;base64,...)。
resolution string
输出视频的分辨率
size string
输出视频的尺寸
user string
代表最终用户的唯一标识符
本文档根据 Router 在 GET /v2/models/xai/grok-imagine-video/openapi.json 提供的 schema 生成,该 schema 与 Router 在请求到达提供商之前用于校验调用的是同一份文档。
输出
block\_reason string
本文档中从不会携带原因:非空的 block reason 属于输入审核拒绝,Router 会以 Comfy Router 错误的形式作答,而不是返回此响应体。此处为缺失、null 或空。
model string
用于生成视频的模型
status string
本文档中为缺失或 done。pending 是 Router 尚未完成的轮询响应体,绝不会作为 200 返回;当 video.url 已填充时,Router 接受缺失的 status,因此调用方不得要求该字段存在。
可选值:done
usage object
视频生成请求的用量信息
usage.cost\_in\_usd\_ticks integer
本次请求的成本,以 USD ticks 表示。1 美分等于 100,000,000 ticks,因此 1 美元等于 10,000,000,000 ticks。
video object required
已完成的生成结果。与轮询文档不同,此处为必填:没有 video.url 的终端结果会以 Comfy Router 错误的形式作答,因此 200 总是携带该字段。
video.duration integer
已生成视频的时长,单位为秒
video.respect\_moderation boolean
模型生成的视频是否符合审核规则
video.url string required
本文档中非空。仅当此叶子字段已填充时,Router 才报告成功;它即上文所述重新托管后的 Comfy 签名 URL,或在无法执行重新托管时使用 xAI 自己的短期 URL。
示例
输入
{
"duration": 4,
"prompt": "a single red maple leaf falling onto still water, slow motion"
}
输出
{
"model": "grok-imagine-video",
"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 会过期。
上文任何字段描述中提到的尺寸限制,都是提供商对该字段自身的限定,引自提供商的规范。Router 会对整个请求体另行设置上限,base64 编码的媒体内容也计入其中:参见请求体大小。
本页记录的是通过 Comfy Router 调用的某一个合作伙伴模型。同一个 comfy-sdk / @comfyorg/sdk 包还提供第二个客户端,用于在 Comfy Cloud 上运行完整的 ComfyUI 工作流图:Comfy(api_key=...) / new Comfy({ apiKey }),并带有 client.workflows、client.assets 和 client.jobs。请参阅 Comfy SDKs。
[card] 请求头 身份验证、幂等性、请求 ID、错误分类、重试节奏、消费限额。
[card] 使用 Router API 模型发现、验证错误、重试与计费。
[card] 限制 Router 目前不支持的功能,以及替代方案。