API 参考
使用 GPT Image 2 开放 API 以编程方式生成和编辑图像。用你自己的 API 密钥进行身份验证,用积分付费,通过轮询获取结果。
概述
GPT Image 2 API 允许你通过 GPT Image 2 账户生成图像。请求使用你的站点 API 密钥,并通过我们服务端的 Kie 集成进行路由。
从以下路径创建 API 密钥:
/settings/apikeys然后调用:
POST /api/v1/images/generations
GET /api/v1/images/tasks/{task_id}图像生成是异步的。创建端点返回一个任务 id,任务端点在生成成功后返回最终的图像 URL。
身份验证
将你的 GPT Image 2 API 密钥作为 Bearer token 发送:
Authorization: Bearer sk-xxx不要在浏览器端代码中暴露 API 密钥。请在后端、脚本或 serverless 函数中使用。
计费
API 使用消耗与 Web 应用相同的积分:
| 操作 | 积分费用 |
|---|---|
| 图像生成 | 每张图像 10 积分 |
如果提供商请求在任务被接受之前失败,已消耗的积分将由任务失败流程自动退还。
创建图像生成
端点
POST https://gpt-image-2.art/api/v1/images/generations请求体
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
model | string | 否 | 使用 gpt-image-2。内部别名 gpt-image-2-text-to-image 和 gpt-image-2-image-to-image 也受支持。 |
prompt | string | 是 | 图像的文本描述。除非提供了 image_urls,否则为必填项。 |
image_urls | string[] | 否 | 用于图生图的参考图像 URL。 |
n | integer | 否 | 要生成的图像数量,范围 1 到 10。默认值:1。 |
size | string | 否 | 1024x1024、1536x1024、1024x1536、1920x1088、1088x1920、3824x2160、2160x3824 或 auto 之一。 |
quality | string | 否 | 提供商质量选项,例如 high 或 auto。 |
output_format | string | 否 | 提供商支持时可选 png、jpeg 或 webp。 |
response_format | string | 否 | 仅支持 url。 |
user | string | 否 | 可选的终端用户标识符,用于你自己的追踪。 |
文生图示例
curl https://gpt-image-2.art/api/v1/images/generations \
-H "Authorization: Bearer $GPT_IMAGE_2_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "A cinematic product photo of a matte black coffee bag on a marble counter",
"size": "1536x1024",
"quality": "high",
"n": 1
}'图生图示例
curl https://gpt-image-2.art/api/v1/images/generations \
-H "Authorization: Bearer $GPT_IMAGE_2_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "Keep the product unchanged and place it on a clean studio background",
"image_urls": ["https://example.com/product.png"],
"size": "1024x1024",
"n": 1
}'创建响应
{
"id": "8f6d9f87-1b3f-4f89-bdfb-3df8e77b7af3",
"object": "image.generation",
"created": 1714000000,
"model": "gpt-image-2-text-to-image",
"status": "pending",
"credits": 10,
"data": []
}保存 id 并轮询任务端点。
查询图像任务
端点
GET https://gpt-image-2.art/api/v1/images/tasks/{task_id}示例
curl https://gpt-image-2.art/api/v1/images/tasks/8f6d9f87-1b3f-4f89-bdfb-3df8e77b7af3 \
-H "Authorization: Bearer $GPT_IMAGE_2_API_KEY"成功响应
{
"id": "8f6d9f87-1b3f-4f89-bdfb-3df8e77b7af3",
"object": "image.generation",
"created": 1714000000,
"model": "gpt-image-2-text-to-image",
"status": "succeeded",
"credits": 10,
"data": [
{
"url": "https://..."
}
]
}可能的 status 值:
| 状态 | 含义 |
|---|---|
pending | 任务已排队。 |
processing | 提供商正在生成图像。 |
succeeded | 图像 URL 已在 data 中提供。 |
failed | 生成失败。 |
canceled | 生成已取消。 |
JavaScript 示例
const apiKey = process.env.GPT_IMAGE_2_API_KEY;
const createRes = await fetch(
'https://gpt-image-2.art/api/v1/images/generations',
{
method: 'POST',
headers: {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'gpt-image-2',
prompt:
'A premium skincare bottle on wet stone with soft studio lighting',
size: '1024x1024',
n: 1,
}),
}
);
const task = await createRes.json();
let result = task;
while (['pending', 'processing'].includes(result.status)) {
await new Promise((resolve) => setTimeout(resolve, 3000));
const queryRes = await fetch(
`https://gpt-image-2.art/api/v1/images/tasks/${task.id}`,
{
headers: {
Authorization: `Bearer ${apiKey}`,
},
}
);
result = await queryRes.json();
}
console.log(result.data);错误格式
错误遵循 OpenAI 风格的结构:
{
"error": {
"message": "Invalid API key provided.",
"type": "authentication_error",
"param": null,
"code": "invalid_api_key"
}
}相关资源
- 价格与积分 — 单次调用积分成本与积分包
- 提示词指南 — 让 API 调用产出更好结果
- 使用案例 — 真实 API 集成场景模式
- 作品展示 — 看看 GPT Image 2 能做什么
- OpenAI Image API 官方文档 — 自带 OpenAI Key 用户的官方上游参考