AIMasker API

图片 API 对接文档

通过 OpenAI Images 兼容接口调用 gpt-image-2,支持文生图、单图编辑和最多 16 张参考图编辑。推荐使用异步 URL 模式,生成结果从 imgs.aimasker.com 下载;同步接口继续强制返回 Base64。quality 可选 low、medium、high,不填写时默认使用 medium;尺寸为可选项,默认 4K。

Base URL:https://aimasker.com/v1 模型:gpt-image-2 价格:$0.05 / 张 打开在线作图 → 下载调用 Skill

快速开始

在控制台创建 API Key,然后把下面的 sk-your-key 替换为自己的 Key。

curl -i https://aimasker.com/v1/images/generations/async \
  -H "Authorization: Bearer sk-your-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-image-2",
    "prompt": "一只戴宇航头盔的橘猫,电影级灯光",
    "size": "2880x2880",
    "quality": "high",
    "response_format": "b64_json",
    "output_format": "png",
    "n": 1
  }'

提交成功返回 202 Acceptedtask_idpoll_url。使用同一 API Key 轮询:

curl https://aimasker.com/v1/images/tasks/imgtask_xxx \
  -H "Authorization: Bearer sk-your-key"
推荐异步 URL:任务完成后读取 result.data[0].url,地址为 https://imgs.aimasker.com/images/...。图片临时保留约 1 天(24 小时),请及时下载。在线作图和明确要求同步的客户端继续使用 Base64,两种模式不会同时提交。

AIMasker 图片接口 Skill

适用于 Codex 及支持 SKILL.md / Agent Skills 目录规范的工具。默认使用异步 URL 并立即下载落盘;用户明确要求时可切换同步 Base64。内置 4K、high、最多 16 张参考图编辑,以及 Request ID/task_id 请求台账。

aimasker-image-api.zip

源码包 · 25,537 字节 · 不包含 API Key

Codex 一键安装(Windows PowerShell)

$skillZip = Join-Path $env:TEMP "aimasker-image-api.zip"
Invoke-WebRequest "https://api.aimasker.com/image-api-docs/downloads/aimasker-image-api.zip" -OutFile $skillZip
New-Item -ItemType Directory -Force -Path "$env:USERPROFILE\.codex\skills" | Out-Null
Expand-Archive $skillZip -DestinationPath "$env:USERPROFILE\.codex\skills" -Force
python -m pip install -r "$env:USERPROFILE\.codex\skills\aimasker-image-api\requirements.txt"

macOS / Linux

mkdir -p ~/.codex/skills
curl -L "https://api.aimasker.com/image-api-docs/downloads/aimasker-image-api.zip" -o /tmp/aimasker-image-api.zip
unzip -o /tmp/aimasker-image-api.zip -d ~/.codex/skills
python3 -m pip install -r ~/.codex/skills/aimasker-image-api/requirements.txt

配置与调用

设置环境变量 AIMASKER_API_KEY 后重新打开 Codex 或对应工具。Windows 可执行:

[System.Environment]::SetEnvironmentVariable("AIMASKER_API_KEY", "你的 API Key", "User")

macOS(默认 zsh)可持久化写入:

echo 'export AIMASKER_API_KEY="你的 API Key"' >> ~/.zshrc
source ~/.zshrc

Linux(常见 bash)可持久化写入:

echo 'export AIMASKER_API_KEY="你的 API Key"' >> ~/.bashrc
source ~/.bashrc

调用示例:

$aimasker-image-api 生成一张 16:9、4K、high 的电影感城市夜景
其他工具:把解压后的完整 aimasker-image-api 文件夹放入该工具配置的 Skills 目录,确保 SKILL.md 位于文件夹根目录,并允许执行 Python 脚本。

SHA256:C419789F02B07AC50CA5995D145FA58FB4A2983FFD8A8402A836EBBFC139166B

认证方式

所有请求都要在 HTTP Header 中携带 Bearer Token:

