API 参考

图像

使用图片生成与图像编辑接口,创建图像或修改已有图片。

POST https://api.aigocode.dev/v1/images/generations

选择支持图片模型的可外接分组,使用 Authorization: Bearer YOUR_API_KEY 鉴权。下面以 gpt-image-2.5-flare 为例,生成一张 PNG 图片。

请求参数

字段类型必填说明
modelstring是图片模型 ID
promptstring是图片描述或生成指令
sizestring否请求的图片尺寸,示例为 1024x1024
ninteger否生成数量,示例为 1
qualitystring否模型支持的质量档位,示例为 low
output_formatstring否输出编码格式,示例为 png

当前示例的 GPT 图片模型通过 b64_json 返回图片数据,使用 output_format 指定图片编码。

请求示例

export AIGOCODE_API_KEY="填写包含图片模型且支持外接调用的分组 API Key"

curl "https://api.aigocode.dev/v1/images/generations" \
  -H "Authorization: Bearer $AIGOCODE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "gpt-image-2.5-flare",
  "prompt": "A simple blue circle on a white background, minimal icon.",
  "size": "1024x1024",
  "n": 1,
  "quality": "low",
  "output_format": "png"
}'
$env:AIGOCODE_API_KEY = "填写包含图片模型且支持外接调用的分组 API Key"

$body = @{
  model = "gpt-image-2.5-flare"
  prompt = "A simple blue circle on a white background, minimal icon."
  size = "1024x1024"
  n = 1
  quality = "low"
  output_format = "png"
} | ConvertTo-Json -Depth 6

$response = Invoke-RestMethod `
  -Method Post `
  -Uri "https://api.aigocode.dev/v1/images/generations" `
  -Headers @{ Authorization = "Bearer $env:AIGOCODE_API_KEY" } `
  -ContentType "application/json; charset=utf-8" `
  -Body ([System.Text.Encoding]::UTF8.GetBytes($body))

$response | ConvertTo-Json -Depth 20
set "AIGOCODE_API_KEY=填写包含图片模型且支持外接调用的分组 API Key"

curl.exe "https://api.aigocode.dev/v1/images/generations" ^
  -H "Authorization: Bearer %AIGOCODE_API_KEY%" ^
  -H "Content-Type: application/json" ^
  -d "{\"model\":\"gpt-image-2.5-flare\",\"prompt\":\"A simple blue circle on a white background, minimal icon.\",\"size\":\"1024x1024\",\"n\":1,\"quality\":\"low\",\"output_format\":\"png\"}"

响应

{
  "data": [
    {
      "b64_json": "BASE64_IMAGE_DATA"
    }
  ],
  "output_format": "png"
}

BASE64_IMAGE_DATA 代表图片的 Base64 数据,解码后得到实际图片文件。输出尺寸可从响应和图片文件中查看。

保存图片

Windows PowerShell 中,接着上面的请求执行:

[System.IO.File]::WriteAllBytes(
  (Join-Path (Get-Location) "aigocode-image.png"),
  [System.Convert]::FromBase64String($response.data[0].b64_json)
)

macOS / Linux 和 Windows Cmd 可以在 curl 命令中增加 -o image-response.json,将响应保存到文件,再用下面的 Node.js 脚本解码:

import { readFileSync, writeFileSync } from "node:fs";

const raw = readFileSync("image-response.json", "utf8").replace(/^\uFEFF/, "");
const result = JSON.parse(raw);
const base64 = result.data?.[0]?.b64_json;
if (!base64) throw new Error("响应中没有图片数据,请检查错误信息。");

writeFileSync("aigocode-image.png", Buffer.from(base64, "base64"));
console.log("已保存:aigocode-image.png");

将代码保存为 save-image.mjs,运行 node save-image.mjs 即可。

图像编辑

上传参考图并描述需要修改的内容,模型会基于原图生成编辑结果。gpt-image-2.5-flare 和 gpt-image-2.5-sunburst 均支持下面的单张参考图编辑方式。

POST https://api.aigocode.dev/v1/images/edits

请求使用 multipart/form-data,通过 image 字段上传原图,鉴权仍为 Authorization: Bearer YOUR_API_KEY。

编辑参数

字段说明
model图片模型 ID,可使用 gpt-image-2.5-flare 或 gpt-image-2.5-sunburst
image上传的参考图片文件,本例为 PNG
prompt编辑指令,说明要修改的内容及需要保留的部分
n返回图片数量,本例为 1
size请求的输出尺寸,本例为 1024x1024
quality图片质量,本例为 low
output_format输出编码,本例为 png

编辑示例(macOS / Linux)

将自己的 PNG 图片保存为 input.png,并按图片内容填写编辑指令。下面以将蓝色圆形改为红色为例:

export AIGOCODE_API_KEY="填写包含图片模型且支持外接调用的分组 API Key"

curl "https://api.aigocode.dev/v1/images/edits" \
  -H "Authorization: Bearer $AIGOCODE_API_KEY" \
  -F "model=gpt-image-2.5-flare" \
  -F "image=@input.png;type=image/png" \
  --form-string "prompt=Change the blue circle in the input image to red. Keep the rest of the image unchanged." \
  -F "n=1" \
  -F "size=1024x1024" \
  -F "quality=low" \
  -F "output_format=png" \
  -o image-response.json

使用 Sunburst 时,将 model 改为 gpt-image-2.5-sunburst。

curl 会自动设置表单的 Content-Type 和 boundary。编辑结果同样从 data[].b64_json 读取,保存方式沿用上面的「保存图片」。

用量与计费

图片按所选分组和模型的图片价格规则扣减额度,价格可在模型广场查看,实际消耗可在「使用统计」中核对。套餐额度与灵活额度的扣减顺序与其他模型调用一致。

相关入口

本页目录