TokenFlashTokenFlash
Api referenceImages

图像生成

使用 GPT Image 2 根据文本提示词生成图片

使用 gpt-image-2 根据文本提示词生成图片。接口兼容 OpenAI Images API,并在图片生成完成后同步返回结果。

说明

图片生成通常需要几十秒到数分钟。建议将客户端请求超时时间设置为至少 300 秒。

请求示例

curl --request POST \
  --url https://tokenflash.cn/v1/images/generations \
  --header 'Authorization: Bearer YOUR_API_KEY' \
  --header 'Content-Type: application/json' \
  --data '{
    "model": "gpt-image-2",
    "prompt": "一只橘猫坐在窗台上看夕阳,水彩画风格",
    "n": 1,
    "size": "1024x1024"
  }'
import requests

response = requests.post(
    "https://tokenflash.cn/v1/images/generations",
    headers={"Authorization": "Bearer YOUR_API_KEY"},
    json={
        "model": "gpt-image-2",
        "prompt": "一只橘猫坐在窗台上看夕阳,水彩画风格",
        "n": 1,
        "size": "1024x1024",
    },
    timeout=300,
)
response.raise_for_status()
print(response.json())
const response = await fetch('https://tokenflash.cn/v1/images/generations', {
  method: 'POST',
  headers: {
    Authorization: 'Bearer YOUR_API_KEY',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    model: 'gpt-image-2',
    prompt: '一只橘猫坐在窗台上看夕阳,水彩画风格',
    n: 1,
    size: '1024x1024',
  }),
});

if (!response.ok) throw new Error(await response.text());
console.log(await response.json());

请求参数

string required

Bearer API Key,例如 Bearer sk-xxxxxxxx

string required

固定填写 gpt-image-2

string required

图片生成提示词。支持中文和英文。

integer

生成图片数量。建议使用 1;实际可用范围取决于当前渠道。

string

输出尺寸,例如 1024x1024。实际支持的尺寸取决于当前渠道。

string

图片质量,例如 autostandardhigh。该参数可能因渠道不同而被忽略。

string

期望的返回格式,可传 urlb64_json。客户端应同时兼容这两种实际返回形式。

成功响应

图片通常通过 b64_jsonurl 返回。

{
  "created": 1784039704,
  "data": [
    {
      "b64_json": "iVBORw0KGgoAAAANSUhEUgAA..."
    }
  ]
}
{
  "created": 1784039704,
  "data": [
    {
      "url": "https://example.com/generated-image.png"
    }
  ]
}
{
  "error": {
    "message": "prompt is required",
    "type": "invalid_request_error"
  }
}

保存 Base64 图片

jq -r '.data[0].b64_json' response.json | base64 --decode > result.png

需要基于参考图像生成内容时,请使用图像编辑接口

On this page