OpenAI 生成图片
/v1/images/generations 是 OpenAI Images API 兼容的同步生图接口。HTTP 请求会在图片结果就绪后返回。
接口说明
| 项目 | 值 |
|---|---|
| Method | POST |
| Path | /v1/images/generations |
| Content-Type | application/json |
| 认证 | Authorization: Bearer YOUR_API_KEY |
| 平台要求 | 普通 API Key 需要 OpenAI 平台分组且分组启用生图能力;任务 API Key 可使用本同步图片接口 |
API Key 行为
| Key 类型 | 行为 |
|---|---|
| 普通 API Key / OpenAI 分组 | 走同步 OpenAI Images 网关,行为与原接口一致。 |
| 任务 API Key | 使用任务 API 的模型和价格配置,同步等待最多 120 秒,再返回 OpenAI Images 风格响应。 |
任务 API Key 支持 POST /v1/images/generations 和 POST /v1/images/edits,也支持无 /v1 前缀的 /images/generations、/images/edits 别名,不支持 /v1/responses 或对话接口。如果同步等待超时且响应包含 task_id,请使用任务 API 查询最终状态。
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 否 | 图片模型。未传时默认 gpt-image-2。普通 API Key 需使用 OpenAI Images 兼容模型;任务 API Key 可使用任务 API 模型列表中的图片模型。 |
prompt | string | 是 | 生图提示词。 |
size | string | 否 | 输出尺寸,例如 1024x1024。 |
n | integer | 否 | 生成图片数量,默认 1,必须大于 0。 |
response_format | string | 否 | 响应格式。普通 API Key 常用 b64_json;任务 API Key 支持 url 和 b64_json,默认 url。 |
stream | boolean | 否 | 是否流式返回,取决于所选模型能力;任务 API Key 在本接口不支持 stream=true。 |
quality | string | 否 | OpenAI Images API 参数,是否生效取决于所选模型。 |
background | string | 否 | OpenAI Images API 参数,是否生效取决于所选模型。 |
output_format | string | 否 | OpenAI Images API 参数,是否生效取决于所选模型。 |
output_compression | integer | 否 | OpenAI Images API 参数,是否生效取决于所选模型。 |
moderation | string | 否 | OpenAI Images API 参数,是否生效取决于所选模型。 |
style | string | 否 | OpenAI Images API 参数,是否生效取决于所选模型。 |
请求示例
bash
curl https://api.llmapi.site/v1/images/generations \
-H 'Authorization: Bearer YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"model": "gpt-image-2",
"prompt": "a product photo of a ceramic coffee cup on a white table",
"size": "1024x1024",
"n": 1,
"response_format": "b64_json"
}'响应 — 200 OK
json
{
"created": 1779912000,
"data": [
{
"b64_json": "..."
}
]
}| 字段 | 说明 |
|---|---|
created | 响应创建时间,Unix 时间戳。 |
data[].b64_json | 当 response_format 为 b64_json 时返回的图片 Base64。 |
data[].url | 当 URL 响应可用时返回的图片地址。 |
使用任务 API Key 时,url 会返回可访问的图片资产地址;如果客户端需要内联图片数据,请使用 response_format: "b64_json"。
错误码
| 状态码 | 原因 |
|---|---|
400 | 请求体无效、模型不是图片模型,或缺少必要参数。 |
401 | API Key 缺失或无效。 |
403 | 余额、额度或分组权限校验失败。 |
404 | 当前 API Key 分组不能调用 OpenAI Images API。 |
429 | 达到频率限制或并发限制。 |
500 / 502 / 503 | 网关或上游服务商失败。 |
504 | 任务 API Key 同步等待超时;如果后续可查询最终状态,响应会包含 task_id。 |
任务 API Key 超时示例:
json
{
"error": {
"type": "TASK_SYNC_TIMEOUT",
"message": "task did not complete before synchronous wait timeout",
"task_id": "task_..."
}
}适用边界
- 本接口只适用于 OpenAI Images API 兼容请求。
- 长耗时生产任务建议使用 任务 API。
- 任务 API Key 可以调用 OpenAI Images 兼容的生成和编辑接口,但不能调用文本或对话接口。