Grok Imagine Image 2.0 API 文档
查看从代码中调用 Grok Imagine Image 2.0 所需的全部信息。基础 URL 为 https://cheapergen.com/api/v1。你可以先在 Playground 中试用。
正在使用编程智能体?
将这段内容粘贴到 Claude Code、Cursor、Codex 或任意编程智能体中。它会将 API 接入你的项目,把密钥保存在 CHEAPERGEN_API_KEY 中,并在开始任何付费操作前先用免费报价进行测试。
Add Grok Imagine Image 2.0 (CheaperGen endpoint xai/ grok-imagine-image/ text-to-image) to this project by following https:/ / cheapergen.com/ agents/ integrate.md身份验证
在 控制面板 → API 密钥 创建密钥并以 Bearer 令牌的形式发送。请仅在服务器端保存密钥。
export CHEAPERGEN_API_KEY="cg_live_…"
# every request
Authorization: Bearer $CHEAPERGEN_API_KEYGrok Imagine Image 2.0 文生图 API
POST
/api/v1/run/xai/grok-imagine-image/text-to-image提交异步任务并返回 202 和生成任务 ID。请轮询 GET /api/v1/generations/{id} 或传入 webhook_url。参见 任务生命周期。
输入参数
| 名称 | 类型 | 说明 | 影响价格 |
|---|---|---|---|
prompt必填 | string | 描述镜头,包括主体、动作、镜头运动、光线和风格。 | 否 |
aspect_ratio | string (enum) | 输出图像比例。 可选值:1:1 · 3:4 · 4:3 · 9:16 · 16:9 · 2:3 · 3:2 · 1:2 · 2:1 · 9:19.5 · 19.5:9 · 9:20 · 20:9 默认值:1:1 | 是 |
resolution | string (enum) | 1K ≈ 1 MP,2K ≈ 4 MP。 可选值:1K · 2K 默认值:1K | 是 |
quality | string (enum) | 低画质速度更快、价格更低;中等画质细节更丰富。(编辑任务始终使用中等画质。) 可选值:low · medium 默认值:medium | 是 |
webhook_url | string (URL) | 可选。生成完成后,我们会在此地址 POST 结果,并使用你的 Webhook 密钥进行签名。Webhooks | 否 |
请求
const API = "https://cheapergen.com/api/v1";
const headers = {
Authorization: `Bearer ${process.env.CHEAPERGEN_API_KEY}`,
"Content-Type": "application/json",
};
// 1. Submit the job
let job = await fetch(`${API}/run/xai/grok-imagine-image/text-to-image`, {
method: "POST",
headers,
body: JSON.stringify({
"prompt": "时尚杂志摄影:正午时分,模特身穿雕塑感橙色外套站在盐滩上,阴影强烈,中画幅",
"aspect_ratio": "1:1",
"resolution": "1K",
"quality": "medium"
}),
}).then((r) => r.json());
// 2. Poll until it finishes (or pass webhook_url and skip polling)
while (job.status === "queued" || job.status === "running") {
await new Promise((r) => setTimeout(r, 3000));
job = await fetch(`${API}/generations/${job.id}`, { headers }).then((r) => r.json());
}
console.log(job.status === "succeeded" ? job.output.images[0].url : job.error);响应
{
"id": "gen_8c2QmX1nq0k4",
"object": "generation",
"model": "xai/grok-imagine-image",
"endpoint": "xai/grok-imagine-image/text-to-image",
"mode": "text-to-image",
"status": "queued",
"input": {
"prompt": "时尚杂志摄影:正午时分,模特身穿雕塑感橙色外套站在盐滩上,阴影强烈,中画幅",
"aspect_ratio": "1:1",
"resolution": "1K",
"quality": "medium"
},
"output": null,
"error": null,
"cost": {
"amount_usd": 0.2255,
"currency": "usd",
"estimated": true,
"official_usd": null,
"list_price_source": "official",
"list_price_label": "Official",
"saved_usd": null
},
"source": "api",
"started_at": null,
"completed_at": null,
"urls": {
"get": "https://cheapergen.com/api/v1/generations/gen_8c2QmX1nq0k4",
"cancel": "https://cheapergen.com/api/v1/generations/gen_8c2QmX1nq0k4/cancel"
},
"created_at": "2026-10-15T09:21:04Z"
}Grok Imagine Image 2.0 图像编辑 API
POST
/api/v1/run/xai/grok-imagine-image/edit提交异步任务并返回 202 和生成任务 ID。请轮询 GET /api/v1/generations/{id} 或传入 webhook_url。参见 任务生命周期。
输入参数
| 名称 | 类型 | 说明 | 影响价格 |
|---|---|---|---|
prompt必填 | string | 描述镜头,包括主体、动作、镜头运动、光线和风格。 | 否 |
image_urls必填 | string[] (URLs) | 使用 1–3 张图像进行编辑或合成。在提示词中使用“图像 1”“图像 2”…来引用。 最多数量:3 | 否 |
aspect_ratio | string (enum) | 输出图像比例。 可选值:1:1 · 3:4 · 4:3 · 9:16 · 16:9 · 2:3 · 3:2 · 1:2 · 2:1 · 9:19.5 · 19.5:9 · 9:20 · 20:9 默认值:1:1 | 是 |
resolution | string (enum) | 1K ≈ 1 MP,2K ≈ 4 MP。 可选值:1K · 2K 默认值:1K | 是 |
webhook_url | string (URL) | 可选。生成完成后,我们会在此地址 POST 结果,并使用你的 Webhook 密钥进行签名。Webhooks | 否 |
请求
const API = "https://cheapergen.com/api/v1";
const headers = {
Authorization: `Bearer ${process.env.CHEAPERGEN_API_KEY}`,
"Content-Type": "application/json",
};
// 1. Submit the job
let job = await fetch(`${API}/run/xai/grok-imagine-image/edit`, {
method: "POST",
headers,
body: JSON.stringify({
"prompt": "让图像 1 中的人穿上图像 2 中的夹克,置身雨夜的东京街头",
"image_urls": [
"https://cdn.cheapergen.com/samples/portrait.jpg"
],
"aspect_ratio": "1:1",
"resolution": "1K"
}),
}).then((r) => r.json());
// 2. Poll until it finishes (or pass webhook_url and skip polling)
while (job.status === "queued" || job.status === "running") {
await new Promise((r) => setTimeout(r, 3000));
job = await fetch(`${API}/generations/${job.id}`, { headers }).then((r) => r.json());
}
console.log(job.status === "succeeded" ? job.output.images[0].url : job.error);响应
{
"id": "gen_8c2QmX1nq0k4",
"object": "generation",
"model": "xai/grok-imagine-image",
"endpoint": "xai/grok-imagine-image/edit",
"mode": "edit",
"status": "queued",
"input": {
"prompt": "让图像 1 中的人穿上图像 2 中的夹克,置身雨夜的东京街头",
"image_urls": [
"https://cdn.cheapergen.com/samples/portrait.jpg"
],
"aspect_ratio": "1:1",
"resolution": "1K"
},
"output": null,
"error": null,
"cost": {
"amount_usd": 0.2255,
"currency": "usd",
"estimated": true,
"official_usd": null,
"list_price_source": "official",
"list_price_label": "Official",
"saved_usd": null
},
"source": "api",
"started_at": null,
"completed_at": null,
"urls": {
"get": "https://cheapergen.com/api/v1/generations/gen_8c2QmX1nq0k4",
"cancel": "https://cheapergen.com/api/v1/generations/gen_8c2QmX1nq0k4/cancel"
},
"created_at": "2026-10-15T09:21:04Z"
}获取结果
生成任务以异步方式处理。请持续轮询,直到 status 状态为 succeeded、failed 或 canceled;也可以在提交时传入 webhook_url。生成失败会自动退款。
curl https://cheapergen.com/api/v1/generations/gen_8c2QmX1nq0k4 \
-H "Authorization: Bearer $CHEAPERGEN_API_KEY"Grok Imagine Image 2.0 定价
按 每张图像 按量付费,无需订阅。生成失败会自动退款。
| 档位 | 官方价格 | CheaperGen | 可节省 |
|---|---|---|---|
1K · 低 | $0.0329/img 订阅月度套餐后: Pro $0.0318Max $0.0307 | −18% | |
1K · 中 | $0.0429/img 订阅月度套餐后: Pro $0.0408Max $0.0387 | −29% | |
2K · 低 | $0.0552/img | −8% | |
2K · 中 | $0.0736/img | −8% |
月度套餐 可降低大多数模型的价格(各价格下方会显示);你仍可按量付费。Pro 每月 $499 · Max 每月 $4,999.
- 官方价格:xAI API 的 grok-imagine-image-2.0 目录价(1K 低/中画质 $0.04/$0.06,2K $0.06/$0.08;每张输入图像 $0.01)。价格核实于 2026年10月18日。
- 编辑按中等画质计费,每张输入图像另加 $0.01(折扣前)。