生成图片接口,在 data[].url 返回图片链接,b64_json 为空字符串,链接 1~24 小时内有效。
传入 image 数组(Base64 / data URI / URL)后转为图生图,仍由本接口处理并返回 url。如需 base64 输出或带 mask 的局部重绘,请用 /v1/images/edits。
| 线路 | Base URL | 说明 |
|---|---|---|
| 国内加速(推荐) | https://cdn.wusag.com | 走国内 CDN 节点 |
| 海外直连 | https://api.xinliu.one | 海外服务器或加速节点异常时使用 |
两个地址共用同一账户和 Key。
POST {Base URL}/v1/images/generationsAuthorization: Bearer 你的令牌Content-Type: application/json| model | 备注 |
|---|---|
gpt-image-2 | 标准版 |
gpt-image-2-pro | Pro 版 |
gpt-image-2-2026-04-21 | OpenAI 官方版本(直连) |
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | 见上表 |
prompt | string | 是 | 文本提示词,最大 32000 字符 |
n | int | 否 | 生成数量,1~10,默认 1 |
size | string | 否 | 输出尺寸,见下表;默认 auto |
quality | string | 否 | low / medium / high / auto(默认) |
background | string | 否 | auto(默认)/ opaque。gpt-image-2 不支持 transparent,需要透明背景请用 gpt-image-1.5;transparent 时 output_format 必须为 png 或 webp |
output_format | string | 否 | png(默认)/ webp / jpeg |
output_compression | int | 否 | 0~100,仅 webp / jpeg,默认 100 |
moderation | string | 否 | auto(默认)/ low |
stream | bool | 否 | 是否流式返回,默认 false |
partial_images | int | 否 | 流式中间图数量,0~3。stream=true 时必须 ≥ 1 |
image | array | 否 | 参考图数组,元素为裸 Base64、data:image/png;base64,... 或公网 URL。传入后转为图生图模式 |
user | string | 否 | 终端用户唯一标识 |
response_format 参数对 gpt-image 系列不适用,传入会被忽略或返回 unknown_parameter 错误。
size 取值| 取值 | 说明 |
|---|---|
1024x1024 | 正方形 |
1536x1024 | 横版 |
1024x1536 | 竖版 |
2048x2048 | 2K 正方形 |
2048x1152 | 2K 横版 |
3840x2160 | 4K 横版 |
2160x3840 | 4K 竖版 |
auto | 默认,由模型选择 |
自定义尺寸需满足:边长是 16 的倍数;单边 ≤ 3840px;宽高比 < 3:1;总像素 ∈ [655 360, 8 294 400]。
| 字段 | 类型 | 说明 |
|---|---|---|
data[].url | string | 图片链接(本端点返回),1~24h 有效 |
data[].b64_json | string | base64(本端点返回空字符串) |
data[].revised_prompt | string | 模型对原始 prompt 的改写(如启用) |
created | int | Unix 时间戳 |
usage.input_tokens | int | 输入 token 数 |
usage.output_tokens | int | 输出图像 token 数 |
usage.total_tokens | int | 总 token |
usage.input_tokens_details.text_tokens | int | 提示词文本 token |
usage.input_tokens_details.image_tokens | int | 输入图 token(仅 edits 端点非零) |
{
"data": [{
"url": "https://files.example.com/img/<uuid>.png",
"b64_json": "",
"revised_prompt": ""
}],
"created": 1778069104
}
服务端按 Server-Sent Events 推送:先若干条 partial_image 事件,再一条最终事件。partial_images 必须 ≥ 1;缺省或填 0 时 400:
{
"error": {
"message": "Streaming must have non-zero partial images",
"type": "invalid_request_error",
"code": null
}
}
curl https://cdn.wusag.com/v1/images/generations \
-H "Authorization: Bearer 你的令牌" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "A sharp product hero image, dark background",
"size": "1024x1024",
"quality": "high",
"n": 1
}'
传入 image 数组即可,无需切换到 /v1/images/edits:
curl https://cdn.wusag.com/v1/images/generations \
-H "Authorization: Bearer 你的令牌" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "Add a glowing aurora effect, keep composition unchanged",
"image": ["https://files.example.com/img/source.png"],
"size": "1024x1024"
}'
image 数组支持多张参考图(多元素),元素之间可混用 Base64、data URI、URL。
import OpenAI from "openai";
const openai = new OpenAI({
baseURL: "https://cdn.wusag.com/v1",
apiKey: process.env.XINLIU_API_KEY,
});
const result = await openai.images.generate({
model: "gpt-image-2",
prompt: "Create a clean product-style image of a white desk lamp",
size: "1536x1024",
quality: "medium",
output_format: "webp",
});
from openai import OpenAI
client = OpenAI(
base_url="https://cdn.wusag.com/v1",
api_key="sk-你的令牌",
)
resp = client.images.generate(
model="gpt-image-2",
prompt="A modern logo for an AI startup, minimalist style",
size="1024x1024",
quality="high",
n=1,
)
print(resp.data[0].url)
通用错误体:
{
"error": {
"message": "具体错误描述",
"type": "invalid_request_error",
"param": "model",
"code": "invalid_value"
}
}
| 状态码 | 说明 |
|---|---|
400 | 参数错误(invalid_request_error,常见 unknown_parameter / invalid_value / unsupported_model) |
401 | 令牌无效或未提供(invalid_api_key) |
402 | 余额不足 |
404 | 模型不存在或未开通 |
429 | 速率限制(rate_limit_exceeded) |
500 | 服务端错误 |