跳转到主要内容
POST
/
v1
/
images
/
edits
curl 'https://www.bettertoken.ai/v1/images/edits' \
  -H 'Authorization: Bearer sk-***REDACTED***' \
  -F 'model=gpt-image-2' \
  -F 'prompt=参考这张图片,生成一张更精致的方形产品主视觉,保持主体风格一致。' \
  -F 'n=1' \
  -F 'size=1024x1024' \
  -F 'response_format=b64_json' \
  -F 'output_format=png' \
  -F 'image=@reference.png'
{
  "created": 1710000000,
  "data": [
    {
      "b64_json": "iVBORw0KGgoAAAANSUhEUgAA...(truncated)"
    }
  ]
}

Documentation Index

Fetch the complete documentation index at: https://docs.bettertoken.ai/llms.txt

Use this file to discover all available pages before exploring further.

POST /v1/images/edits 图生图接口使用 multipart/form-data 请求体。你需要上传一张或多张参考图片,并通过表单字段提交提示词和参数。
使用 https://www.bettertoken.ai/v1 作为 Base URL。调用时通过 Authorization: Bearer YOUR_API_KEY 传入 BetterToken API Key。
你可以在页面右侧的 Playground 填写表单字段并上传参考图片,直接在线调试。请求会发往 https://www.bettertoken.ai/v1/images/edits
图生图不能使用普通 JSON body。请用 multipart/form-data 提交文本字段和文件字段。

推荐请求值

所有图生图请求建议显式传入这些字段:
model=gpt-image-2
n=1
response_format=b64_json
output_format=png
单参考图使用字段名 image。多参考图使用可重复的字段名 image[]

单参考图

提交这些表单字段:
表单字段示例值
modelgpt-image-2
prompt参考这张图片,生成一张更精致的方形产品主视觉,保持主体风格一致。
n1
size1024x1024
response_formatb64_json
output_formatpng
image选择本地文件 reference.png
curl 'https://www.bettertoken.ai/v1/images/edits' \
  -H 'Authorization: Bearer sk-***REDACTED***' \
  -F 'model=gpt-image-2' \
  -F 'prompt=参考这张图片,生成一张更精致的方形产品主视觉,保持主体风格一致。' \
  -F 'n=1' \
  -F 'size=1024x1024' \
  -F 'response_format=b64_json' \
  -F 'output_format=png' \
  -F 'image=@reference.png'

多参考图

提交这些表单字段:
表单字段示例值
modelgpt-image-2
prompt综合这些参考图,生成一张统一风格的宣传海报。
n1
size1024x1024
response_formatb64_json
output_formatpng
image[]选择本地文件 reference-1.png
image[]选择本地文件 reference-2.jpg
image[]选择本地文件 reference-3.png
curl 'https://www.bettertoken.ai/v1/images/edits' \
  -H 'Authorization: Bearer sk-***REDACTED***' \
  -F 'model=gpt-image-2' \
  -F 'prompt=综合这些参考图,生成一张统一风格的宣传海报。' \
  -F 'n=1' \
  -F 'size=1024x1024' \
  -F 'response_format=b64_json' \
  -F 'output_format=png' \
  -F 'image[]=@reference-1.png' \
  -F 'image[]=@reference-2.jpg' \
  -F 'image[]=@reference-3.png'

Python 示例

import requests

url = "https://www.bettertoken.ai/v1/images/edits"
headers = {
    "Authorization": "Bearer sk-***REDACTED***",
}

data = {
    "model": "gpt-image-2",
    "prompt": "参考这张图片,生成一张更精致的方形产品主视觉,保持主体风格一致。",
    "n": "1",
    "size": "1024x1024",
    "response_format": "b64_json",
    "output_format": "png",
}

with open("reference.png", "rb") as image_file:
    files = {
        "image": ("reference.png", image_file, "image/png"),
    }
    response = requests.post(url, headers=headers, data=data, files=files, timeout=300)

response.raise_for_status()
result = response.json()
b64_json = result["data"][0]["b64_json"]

推荐尺寸