Authorization: Bearer sk-your-key

请勿把 API Key 写入前端网页、公开仓库、日志或截图。服务端调用时建议通过环境变量读取。

异步 URL(推荐)

异步接口不会要求客户端维持一个长时间生成连接,适合 4K 图片、自动化工具和高并发调用。提交生成 POST 只执行一次,后续只轮询任务 GET。

POST/v1/images/generations/async提交文生图任务
POST/v1/images/edits/async提交图片编辑任务
GET/v1/images/tasks/{task_id}使用提交任务的同一 API Key 查询

提交响应

{
  "id": "imgtask_xxx",
  "task_id": "imgtask_xxx",
  "status": "processing",
  "poll_url": "/v1/images/tasks/imgtask_xxx"
}

完成响应

{
  "task_id": "imgtask_xxx",
  "status": "completed",
  "image_url": "https://imgs.aimasker.com/images/xxx.png",
  "result": {
    "created": 1788172800,
    "data": [{"url": "https://imgs.aimasker.com/images/xxx.png"}]
  }
}
保存期限与隐私:异步结果会临时转存到 AIMasker 对象存储,约 1 天(24 小时)后自动删除;任务状态及结果约 24 小时后失效。请在任务完成后立即下载。同步接口不转存图片。

文生图

POST/v1/images/generationsapplication/json

Python 示例

import base64
import os
import requests

response = requests.post(
    "https://aimasker.com/v1/images/generations",
    headers={"Authorization": f"Bearer {os.environ['AIMASKER_API_KEY']}"},
    json={
        "model": "gpt-image-2",
        "prompt": "中国古风山水,云海中的宫殿,横向构图",
        "size": "3840x2160",
        "quality": "high",
        "response_format": "b64_json",
        "output_format": "png",
        "n": 1,
    },
    timeout=600,
)
response.raise_for_status()
image_bytes = base64.b64decode(response.json()["data"][0]["b64_json"])
with open("generated.png", "wb") as output:
    output.write(image_bytes)

图片编辑

POST/v1/images/editsmultipart/form-data

上传一张参考图时使用 image;上传多张参考图时重复提交 image[],最多 16 张。可选上传透明蒙版 mask

curl https://aimasker.com/v1/images/edits \
  -H "Authorization: Bearer sk-your-key" \
  -F "model=gpt-image-2" \
  -F "prompt=保留人物与构图,把背景改成雨夜霓虹街道" \
  -F "image=@reference.png" \
  -F "size=2160x3840" \
  -F "quality=high" \
  -F "response_format=b64_json" \
  -F "output_format=png" \
  -F "n=1"

多参考图

curl https://aimasker.com/v1/images/edits \
  -H "Authorization: Bearer sk-your-key" \
  -F "model=gpt-image-2" \
  -F "prompt=使用第一张图的人物和第二张图的服装,生成完整角色图" \
  -F "image[]=@character.png" \
  -F "image[]=@costume.png" \
  -F "size=2448x3264" \
  -F "quality=high" \
  -F "response_format=b64_json"

参数说明

参数必填说明
model固定使用 gpt-image-2
prompt生成或编辑指令,不能为空
image / image[]编辑必填参考图片文件;多图时重复提交 image[],最多 16 张
mask编辑区域蒙版文件
size可选目标像素尺寸;可从下方尺寸表选择,默认 4K
quality可选可选 lowmediumhigh;不填写时默认使用 medium,如需高质量必须显式传入 high
response_format可选同步接口强制使用 b64_json;异步任务完成后统一返回对象存储 url
output_formatpngjpegwebp
n返回图片数量,默认 1;按实际返回张数计费

图片尺寸表

size 为可选项,支持 1K、2K、4K,默认 4K。比例文字写进提示词不能代替 size 参数,且 quality=high 不能代替尺寸参数。

