生图 API 接入¶
本页提供 GPT Image 和 Gemini 图片 API 的 cURL 示例。模型与参数速查见 模型与分组,Grok 的调用方式见 Grok 生图 API。
准备地址与密钥¶
| 项目 | 说明 |
|---|---|
| Base URL | https://jarlessapi.com |
| 认证方式 | Authorization: Bearer <api_key> |
| 超时时间 | 同步/流式请求建议 300s |
| 响应格式 | JSON / Server-Sent Events (SSE) |
以下 cURL 示例在 Bash 中执行(macOS / Linux / WSL / Git Bash)。
export BASE_URL="https://jarlessapi.com"
export API_KEY="你的生图APIkey"
可选:检查密钥
OpenAI Images Key 可用以下接口验证:
curl "$BASE_URL/v1/models" \
-H "Authorization: Bearer $API_KEY"
Gemini 图片生成接口支持直连 /v1beta。如果只验证模型列表,部分部署可能未开放裸 /v1beta/models;应以实际生成接口验证为准。
请求示例¶
选择要调用的图片服务。响应示例中的图片 base64 已用占位符省略。
GPT Image 各型号沿用相同的接口和参数,按所选型号替换示例中的 model。size 表示期望尺寸,实际输出像素可能因型号不同而变化,请读取返回图片确认。
文生图
stream:false 时,请求完成后一次性返回 JSON。图片在 data[0].b64_json。
curl --max-time 300 "$BASE_URL/v1/images/generations" \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "A simple black checkmark icon on a white background",
"n": 1,
"size": "1024x1024",
"quality": "low",
"response_format": "b64_json",
"stream": false
}'
查看响应示例
{
"created": 1778903610,
"data": [
{
"b64_json": "<base64 encoded image data>",
"revised_prompt": "...",
"output_format": "png"
}
]
}
流式文生图
stream:true 时返回 SSE。不同上游会返回两种格式:image_generation.completed 事件的顶层 b64_json,或最后一个数据块中的 data[0].b64_json。客户端应兼容两种格式;进度块中的空 data 和 partial_image 都不是最终结果。
curl -N --max-time 300 "$BASE_URL/v1/images/generations" \
-H "Authorization: Bearer $API_KEY" \
-H "Accept: text/event-stream" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "A simple red square icon on a white background",
"n": 1,
"size": "1024x1024",
"quality": "low",
"response_format": "b64_json",
"stream": true,
"partial_images": 1
}'
响应事件示例:
event: image_generation.partial_image
data: {"type":"image_generation.partial_image","partial_image_index":0,"b64_json":"<base64>"}
event: image_generation.completed
data: {"type":"image_generation.completed","b64_json":"<base64>"}
另一种实际响应(flare、sunburst 已验证):
data: {"object":"image.generation.chunk","progress_text":"...","data":[]}
data: {"object":"image.generation.result","data":[{"b64_json":"<base64>"}]}
data: [DONE]
图生图
单张参考图使用字段 image,多张参考图使用 image[0]、image[1],最多 16 张。
curl --max-time 300 "$BASE_URL/v1/images/edits" \
-H "Authorization: Bearer $API_KEY" \
-F "model=gpt-image-2" \
-F "prompt=Change the circle to green while keeping a plain white background" \
-F "image=@ref.png" \
-F "size=1024x1024" \
-F "quality=low" \
-F "response_format=b64_json"
使用多张参考图
curl --max-time 300 "$BASE_URL/v1/images/edits" \
-H "Authorization: Bearer $API_KEY" \
-F "model=gpt-image-2" \
-F "prompt=Blend these references into one clean product image" \
-F "image[0]=@ref-1.png" \
-F "image[1]=@ref-2.png" \
-F "size=1024x1024" \
-F "quality=low" \
-F "response_format=b64_json"
参数参考
| 参数 | 类型 | 说明 |
|---|---|---|
| model | string | 从 模型列表 中选择 GPT Image 型号 |
| prompt | string | 图片提示词 |
| n | number | 生成数量,默认 1 |
| size | string | 输出尺寸,例如 1024x1024、1536x1024、1024x1536、3840x2160、auto |
| quality | string | low、medium、high、auto |
| response_format | string | b64_json 或 url |
| stream | boolean | 是否使用 OpenAI Images SSE |
| partial_images | number | 流式中间图数量,当前建议 1 |
请求体需包含 generationConfig.responseModalities: ["IMAGE"]。最终图片位于 candidates[].content.parts[].inlineData.data。
文生图
curl --max-time 300 "$BASE_URL/v1beta/models/gemini-3.1-flash-image:generateContent" \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"contents": [
{
"role": "user",
"parts": [
{ "text": "Draw a simple yellow star icon on a white background" }
]
}
],
"generationConfig": {
"responseModalities": ["IMAGE"],
"imageConfig": {
"aspectRatio": "1:1",
"imageSize": "1K"
}
}
}'
查看响应示例
{
"candidates": [
{
"content": {
"parts": [
{
"inlineData": {
"mimeType": "image/png",
"data": "<base64 encoded image data>"
}
}
]
},
"finishReason": "STOP"
}
],
"modelVersion": "gemini-3.1-flash-image"
}
流式文生图
流式接口会先返回 queued/running 文本块,最终返回 inlineData.data。不要因为中间文本块没有图片就判定失败。
curl -N --max-time 300 "$BASE_URL/v1beta/models/gemini-3.1-flash-image:streamGenerateContent?alt=sse" \
-H "Authorization: Bearer $API_KEY" \
-H "Accept: text/event-stream" \
-H "Content-Type: application/json" \
-d '{
"contents": [
{
"role": "user",
"parts": [
{ "text": "Draw a simple cyan hexagon icon on a white background" }
]
}
],
"generationConfig": {
"responseModalities": ["IMAGE"],
"imageConfig": {
"aspectRatio": "1:1",
"imageSize": "1K"
}
}
}'
响应事件示例:
data: {"candidates":[{"content":{"role":"model","parts":[{"text":"image task queued (0%)"}]},"index":0}],"modelVersion":"gemini-3.1-flash-image"}
data: {"candidates":[{"content":{"role":"model","parts":[{"text":"image task running (10%)"}]},"index":0}],"modelVersion":"gemini-3.1-flash-image"}
data: {"candidates":[{"content":{"role":"model","parts":[{"inlineData":{"mimeType":"image/png","data":"<base64>"}}]},"finishReason":"STOP","index":0}],"modelVersion":"gemini-3.1-flash-image"}
data: [DONE]
图生图
参考图使用 inlineData,data 为不带 data:image/...;base64, 前缀的 base64。
IMAGE_B64="$(base64 < ref.png | tr -d '\r\n')"
curl --max-time 300 "$BASE_URL/v1beta/models/gemini-3.1-flash-image:generateContent" \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d "{
\"contents\": [
{
\"role\": \"user\",
\"parts\": [
{ \"text\": \"Turn this reference image into a watercolor style image\" },
{
\"inlineData\": {
\"mimeType\": \"image/png\",
\"data\": \"$IMAGE_B64\"
}
}
]
}
],
\"generationConfig\": {
\"responseModalities\": [\"IMAGE\"],
\"imageConfig\": {
\"aspectRatio\": \"1:1\",
\"imageSize\": \"1K\"
}
}
}"
参数参考
| 参数 | 说明 |
|---|---|
| contents[].parts[].text | 图片提示词 |
| contents[].parts[].inlineData | 参考图 base64 |
| generationConfig.responseModalities | 必须包含 IMAGE |
| imageConfig.aspectRatio | auto、1:1、16:9、9:16、4:3、3:4、3:2、2:3、5:4、4:5、21:9、1:4、4:1、1:8、8:1 |
| imageConfig.imageSize | 1K、2K、4K |
错误处理¶
| 状态码 | 含义 | 建议处理 |
|---|---|---|
| 400 | 参数错误 | 检查请求体和模型名 |
| 401 | API Key 无效或缺失 | 检查 Authorization |
| 402 | 余额不足 | 充值或更换 Key |
| 403 | 无权访问 | 检查 Key 分组和模型权限 |
| 404 | 接口不存在或路径错误 | 检查 Base URL 和路径 |
| 413 | 请求体过大 | 减少参考图数量或压缩图片 |
| 429 | 请求过多 | 降低并发,指数退避 |
| 500+ | 服务端或上游错误 | 稍后重试 |
GPT Image 错误响应
{
"error": {
"type": "invalid_request_error",
"message": "prompt is required"
}
}
Gemini 错误响应
{
"error": {
"code": 400,
"message": "Request body is empty",
"status": "INVALID_ARGUMENT"
}
}