端点 POST /v1/images/generations
与 OpenAI Images API 兼容,用于 gpt-image-2。生图耗时较长(实测数十秒),客户端超时建议设为 300 秒。
请求
| 请求头 | 取值 |
|---|---|
Authorization | Bearer sk-xxxxxx |
content-type | application/json |
最小请求体:
json
{
"model": "gpt-image-2",
"prompt": "一只在键盘上打字的橘猫,插画风格"
}参数
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | gpt-image-2 |
prompt | string | 是 | 图像描述 |
n | integer | 否 | 生成张数,默认 1。data 数组按张数返回 |
size | string | 否 | 输出尺寸 宽x高,如 1024x1024;或 auto |
quality | string | 否 | low、medium、high 或 auto |
background | string | 否 | transparent、opaque 或 auto。transparent 时返回带透明通道的图片,需搭配 output_format 为 png 或 webp |
output_format | string | 否 | png(默认)、jpeg 或 webp |
output_compression | integer | 否 | 0 到 100,仅对 jpeg 与 webp 生效,数值越小文件越小 |
moderation | string | 否 | 内容审核强度,low 或 auto |
stream | boolean | 否 | 设为 true 时以 SSE 流式返回,格式见下 |
partial_images | integer | 否 | 0 到 3。当前实现中间分片不带图片,只有最后一个分片带结果 |
user | string | 否 | 调用方自定义的最终用户标识,原样透传 |
其余字段按 OpenAI Images API 格式透传。
无法识别的字段会被忽略,不会报错。
响应
json
{
"created": 1752345600,
"data": [
{ "b64_json": "iVBORw0KGgo..." }
],
"usage": {
"input_tokens": 10,
"output_tokens": 272,
"total_tokens": 282,
"input_tokens_details": { "text_tokens": 10, "image_tokens": 0, "cached_tokens": 0 },
"output_tokens_details": { "text_tokens": 0, "image_tokens": 272, "reasoning_tokens": 0 }
}
}| 字段 | 说明 |
|---|---|
created | 创建时间(Unix 秒) |
data[].b64_json | Base64 编码的图片,格式由 output_format 决定 |
usage | 本次请求的用量,图片按 output_tokens_details.image_tokens 计 |
stream 为 true 时响应为 text/event-stream,每行 data: 是一个 JSON:进度分片的 object 为 image.generation.chunk,data 为空;最后一个分片的 object 为 image.generation.result,data[0].b64_json 是结果图;随后是 data: [DONE]。
示例
基础请求
bash
curl https://caapi.top/v1/images/generations \
-H "Authorization: Bearer sk-xxxxxx" \
-H "content-type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "一只在键盘上打字的橘猫,插画风格"
}'透明背景
bash
curl https://caapi.top/v1/images/generations \
-H "Authorization: Bearer sk-xxxxxx" \
-H "content-type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "一个红色圆形图标,透明背景",
"size": "1024x1024",
"quality": "low",
"background": "transparent",
"output_format": "png"
}'JPEG 压缩输出
bash
curl https://caapi.top/v1/images/generations \
-H "Authorization: Bearer sk-xxxxxx" \
-H "content-type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "一片蓝天下的草地",
"output_format": "jpeg",
"output_compression": 50
}'错误
| HTTP 状态 | error.type | 触发条件 |
|---|---|---|
| 401 | missing_auth_credential / invalid_api_key / invalid_bearer_token | 鉴权失败,详见 鉴权 |
| 400 | insufficient_balance | 账户余额不足,details.recharge_url 是充值地址 |