GPT Image 2 API reference
OpenAI gpt-image-2。同一端点同时支持文生图和多图编辑(最多 16 张参考图,单张 ≤ 30MB),输出构图由 aspect ratio 控制。原生 1K / 2K / 4K($0.025 / $0.03 / $0.05)。异步任务:POST 创建任务后用 GET 轮询或走 callback_url 回调。
模型页:GPT Image 2 API —— 价格、规格与在线体验
认证
Authorization: Bearer YOUR_API_KEY模型与价格
| 模型 | model | 价格 | 说明 |
|---|---|---|---|
| GPT Image 2 | gpt-image-2 | $0.025 / $0.03 / $0.05 | 异步 · 1K/2K/4K 分档 |
端点
POST
/api/v1/images/generations创建任务GET
/api/v1/images/generations?task_id=...查询状态代码示例
# ─── 1. Text-to-image ──────────────────────────────────────
# resolution: "1K" | "2K" | "4K" (optional, default 1K). aspect_ratio: 1:1, 3:2, 2:3, 4:3, 3:4, 16:9, 9:16.
curl -X POST https://api.apimodels.app/v1/images/generations \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "A minimalist product shot of a white sneaker on a pastel gradient backdrop, soft studio lighting",
"aspect_ratio": "1:1",
"resolution": "2K"
}'
# Response:
# { "code": 200, "msg": "success", "data": { "taskId": "clxxx...", "state": "pending" } }
# ─── 2. Image editing with a reference URL ─────────────────
curl -X POST https://api.apimodels.app/v1/images/generations \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "Place this product on a marble kitchen counter with soft morning light",
"image_url": "https://example.com/product.jpg",
"aspect_ratio": "3:2"
}'
# ─── 3. Multi-image fusion (up to 10 reference images) ─────
curl -X POST https://api.apimodels.app/v1/images/generations \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "Use the model from image 1 wearing the outfit from image 2, studio lighting",
"image_urls": [
"https://example.com/model.jpg",
"https://example.com/outfit.jpg"
],
"aspect_ratio": "2:3"
}'
# ─── 4. Image editing via base64 ───────────────────────────
curl -X POST https://api.apimodels.app/v1/images/generations \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "Add a geometric fox logo in the center of the mug",
"image_base64": "iVBORw0KGgoAAAANSUhEUgAA...",
"image_mime_type": "image/png",
"aspect_ratio": "1:1"
}'
# ─── 5. Poll task status ───────────────────────────────────
curl "https://api.apimodels.app/v1/images/generations?task_id=TASK_ID" \
-H "Authorization: Bearer YOUR_API_KEY"
# Success response:
# {
# "code": 200,
# "msg": "success",
# "data": {
# "taskId": "clxxx...",
# "state": "completed",
# "resultUrls": ["https://r2.apimodels.app/images/xxx.png"],
# "images": [{"url": "https://r2.apimodels.app/images/xxx.png", "width": 2048, "height": 2048}],
# "costTime": 14320,
# "completeTime": 1705123465000
# }
# }
# ─── 6. Webhook callback (optional) ────────────────────────
# Add "callback_url" to the create request and your endpoint
# will receive a POST with the same payload when the task
# completes — useful for production to avoid polling.
curl -X POST https://api.apimodels.app/v1/images/generations \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "Studio photo of a ceramic mug",
"aspect_ratio": "1:1",
"callback_url": "https://your-domain.com/webhook/image"
}'请求参数
| 字段 | 必填 | 类型 | 说明 |
|---|---|---|---|
| model | 是 | string | 必须为 "gpt-image-2" |
| prompt | 是 | string | 图片描述或编辑指令。多图编辑时可用 "image 1"、"image 2" 引用具体参考图。长度 1–20000 字符 |
| aspect_ratio | 否 | string | 输出画面比例,枚举:1:1 / 2:3 / 3:2 / 4:5 / 5:4 / 4:3 / 3:4 / 16:9 / 9:16 / 21:9。省略或传 "auto" 见下方说明。注意:5:4 和 4:5 仅在 1K 支持,2K/4K 下不支持(请改用 1K,或换 3:4 / 4:3)。⚠️ auto 的真实行为:**图生图**跟随参考图比例;**文生图**没有可跟随的输入,会退回 1:1 方图——不是"让模型自决"。要指定形状请直接选枚举值。另外两点实测(2026-08-13):在提示词里写比例**无效**(不传 size 只在提示词里写 5:2,仍回 1024×1024);传枚举以外的精确 size 会被上游**吸附到最近的网格**(传 5:2 的 1600×640 与传 21:9 的 1552×656 拿回的都是 1456×624)。所以当前最宽只能到 21:9(2.33)。 |
| resolution | 否 | string | "1K"(默认)/ "2K" / "4K"。分档计价:1K $0.025、2K $0.03、4K $0.05。 |
| image_url | 否 | string | 单张参考图 URL(公网可访问)。传入则走图生图,每张 ≤ 10MB |
| image_urls | 否 | string[] | 多张参考图 URL(最多 16 张,每张 ≤ 10MB),用于多图融合。超过 16 张会只取前 16 张 |
| image_base64 | 否 | string | Base64 编码或 data URI 的参考图(本地文件用)。和 image_url 二选一。 |
| image_mime_type | 否 | string | image_base64 对应的 MIME,默认 image/jpeg。支持 image/png、image/jpeg、image/webp |
| output_format | 否 | string | 交付格式:"png" / "jpeg" / "webp"。省略时的默认行为:不透明图交付 JPEG q95、带透明通道的图保持 PNG。传 "png" 得到无损原图(我们不做任何重编码,原样交付上游 PNG),代价是体积约为 JPEG 的 6 倍——实测一张 1760×2352 的成片 PNG 6.6MB、JPEG 1.0MB。"webp" 体积与 JPEG 相当,并且是唯一既能压缩又保留透明通道的选项。生产管线要求无损时用 png,其余情况建议留空。 |
| callback_url | 否 | string | 任务完成回调 URL。服务端会 POST 与轮询返回相同结构的 JSON 到这个地址 |
输出尺寸:2K 和 4K
2K / 4K 档沿用 OpenAI 官方对 gpt-image-2 的尺寸口径。OpenAI 图像生成指南里列出的 2K / 4K 尺寸如下:
| 档位 | 尺寸 | OpenAI 官方标注 |
|---|---|---|
| 2K | 2048x2048 | 2K square |
| 2K | 2048x1152 | 2K landscape |
| 4K | 3840x2160 | 4K landscape |
| 4K | 2160x3840 | 4K portrait |
- OpenAI 的 2K 尺寸长边是 2048。16:9 可能交 2048x1152(即 OpenAI 标注的 2K 横图),也可能交同比例更大的 2736x1536;9:16 同理,交 1152x2048 或 1536x2736。两种都是 2K 出图,都按 2K 的 $0.03 计费。
- 同一份指南写明,2560x1440 以上的分辨率属于实验性,所以 2736x1536 这类更大的尺寸不保证每次都有。
- 4K 受 OpenAI 的总像素上限 8,294,400(即 3840x2160)约束:16:9 和 9:16 的长边能到 3840,其他比例长边会短一些,比如 1:1 的 4K 是 2880x2880。
- OpenAI 对 gpt-image-2 尺寸的完整限制:两边都是 16 的倍数、最长边 ≤ 3840、长短边比 ≤ 3:1、总像素 655,360–8,294,400。
- 每个完成的任务都会在 data.images 里返回实际宽高,可以在代码里直接校验。
响应字段
| 字段 | 必填 | 类型 | 说明 |
|---|---|---|---|
| code | — | integer | 200 表示创建/查询成功;其他值见错误码表 |
| msg | — | string | "success" 或错误描述 |
| data.taskId | — | string | 任务 ID,查询状态时用 |
| data.state | — | string | pending / processing / completed / failed |
| data.resultUrls | — | string[] | 仅在 state=completed 时返回;R2 托管的图片 URL,保留 30 天后自动删除,请及时下载或转存到自有存储 |
| data.images | — | object[] | 仅在 state=completed 时返回(2026-10-25 起的任务):每张图的实际像素尺寸,顺序与 resultUrls 相同 —— [{ url, width, height }];量不出的那张只有 url。 |
| data.failMsg | — | string | 仅在 state=failed 时返回,上游失败原因 |
| data.costTime | — | integer | 任务耗时(毫秒) |
| data.completeTime | — | integer | 完成时间戳(毫秒) |
说明
- 异步流程:POST 立即返回 taskId(state=pending),再 GET ?task_id= 轮询直到 state=completed。典型耗时 10–30 秒,最长 ≤ 3 分钟。
- 端点自动路由:只传 prompt → text-to-image;携带 image_url / image_urls / image_base64 → image-to-image。
- 图片输入可以是公网 URL、data:<mime>;base64,... 或 bare base64 字符串(会自动包装成 data URI)。最多 16 张,每张 ≤ 10MB;超过 16 张只取前 16 张。
- callback_url 优先于轮询——后端会在任务完成时 POST 同样的 data 结构过去,适合生产场景避免自建轮询。
- resultUrls 指向 R2,保留 30 天后自动删除;请在窗口内下载或转存到自有存储。
- 失败不扣费:state=failed 的任务不计入账单,可安全重试。
- 分档计价:1K $0.025、2K $0.03、4K $0.05,与 aspect ratio、提示词长度无关。5:4 / 4:5 仅 1K 支持,2K/4K 不支持。
相关:OpenAI SDK 兼容通道见 GPT Image 2 All;试一试:Playground
常见问题
GPT Image 2 API 多少钱?
按分辨率分档:1K $0.025、2K $0.03、4K $0.05 每张,与比例、提示词长度无关。仅成功扣费。
怎么用 GPT Image 2 做图生图/编辑?
调 POST /api/v1/images/generations,model 填 gpt-image-2,带 prompt + image_url(单张)或 image_urls[](最多 16 张)。GET ?task_id= 轮询到完成,或传 callback_url 收回调。
gpt-image-2、gpt-image-2-all、gpt-image-2-lite 有什么区别?
gpt-image-2 是异步任务通道(1K/2K/4K 分档);gpt-image-2-all 是 OpenAI SDK 兼容的同步通道,带完整质量矩阵和多图编辑;gpt-image-2-lite 是价格最低的通道:单档 $0.01/张(按你的画幅约 1.5MP)、同步。