跳转到主要内容
POST
/
v1
/
images
/
generations
文生图:根据文本描述生成图片
curl --request POST \
  --url https://api.apiyi.com/v1/images/generations \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --data '
{
  "model": "gpt-image-2-all",
  "prompt": "横版 16:9 电影画幅,黄昏时的海边老灯塔",
  "response_format": "url"
}
'
{
  "data": [
    {
      "url": "https://r2cdn.copilotbase.com/r2cdn2/00aaa9fb-756c-4119-a4a0-4a44fc75152b.png",
      "b64_json": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
    }
  ]
}
右侧的交互式 Playground 支持直接在线调试。请在 Authorization 中填入你的 API Key(格式:Bearer sk-xxx),输入 prompt 后一键发送即可。
场景说明:本页用于「文本生成图片」。只需输入提示词即可,无需上传任何图片。如需根据现有图片做编辑或融合,请使用 图片编辑接口
⚠️ 不接受的参数本模型 不接受 sizenqualityaspect_ratio 等字段,传入可能触发参数校验错误。尺寸与比例请直接写进 prompt,例如:
  • 横版 16:9 电影画幅,黄昏时的海边老灯塔
  • 竖版 9:16 手机壁纸,赛博朋克城市雨夜
  • 1024×1024 方形 LOGO,极简猫咪线条
建议把尺寸描述放在 prompt 最前面,模型遵循度更高。

代码示例

Python

import requests

API_KEY = "sk-your-api-key"

response = requests.post(
    "https://api.apiyi.com/v1/images/generations",
    headers={"Authorization": f"Bearer {API_KEY}"},
    json={
        "model": "gpt-image-2-all",
        "prompt": "横版 16:9 日落海边的老灯塔,电影画幅,写实风格",
        "response_format": "url"
    },
    timeout=120
).json()

image_url = response["data"][0]["url"]
print(image_url)
b64_json 模式(直接拿到可渲染的 data URL)
import requests

response = requests.post(
    "https://api.apiyi.com/v1/images/generations",
    headers={"Authorization": "Bearer sk-your-api-key"},
    json={
        "model": "gpt-image-2-all",
        "prompt": "1024x1024 方形 LOGO,极简猫咪线条",
        "response_format": "b64_json"
    },
    timeout=120
).json()

# 注意:b64_json 字段已经包含 "data:image/png;base64," 前缀,无需再拼接
data_url = response["data"][0]["b64_json"]

cURL

curl -X POST "https://api.apiyi.com/v1/images/generations" \
  -H "Authorization: Bearer sk-your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-image-2-all",
    "prompt": "横版 16:9 电影画幅,黄昏时的海边老灯塔",
    "response_format": "url"
  }'

Node.js

const API_KEY = "sk-your-api-key";

const response = await fetch(
  "https://api.apiyi.com/v1/images/generations",
  {
    method: "POST",
    headers: {
      "Authorization": `Bearer ${API_KEY}`,
      "Content-Type": "application/json"
    },
    body: JSON.stringify({
      model: "gpt-image-2-all",
      prompt: "1024x1024 方形 LOGO,极简猫咪线条",
      response_format: "b64_json"
    })
  }
);

const data = await response.json();
// b64_json 已含前缀,可直接用作 <img src>
document.getElementById("result").src = data.data[0].b64_json;

浏览器 JavaScript(Fetch)

const resp = await fetch('https://api.apiyi.com/v1/images/generations', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer sk-your-api-key'
    },
    body: JSON.stringify({
        model: 'gpt-image-2-all',
        prompt: '竖版 9:16 手机壁纸,赛博朋克城市雨夜',
        response_format: 'b64_json'
    })
});
const { data } = await resp.json();
document.getElementById('result').src = data[0].b64_json;

参数说明速查

参数类型必填说明
modelstring固定填 gpt-image-2-all
promptstring提示词,尺寸/比例/风格请写在此处
response_formatstringurl(默认,返回 R2 CDN 链接)或 b64_json
详细的参数约束和可选值请查看右侧 Playground 中的字段说明,response_format 字段支持下拉选择。

响应格式

url 模式(默认,R2 CDN 全球加速):
{
    "data": [
        {
            "url": "https://r2cdn.copilotbase.com/r2cdn2/00aaa9fb-756c-4119-a4a0-4a44fc75152b.png"
        }
    ]
}
b64_json 模式(需显式 "response_format": "b64_json"):
{
    "data": [
        {
            "b64_json": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
        }
    ]
}
兼容性提示b64_json 字段本身已经包含 data:image/png;base64, 前缀,可直接赋给 HTML <img src> 或写入文件;无需再手动拼接 data:image/...;base64,。与部分旧版 OpenAI 兼容模型不同,请留意。

授权

Authorization
string
header
必填

在 API易控制台获取的 API Key

请求体

application/json
model
enum<string>
默认值:gpt-image-2-all
必填

模型名称,固定为 gpt-image-2-all

可用选项:
gpt-image-2-all
prompt
string
必填

提示词。尺寸/比例/风格请写在此处,例如:横版 16:9 电影画幅,黄昏时的海边老灯塔

示例:

"横版 16:9 电影画幅,黄昏时的海边老灯塔"

response_format
enum<string>
默认值:url

响应格式。url 返回 R2 CDN 链接(默认);b64_json 返回已含 data URL 前缀的 base64 字符串

可用选项:
url,
b64_json

响应

成功生成图片

data
object[]

生成结果数组(本模型单次返回 1 张)