Skip to content

OpenAI 生成图片

/v1/images/generations 是 OpenAI Images API 兼容的同步生图接口。HTTP 请求会在图片结果就绪后返回。

接口说明

项目
MethodPOST
Path/v1/images/generations
Content-Typeapplication/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/generationsPOST /v1/images/edits,也支持无 /v1 前缀的 /images/generations/images/edits 别名,不支持 /v1/responses 或对话接口。如果同步等待超时且响应包含 task_id,请使用任务 API 查询最终状态。

请求参数

参数类型必填说明
modelstring图片模型。未传时默认 gpt-image-2。普通 API Key 需使用 OpenAI Images 兼容模型;任务 API Key 可使用任务 API 模型列表中的图片模型。
promptstring生图提示词。
sizestring输出尺寸,例如 1024x1024
ninteger生成图片数量,默认 1,必须大于 0
response_formatstring响应格式。普通 API Key 常用 b64_json;任务 API Key 支持 urlb64_json,默认 url
streamboolean是否流式返回,取决于所选模型能力;任务 API Key 在本接口不支持 stream=true
qualitystringOpenAI Images API 参数,是否生效取决于所选模型。
backgroundstringOpenAI Images API 参数,是否生效取决于所选模型。
output_formatstringOpenAI Images API 参数,是否生效取决于所选模型。
output_compressionintegerOpenAI Images API 参数,是否生效取决于所选模型。
moderationstringOpenAI Images API 参数,是否生效取决于所选模型。
stylestringOpenAI 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_jsonresponse_formatb64_json 时返回的图片 Base64。
data[].url当 URL 响应可用时返回的图片地址。

使用任务 API Key 时,url 会返回可访问的图片资产地址;如果客户端需要内联图片数据,请使用 response_format: "b64_json"

错误码

状态码原因
400请求体无效、模型不是图片模型,或缺少必要参数。
401API 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 兼容的生成和编辑接口,但不能调用文本或对话接口。