size比例适用场景
auto自动自动选择尺寸
1024x10241:1方图,通用头像、封面、素材图
1536x10243:2横图,海报、banner、场景图
1024x15362:3竖图,移动端封面、人物海报
2048x20481:1高清方图
2048x115216:9高清横图
3840x216016:94K 横图
2160x38409:164K 竖图
size 表示期望的图片比例和尺寸档位。实际返回图片像素可能由服务端映射或调整,客户端应以解码后的真实图片尺寸为准。

保存返回图片

成功响应为 OpenAI 兼容图片响应结构:
{
  "created": 1710000000,
  "data": [
    {
      "b64_json": "iVBORw0KGgoAAAANSUhEUgAA...(truncated)"
    }
  ]
}
客户端应读取 data[0].b64_json 并将其作为 base64 图片内容保存。响应中可能出现额外字段,例如 revised_prompt,客户端应允许这些字段存在。 建议所有请求都显式指定 output_format: "png"。这样保存为 .png 即可,不需要再解析文件头判断格式。

响应流程

图生图接口是同步响应模式。客户端提交 POST /images/edits 后,应保持当前 HTTP 请求连接并等待服务端返回。生成成功时,图片内容会直接出现在同一个响应的 data[0].b64_json 字段中。 图生图不会返回 task_id,也没有单独的状态查询接口或结果下载接口。不需要轮询任务状态,也不能通过额外的 /images/... URL 再获取结果。

超时和重试

  • HTTP 客户端超时建议设置为数分钟级别。
  • 对传输层异常、4084094254295xx 可以重试。
  • 400401、参数缺失、格式错误不要自动重试。
  • 重试时使用指数退避,例如等待 3s8s15s
  • 如果业务不能接受重复图片,应用层应记录自己的请求 ID,避免用户重复提交。

接入检查清单

  • Base URL 是 https://www.bettertoken.ai/v1
  • Header 包含 Authorization: Bearer sk-...
  • 请求使用 multipart/form-data
  • 模型固定为 gpt-image-2
  • 单参考图使用 image,多参考图使用重复的 image[]
  • response_format 固定为 b64_json
  • output_format 固定为 png
  • n 固定为 1
  • size 使用推荐尺寸之一。
  • 客户端能处理数分钟级别的生成耗时。
  • 日志和报错截图中不会泄露 API Key 或完整 base64 图片内容。

相关文档

授权

Authorization
string
header
必填

Use your BetterToken API Key as a bearer token. Do not expose API keys in frontend browser code, screenshots, logs, tickets, or Git repositories.

请求体

multipart/form-data
model
enum<string>
默认值:gpt-image-2
必填

固定使用 gpt-image-2。

可用选项:
gpt-image-2
示例:

"gpt-image-2"

prompt
string
必填

图片编辑或参考图生成说明。

示例:

"参考这张图片,生成一张更精致的方形产品主视觉,保持主体风格一致。"

image
file

单张参考图字段名。单参考图时使用 image。

image[]
file[]

多张参考图字段名。多参考图时重复提交 image[]。

n
默认值:1

推荐固定为 1。多张图片建议发起多次独立请求。

示例:

"1"

size
enum<string>
默认值:1024x1024

图片尺寸和比例档位。auto 为自动;1024x1024 和 2048x2048 为 1:1;1536x1024 为 3:2;1024x1536 为 2:3;2048x1152 和 3840x2160 为 16:9;2160x3840 为 9:16。实际返回像素可能由服务端映射或调整,客户端应以解码后的真实图片尺寸为准。

可用选项:
auto,
1024x1024,
1536x1024,
1024x1536,
2048x2048,
2048x1152,
3840x2160,
2160x3840
示例:

"1024x1024"

response_format
enum<string>
默认值:b64_json

推荐固定为 b64_json。

可用选项:
b64_json
示例:

"b64_json"

output_format
enum<string>
默认值:png

推荐固定为 png。不要依赖 jpeg 或 webp 直接返回对应格式。

可用选项:
png
示例:

"png"

响应

Image generation result.

OpenAI-compatible image response. Clients should read data[0].b64_json and allow additional fields such as revised_prompt.

created
integer
示例:

1710000000

data
object[]