比例1K2K4K(推荐默认)
1:11024×10242048×20482880×2880
5:41120×8962240×17923200×2560
4:31152×8642304×17283264×2448
3:21248×8322496×16643504×2336
16:91280×7202560×14403840×2160
21:91456×6243024×12963696×1584
4:5896×11201792×22402560×3200
3:4864×11521728×23042448×3264
2:3832×12481664×24962336×3504
9:16720×12801440×25602160×3840

图片获取与保存

推荐方式:异步任务完成后读取 result.data[].url 并及时下载。URL 使用 imgs.aimasker.com,不会暴露图片来源,约 1 天(24 小时)后失效。

异步 URL 下载

task = requests.get(
    f"https://aimasker.com/v1/images/tasks/{task_id}",
    headers={"Authorization": f"Bearer {os.environ['AIMASKER_API_KEY']}"},
).json()

if task["status"] == "completed":
    image_url = task["result"]["data"][0]["url"]
    image_bytes = requests.get(image_url, timeout=120).content
    with open("generated.png", "wb") as output:
        output.write(image_bytes)

同步 Base64

在线作图和明确要求同步的客户端调用原生成/编辑接口,读取 data[].b64_json,解码为图片二进制后保存。不要为了同一张图同时调用同步和异步接口。

1. 提交请求调用生成或编辑接口,等待 JSON 响应完成。
2. 读取图片读取 data[0].b64_json;多张图片则遍历 data[]
3. Base64 解码将字符串解码成原始图片字节,不要把 Base64 文本直接写入图片文件。
4. 保存或上传根据 output_format 使用正确扩展名保存,或上传到自己的对象存储。

响应示例

{
  "created": 1788172800,
  "data": [
    {"b64_json": "iVBORw0KGgoAAAANSUhEUgAA..."}
  ]
}

Python:解码并保存全部图片

import base64

body = response.json()
for index, item in enumerate(body["data"], start=1):
    encoded = item["b64_json"]
    # 兼容可能带有 data:image/png;base64, 前缀的返回值
    if "," in encoded and encoded.startswith("data:"):
        encoded = encoded.split(",", 1)[1]
    with open(f"image_{index}.png", "wb") as output:
        output.write(base64.b64decode(encoded))

Node.js:解码并保存

import { writeFile } from "node:fs/promises";

const body = await response.json();
for (const [index, item] of body.data.entries()) {
  const encoded = item.b64_json.includes(",")
    ? item.b64_json.split(",", 2)[1]
    : item.b64_json;
  await writeFile(`image_${index + 1}.png`, Buffer.from(encoded, "base64"));
}

浏览器:直接预览 PNG

const encoded = body.data[0].b64_json;
document.querySelector("#result-image").src =
  `data:image/png;base64,${encoded}`;
注意:Base64 是无损编码,不会降低画质或分辨率,但响应体通常比原始二进制大约增加 33%。普通 API/Skill 推荐异步 URL;在线作图默认同步 Base64。若使用 JPEG 或 WebP,画质取决于图片格式及编码设置,而不是 Base64。

错误与重试

401 UnauthorizedAPI Key 缺失、无效或已过期。
402 / 403余额、分组权限或模型权限不足。
408 Timeout服务繁忙或处理超时;先保存 Request ID 再排查。
429 Too Many Requests并发或速率超限,请降低调用频率。
5xx Service Error服务端异常,请记录完整错误和响应头中的请求 ID。
响应中断4K Base64 响应体较大;保留 Request ID,先排查记录,不要立即重提付费请求。
异步轮询超时任务可能仍在执行;保留 task_id 并继续 GET 查询,不要重新提交生成 POST。
重要:图片生成与编辑的 POST 请求不要自动重试。连接中断时请求可能已经成功并产生费用;应先根据 x-request-idrequest-idopenai-request-id 排查。

计费说明

$0.05 / 张

按成功返回的图片数量计费。若 n=2 并成功返回两张图片,则费用为 $0.10。调用失败是否计费取决于请求是否已经进入处理流程,请保留请求 ID 便于核查。