鉴权与连接
- Base URL
https://backend:8000- Authorization
Authorization: Bearer YOUR_API_KEY- Content-Type
application/json- 模型名 (model)
gpt-image-2
接入协议
为什么选这个协议
适合已有 OpenAI Images 图片生成接口代码,且希望同步拿到图片结果的用户。
接口地址
POST/v1/images/generations
Authorization: Bearer YOUR_API_KEYContent-Type: application/json请求 / 响应 JSON
请求示例
图片链接方式reference_images[]
{
"model": "gpt-image-2",
"prompt": "生成一张产品海报,白色背景,商业摄影风格",
"size": "2752x1536",
"response_format": "url",
"reference_images": [
"https://example.com/reference.png"
]
}Base64 方式reference_images[].mime_type + data_base64
{
"model": "gpt-image-2",
"prompt": "生成一张产品海报,白色背景,商业摄影风格",
"size": "2752x1536",
"response_format": "url",
"reference_images": [
{
"mime_type": "image/jpeg",
"data_base64": "BASE64_IMAGE_DATA"
}
]
}响应示例
{
"created": 1710000000,
"data": [
{
"url": "https://example.com/output.png"
}
]
}请求字段 / 响应字段
// INPUT SCHEMA
| 字段 | 类型 | 必填 |
|---|---|---|
| model | string | Y |
| prompt | string | Y |
| size | string | Y |
| response_format | string | Y |
| reference_images | array | Y |
// RESPONSE SCHEMA
| 字段 | 类型 | 必填 |
|---|---|---|
| created | integer | Y |
| data | array | Y |
| data[] | object | Y |
| data[].url | string | Y |
代码示例
cURLrequest.sh
curl -X POST "https://backend:8000/v1/images/generations" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "gpt-image-2",
"prompt": "生成一张产品海报,白色背景,商业摄影风格",
"size": "2752x1536",
"response_format": "url",
"reference_images": [
"https://example.com/reference.png"
]
}'
# 上传参考文件 cURL
curl -X POST "https://backend:8000/v1/files/upload" \
-H "Authorization: Bearer YOUR_API_KEY" \
-F "kind=image" \
-F "purpose=reference" \
-F "file=@demo.png"为什么选这个协议
适合需要提交任务并轮询状态的图像、视频、音频生成工作流。
接口地址
POST/v1/jobs
Authorization: Bearer YOUR_API_KEYContent-Type: application/json请求 / 响应 JSON
// REQUEST
{
"model": "gpt-image-2",
"input": {
"prompt": "生成一张产品海报,白色背景,商业摄影风格",
"image_url": "https://example.com/input.png",
"reference_image_urls": [
"https://example.com/reference.png"
],
"reference_images": [
"https://example.com/reference.png"
],
"mask": "https://example.com/mask.png",
"size": "2752x1536",
"quality": "medium",
"output_format": "png",
"response_format": "url",
"background": "auto",
"n": 1,
"user": "user-123",
"aspect_ratio": "16:9"
}
}// RESPONSE
{
"id": "job_example",
"object": "job",
"status": "processing",
"model": "gpt-image-2",
"created": 1710000000,
"request_id": "req_example"
}请求字段 / 响应字段
// INPUT SCHEMA
| 字段 | 类型 | 必填 |
|---|---|---|
| prompt | string | Y |
| image_url | image | N |
| reference_image_urls | array | N |
| reference_images | array | N |
| mask | image | N |
| size | string | N |
| quality | enum | N |
| output_format | enum | N |
| response_format | enum | N |
| background | enum | N |
| n | integer | N |
| user | string | N |
| aspect_ratio | enum | N |
// RESPONSE SCHEMA
| 字段 | 类型 | 必填 |
|---|---|---|
| id | string | N |
| status | enum | N |
| model | string | N |
| output.images[].url | image | N |
| output.images[].mime_type | string | N |
代码示例
cURLrequest.sh
curl -X POST "https://backend:8000/v1/jobs" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "gpt-image-2",
"input": {
"prompt": "生成一张产品海报,白色背景,商业摄影风格",
"image_url": "https://example.com/input.png",
"reference_image_urls": [
"https://example.com/reference.png"
],
"reference_images": [
"https://example.com/reference.png"
],
"mask": "https://example.com/mask.png",
"size": "2752x1536",
"quality": "medium",
"output_format": "png",
"response_format": "url",
"background": "auto",
"n": 1,
"user": "user-123",
"aspect_ratio": "16:9"
}
}'
# 上传参考文件 cURL
curl -X POST "https://backend:8000/v1/files/upload" \
-H "Authorization: Bearer YOUR_API_KEY" \
-F "kind=image" \
-F "purpose=reference" \
-F "file=@demo.png"
# 查询任务状态 cURL
# 将 job_example 替换为提交任务返回的 task_id
curl -X GET "https://backend:8000/v1/jobs/job_example" \
-H "Authorization: Bearer YOUR_API_KEY"