IMG2 图片生成 API
调用 gpt-image-2 图片生成模型时,请使用 OpenAI Image Generations(图片生成)接口,而不是聊天补全接口。
本页要点图片模型接入先看这里
IMG2 使用 POST /v1/images/generations,不是 /v1/chat/completions。
模型名使用 gpt-image-2,令牌需要选择包含该模型的图片分组。
当前实测返回 b64_json,调用方应把 Base64(图片二进制文本)解码成图片文件。
图片生成接口的返回体可能很大。不要把完整
b64_json打到业务日志里,直接解码保存或上传到自己的对象存储。
一、接口地址
二、请求入参
{
"model": "gpt-image-2",
"prompt": "一张干净的产品海报,白色背景,科技感,方图",
"size": "1024x1024",
"quality": "low",
"n": 1,
"response_format": "b64_json"
}
三、响应出参
优先读取 data[0].b64_json。如果未来某些渠道返回 url,调用方也可以兼容读取 data[0].url。
{
"created": 1780915729,
"data": [
{
"b64_json": "这里是很长的 Base64 图片内容"
}
],
"usage": {
"input_tokens": 24,
"output_tokens": 229,
"total_tokens": 253
}
}
四、curl 示例
curl https://api.jojokey.com/v1/images/generations \
-H "Authorization: Bearer sk-请替换成自己的 JojoKey 令牌" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "一张干净的产品海报,白色背景,科技感,方图",
"size": "1024x1024",
"quality": "low",
"n": 1,
"response_format": "b64_json"
}'
五、JavaScript 示例
import fs from "node:fs";
const JOJOKEY_API_KEY = process.env.JOJOKEY_API_KEY;
const res = await fetch("https://api.jojokey.com/v1/images/generations", {
method: "POST",
headers: {
"Authorization": `Bearer ${JOJOKEY_API_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "gpt-image-2",
prompt: "一张干净的产品海报,白色背景,科技感,方图",
size: "1024x1024",
quality: "low",
n: 1,
response_format: "b64_json"
})
});
if (!res.ok) {
throw new Error(`JojoKey IMG2 request failed: ${res.status} ${await res.text()}`);
}
const json = await res.json();
const image = json.data?.[0];
if (image?.b64_json) {
fs.writeFileSync("jojokey-img2.png", Buffer.from(image.b64_json, "base64"));
} else if (image?.url) {
console.log(image.url);
} else {
throw new Error("No image returned from JojoKey IMG2");
}
六、Python 示例
import base64
import os
import requests
api_key = os.environ["JOJOKEY_API_KEY"]
url = "https://api.jojokey.com/v1/images/generations"
payload = {
"model": "gpt-image-2",
"prompt": "一张干净的产品海报,白色背景,科技感,方图",
"size": "1024x1024",
"quality": "low",
"n": 1,
"response_format": "b64_json",
}
response = requests.post(
url,
headers={
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
},
json=payload,
timeout=120,
)
response.raise_for_status()
image = response.json()["data"][0]
if "b64_json" in image:
with open("jojokey-img2.png", "wb") as f:
f.write(base64.b64decode(image["b64_json"]))
elif "url" in image:
print(image["url"])
else:
raise RuntimeError("No image returned from JojoKey IMG2")
七、常见错误
401:令牌错误、令牌被禁用,或请求没有按Authorization: Bearer sk-...发送。403:令牌无权访问当前图片分组,需要重新创建或调整令牌分组。503 No available channel:当前分组下没有可用的gpt-image-2渠道,请联系 JojoKey 排查渠道状态。522 / 524:网络链路或上游响应超时,建议稍后重试;持续出现时把 request id(请求编号)发给客服。- 返回体很大:
b64_json是图片本体,天然很长。不要打印到日志,直接解码保存。