Skip to content

图片生成 API 文档

本页按真实调用方式把图片生成分成两类:image2 使用同步 Images API,响应里直接返回图片数据;nano_banana*gpt-image-2* 使用异步任务,先提交到 /v1/videos,再用任务 ID 查询结果。

生图 / 生视频

本页是图片生成文档;视频生成请看 视频生成 API 文档。如果你想对照 Apifox 的在线接口目录,可以打开 goswitcher api文档

基础变量

所有示例都使用 BASE_URL 表示站点根地址,不包含 /v1。切换站点时只改这一行即可。

bash
BASE_URL="https://goswitcher.com"
API_KEY="YOUR_API_KEY"

如何选择接口

场景模式模型接口返回方式
image2 文生图 / 带参考图生成同步image2POST /v1/images/generations直接返回 data[0].b64_json
image2 图片编辑同步image2POST /v1/images/edits直接返回 data[0].b64_json
nano_banana 文生图 / 图生图异步nano_banana_2nano_banana_pro-1Knano_banana_pro-2Knano_banana_pro-4KPOST /v1/videos返回任务 ID,最后用任务查询
gpt-image-2 文生图 / 图生图异步gpt-image-2gpt-image-2-2Kgpt-image-2-4KPOST /v1/videos返回任务 ID,最后用任务查询
异步任务查询公共查询任务 IDGET /v1/videos/{task_id}返回任务状态和结果地址

核心区别

异步接口的提交响应只是任务状态,不代表图片已经生成完成。只有任务查询返回 completed 时,才从 urlurlsdata[].url 读取图片。image2 同步接口不需要任务查询,响应里的 b64_json 就是图片数据。

通用鉴权

Header必填说明
AuthorizationBearer YOUR_API_KEY
Content-TypeJSON 请求使用 application/json;文件上传使用 multipart/form-data

地址变量

BASE_URL 是站点根地址,例如 https://goswitcher.com。不要把它写成 https://goswitcher.com/v1 后再拼 /v1/videos,否则会变成重复路径。

接口详情

image2 同步生成

image2 使用 OpenAI 兼容 Images API,同步返回结果,不需要任务查询。

项目文生图 / 带参考图生成图片编辑
MethodPOSTPOST
Path/v1/images/generations/v1/images/edits
JSON 参考图image 支持字符串或字符串数组image 支持字符串或字符串数组
文件上传不推荐推荐使用 multipart/form-data
返回data[0].b64_jsondata[0].b64_json

请求参数

参数类型必填说明
modelstring固定为 image2
promptstring图片描述或编辑要求
sizestring1024x10241024x17921792x1024
imagestring 或 string[]参考图 URL 或完整 Data URL;图片编辑时通常必填
ninteger同步接口会按上游能力返回,不能保证请求多张就一定返回多张

文生图 / 带参考图生成

bash
curl -X POST "$BASE_URL/v1/images/generations" \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "image2",
    "prompt": "参考两张图,生成一张广告主图",
    "size": "1024x1792",
    "image": [
      "https://example.com/reference-1.jpg",
      "https://example.com/reference-2.png"
    ]
  }'

图片编辑 / 文件上传

bash
curl -X POST "$BASE_URL/v1/images/edits" \
  -H "Authorization: Bearer $API_KEY" \
  --form 'model="image2"' \
  --form 'prompt="保留主体,把背景改成浅灰色摄影棚风格"' \
  --form 'size="1024x1024"' \
  --form 'image=@"/path/to/example.jpg"'
image2 同步响应示例
json
{
  "created": 1782108238,
  "data": [
    {
      "b64_json": "iVBORw0KGgoAAAANSU..."
    }
  ],
  "usage": {
    "input_tokens": 4,
    "output_tokens": 1105,
    "total_tokens": 1109
  }
}

任务查询

项目内容
MethodGET
Path/v1/videos/{task_id}
用途查询 Banana、GPT Image、视频等异步任务

路径参数

参数类型必填说明
task_idstring提交接口返回的 id

状态字段

status客户端处理
queued任务排队中,继续轮询
processing任务处理中,继续轮询
in_progress任务处理中,继续轮询
completed任务完成,从 urlurlsdata[].url 读取结果
failed任务失败,读取 errorerror.message

建议每 3 到 5 秒轮询一次,避免过于频繁。

调用示例

bash
TASK_ID="task_xxxxxxxxxxxxx"

curl -X GET "$BASE_URL/v1/videos/$TASK_ID" \
  -H "Authorization: Bearer $API_KEY"
任务响应示例
json
{
  "id": "task_xxxxxxxxxxxxx",
  "object": "image",
  "model": "gpt-image-2",
  "status": "queued",
  "progress": 0,
  "created_at": 1709876543
}
json
{
  "id": "task_xxxxxxxxxxxxx",
  "object": "image",
  "model": "nano_banana_2",
  "status": "completed",
  "progress": 100,
  "created_at": 1709876543,
  "completed_at": 1709876580,
  "url": "https://example.com/images/result.jpg"
}
json
{
  "id": "task_batch_xxxxxxxxxxxxx",
  "object": "image_batch",
  "model": "gpt-image-2",
  "status": "completed",
  "progress": 100,
  "urls": ["https://example.com/images/result-1.jpg"],
  "data": [
    {
      "index": 0,
      "id": "task_a",
      "status": "completed",
      "url": "https://example.com/images/result-1.jpg",
      "image_url": "https://example.com/images/result-1.jpg",
      "error": null
    }
  ]
}
json
{
  "id": "task_xxxxxxxxxxxxx",
  "object": "image",
  "model": "gpt-image-2",
  "status": "failed",
  "progress": 100,
  "created_at": 1709876543,
  "completed_at": 1709876580,
  "error": {
    "message": "上游任务失败原因",
    "code": "upstream_error"
  }
}

常见注意点

  • BASE_URL 是站点根地址,例如 https://goswitcher.com,不要写成 https://goswitcher.com/v1 后再拼 /v1/videos
  • 异步接口只要没有 completedfailed,就继续轮询任务查询接口。
  • 参考图 URL 必须是服务端能访问的图片直链;内网地址、本地文件路径通常不可用。
  • image2 同步接口返回图片数据;nano_banana*gpt-image-2* 异步接口先返回任务状态。
  • n > 1 是 switcher 本地批量任务,查询批量任务时从 urlsdata[].url 读取结果。
  • 生成结果地址通常有有效期,任务完成后建议尽快下载保存。

GoSwitcher documentation site.