← 返回首页

gpt-image-2 生图 API 调用

兼容 OpenAI 图像生成接口,使用 gpt-image-2 模型按文本提示词生成图片。

接口信息
Base URLhttps://niuflu.com/v1
EndpointPOST /images/generations
推荐模型gpt-image-2
鉴权HTTP 头 Authorization: Bearer sk-你的APIKey
核心参数
参数说明
size1024x1024(方形)、1024x1536(竖图)、1536x1024(横图)
quality设为 high 可提升清晰度,但会增加处理时间
n生成数量;保持为 1 更快且更稳定
timeout客户端超时建议至少 300 秒
调用示例
PowerShell curl
Python
JavaScript
curl https://niuflu.com/v1/images/generations `
  -H "Authorization: Bearer sk-你的APIKey" `
  -H "Content-Type: application/json" `
  -d '{
    "model": "gpt-image-2",
    "prompt": "A small green square centered on a white background.",
    "size": "1024x1024",
    "quality": "high",
    "n": 1
  }'
import requests

resp = requests.post(
    "https://niuflu.com/v1/images/generations",
    headers={
        "Authorization": "Bearer sk-你的APIKey",
        "Content-Type": "application/json",
    },
    json={
        "model": "gpt-image-2",
        "prompt": "A small green square centered on a white background.",
        "size": "1024x1024",
        "quality": "high",
        "n": 1,
    },
    timeout=300,
)
print(resp.json())
const resp = await fetch("https://niuflu.com/v1/images/generations", {
  method: "POST",
  headers: {
    "Authorization": "Bearer sk-你的APIKey",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    model: "gpt-image-2",
    prompt: "A small green square centered on a white background.",
    size: "1024x1024",
    quality: "high",
    n: 1,
  }),
});
const data = await resp.json();
console.log(data);
常见问题
建议先用基础参数测试:提示词 A small green square centered on a white background.,分辨率 1024x1024,确认通路正常后再调整其他参数。