GPT Image 2
OpenAI GPT Image 2 图像生成模型。对外统一使用一个模型 ID:gpt-image-2,同时支持纯提示词生图和基于参考图的图生图编辑。
模型
| 模型名称 | 输出 | 模式 |
|---|---|---|
gpt-image-2 | 图片 URL | 文生图、图生图 |
定价
按输出分辨率档位计费:
| Resolution | 积分 | 价格 |
|---|---|---|
1k(默认) | 4.7 积分 | $0.0235 |
2k | 9.4 积分 | $0.047 |
4k | 14.1 积分 | $0.0705 |
创建任务
curl -X POST https://api.aivideoapi.ai/v1/images/generations \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"callback_url": "https://your-server.com/webhook",
"input": {
"prompt": "把这张产品图改成纯白背景的棚拍效果",
"image_urls": [
"https://example.com/source.png"
],
"aspect_ratio": "1:1",
"nsfw_checker": true
}
}'
请求体
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | 固定为 gpt-image-2 |
input | object | 是 | 生成参数,见下方 |
callback_url | string | 否 | 任务完成/失败时接收通知的 URL |
Input 参数
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
prompt | string | 条件必填 | 提示词,最长 20,000 字符 |
image_urls | string[] | 条件必填 | 参考图片 URL,用于图像编辑,最多 16 张 |
aspect_ratio | string | 否 | 生成图比例,默认 auto。可选:auto、1:1、3:2、2:3、4:3、3:4、5:4、4:5、16:9、9:16、2:1、1:2、3:1、1:3、21:9、9:21;也可直接传入像素尺寸(如 1881x836、887x1774)。传 auto 时按 1:1 处理。各档位的实际像素见下方尺寸与分辨率。 |
resolution | string | 否 | 输出分辨率档位,默认 1k。可选:1k、2k、4k。与 aspect_ratio 组合决定实际像素,见下方尺寸与分辨率。 |
nsfw_checker | boolean | 否 | 安全检查开关,默认 true |
prompt 和 image_urls 至少传一个。
尺寸与分辨率
aspect_ratio 决定比例(或直接传像素尺寸),resolution 决定分辨率档位(1k / 2k / 4k),两者组合对应的实际像素如下。4k 支持全部 15 个比例;aspect_ratio=auto 时按 1:1 处理。
| 比例 | 1k | 2k | 4k |
|---|---|---|---|
1:1 | 1024×1024 / 1254×1254 | 2048×2048 | 2880×2880 |
3:2 | 1536×1024 | 2048×1360 | 3520×2336 |
2:3 | 1024×1536 | 1360×2048 | 2336×3520 |
4:3 | 1024×768 | 2048×1536 | 3312×2480 |
3:4 | 768×1024 | 1536×2048 | 2480×3312 |
5:4 | 1280×1024 / 1448×1086 | 2560×2048 | 3216×2576 |
4:5 | 1024×1280 / 1122×1402 | 2048×2560 | 2576×3216 |
16:9 | 1536×864 / 1672×941 | 2048×1152 | 3840×2160 |
9:16 | 864×1536 / 941×1672 | 1152×2048 | 2160×3840 |
2:1 | 2048×1024 / 1774×887 | 2688×1344 | 3840×1920 |
1:2 | 1024×2048 / 887×1774 | 1344×2688 | 1920×3840 |
3:1 | 1881×836 / 1536×512 | 3072×1024 | 3840×1280 |
1:3 | 887×1774 / 512×1536 | 1024×3072 | 1280×3840 |
21:9 | 2016×864 / 1915×821 | 2688×1152 | 3840×1648 |
9:21 | 864×2016 / 821×1915 | 1152×2688 | 1648×3840 |
也可直接通过 aspect_ratio 传入上表中的像素尺寸(例如 1881x836 / 887x1774)。
示例:文生图
curl -X POST https://api.aivideoapi.ai/v1/images/generations \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"callback_url": "https://your-server.com/webhook",
"input": {
"prompt": "一个雨夜霓虹城市海报,街道上有湿润反光。",
"aspect_ratio": "16:9",
"nsfw_checker": true
}
}'
示例:图生图编辑
curl -X POST https://api.aivideoapi.ai/v1/images/generations \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"callback_url": "https://your-server.com/webhook",
"input": {
"prompt": "把这个房间改造成温暖的北欧风室内设计效果图",
"image_urls": [
"https://example.com/room.jpg"
],
"aspect_ratio": "3:2"
}
}'
示例:4K 输出
curl -X POST https://api.aivideoapi.ai/v1/images/generations \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"callback_url": "https://your-server.com/webhook",
"input": {
"prompt": "星空下的古老城堡,电影级构图",
"aspect_ratio": "16:9",
"resolution": "4k"
}
}'
响应
{
"code": 200,
"msg": "success",
"data": {
"taskId": "397ce9f2-c04e-4244-ac33-3af19a7cc297"
}
}
查询任务
curl https://api.aivideoapi.ai/v1/tasks/{taskId} \
-H "Authorization: Bearer sk-your-api-key"
状态变化:pending -> processing -> completed 或 failed。
已完成
{
"id": "397ce9f2-c04e-4244-ac33-3af19a7cc297",
"status": "completed",
"model": "gpt-image-2",
"created_at": 1775383908,
"completed_at": 1775383910,
"output": {
"urls": [
"https://file.aivideoapi.ai/images/2026/04/05/abc123.png"
],
"metadata": {
"model": "gpt-image-2-image-to-image",
"costTime": 70
}
}
}
失败
{
"id": "57c8772c-f834-46f3-9b7d-81f92e104050",
"status": "failed",
"model": "gpt-image-2",
"created_at": 1775383908,
"error": {
"code": "upstream_error",
"message": "Generation failed"
}
}
任务失败时,预扣积分会自动退还。
回调通知
创建任务时传入 callback_url,任务完成或失败时系统会自动向该 URL 发送 POST 请求,格式与 GET /v1/tasks/{taskId} 响应一致。
常见错误码
请求失败时,API 返回 JSON 格式的错误响应:
{
"error": {
"code": "insufficient_credits",
"message": "Your credit balance is too low. Please top up.",
"type": "billing_error"
}
}
错误码一览
| HTTP 状态码 | 错误码 | 类型 | 说明 |
|---|---|---|---|
| 400 | invalid_request | invalid_request_error | 缺少必填参数或参数无效 |
| 401 | invalid_api_key | authentication_error | API Key 无效、已禁用或已删除 |
| 402 | insufficient_credits | billing_error | 积分余额不足,请充值 |
| 403 | ip_not_allowed | permission_error | 请求 IP 不在 Key 的白名单中 |
| 404 | model_not_found | invalid_request_error | 模型不存在或已停用 |
| 404 | task_not_found | invalid_request_error | 任务 ID 不存在 |
| 429 | rate_limit_exceeded | rate_limit_error | 请求过于频繁,请降低频率 |
| 429 | spend_limit_exceeded | billing_error | 达到 Key 的消费限额(每小时/每天/总量) |
| 500 | internal_error | api_error | 服务器内部错误 |
| 503 | upstream_error | upstream_error | 上游 AI 服务返回错误 |
常见场景
invalid_request (400)
缺少必填字段或参数格式错误时返回。
{
"error": {
"code": "invalid_request",
"message": "'model' is required.",
"type": "invalid_request_error"
}
}
insufficient_credits (402)
积分不足。可通过 GET /v1/credits 查询余额,前往 Dashboard > Billing 充值。
invalid_api_key (401)
可能原因:
- Key 不以
sk-开头 - Key 已被禁用或删除
- 用户账户已被封禁
upstream_error (503)
上游 AI 服务返回错误,可能原因:
- 输入内容包含敏感或违规信息
- 上游服务暂时不可用
- 请求参数不被上游支持
因上游错误导致任务失败时,预扣积分会自动退还。