1. POST /ai/generate-image 请求体参数总表

input、model、action 位于 JSON 顶层,其余生图控制项全部位于 parameters 对象内。未知字段或值为 null 的字段(除 skip_cfg_above_sigma: null 外)都会直接返回 400。机器可读规范:openapi-nai.json。

字段路径类型与取值范围
inputstring(必填非空,≤ 20000 UTF-8 字节):正面提示词,原样透传不擅自拼接后缀。
modelstring(必填):具体模型 ID(见模型表);action: "infill" 时追加 -inpainting。
action"generate" | "img2img" | "infill"
parameters.width / heightint(必填,64 的倍数):单边 64–3072,总像素 ≤ 3,145,728(标准免点画幅 ≤ 1,048,576,如 832×1216、1216×832、1024×1024)。
parameters.stepsint(默认 28,范围 1–50):采样步数(传 0 或省略默认 28),常用 23 或 28。
parameters.n_samplesint(默认 1,范围 1–4):单次生图张数(传 0 或省略默认 1)。
parameters.scalenumber(默认 5,范围 0–10):提示词相关性(CFG Scale)。
parameters.sampler枚举:k_euler_ancestral | k_euler | k_dpmpp_2s_ancestral | k_dpmpp_2m_sde | k_dpmpp_2m | k_dpmpp_sde
parameters.noise_schedule枚举:karras | exponential | polyexponential(V5 自动忽略)。
parameters.seedint(可选,0–4294967295):随机种子请直接省略该键;0 是合法固定种子,严禁传 null 或 -1。
parameters.negative_promptstring(或别名 uc,≤ 20000 UTF-8 字节):负面提示词,留空请传 ""。
parameters.cfg_rescalenumber(默认 0,范围 0–1)。
parameters.skip_cfg_above_sigmanumber | null(0–1000):Variety+ / 高噪声步延迟引导阈值。
parameters.tag_hint_transparent_backgroundboolean(可选):NAI V5 原生透明背景生成提示。

2. 图片输入字段速查(纯 Base64 编码)

任务目标actionparameters 内必填图片字段
图生图重绘img2imgimage + strength + noise
局部蒙版重绘infill(模型加 -inpainting)image + mask + strength(可选 mask_blur、inpaintImg2ImgStrength)
人物 / 画风 / 服装精准参考generatedirector_reference_* 五组等长数组(V5 最多 8 张,V4.5 最多 4 张)

image、mask、director_reference_images[] 均填写图片原始文件的纯 Base64 字符串(不含 data:image/png;base64, 前缀,不接受 URL)。支持 PNG / JPEG / WebP,单图 ≤ 24 MiB,整个 JSON 请求体 ≤ 40 MiB;底图 image 与同尺寸 mask ≤ 16,777,216 像素(16 MP),参考图 director_reference_images[] ≤ 24,000,000 像素(24 MP)。

3. 图生图(action: "img2img")

将顶层 action 设为 "img2img",并在 parameters 中加入以下字段(strength 必须满足 0 < strength <= 1):

json
{
  "image": "BASE64_OF_SOURCE_PNG",
  "strength": 0.65,
  "noise": 0
}

下载完整图生图 Python 脚本

4. 蒙版局部重绘(action: "infill")

顶层 action 设为 "infill",model 设为对应的 -inpainting 版本(如 nai-diffusion-5-full-inpainting 或 nai-diffusion-4-5-full-inpainting)。mask 为与原图同尺寸的黑白 PNG(白色区域重绘,黑色区域保留)。若传入 inpaintImg2ImgStrength,其值必须与 strength 相等。

json
{
  "image": "BASE64_OF_SOURCE_PNG",
  "mask": "BASE64_OF_MASK_PNG",
  "strength": 0.65,
  "noise": 0,
  "inpaintImg2ImgStrength": 0.65
}

下载完整蒙版重绘 Python 脚本

5. 精准参考 Precise Reference(支持 NAI V5 与 V4.5)

保持 action: "generate",向 parameters 传入以下五组长度完全相等的数组(每张参考图在五组数组中占同一下标)。请严格注意 V5 与 V4.5 的参数差异:

五组平行数组字段NAI V5(Full / Curated,最多 8 张)NAI V4.5(Full / Curated,最多 4 张)
director_reference_images每项为单张参考图的纯 Base64每项为单张参考图的纯 Base64
director_reference_descriptions[].caption.base_captioncharacter | style | character&style | costume | character_mainoutfit | character&style_anyoutfit(char_captions 必须为 [])character | style | character&style(char_captions 必须为 [])
director_reference_strength_values0 – 5(常用 1)0 – 1(常用 1)
director_reference_secondary_strength_values必须固定填 0(传非 0 会报 400)0 – 1(等于 1 − 工作台保真度 fidelity,例如保真度 0.7 填 0.3)
director_reference_information_extracted必须固定填 1必须固定填 1

NAI V5 双图参考(角色 + 服装)parameters 片段:

json
{
  "director_reference_images": [
    "BASE64_OF_CHAR_PNG",
    "BASE64_OF_OUTFIT_PNG"
  ],
  "director_reference_descriptions": [
    {
      "caption": {
        "base_caption": "character",
        "char_captions": []
      }
    },
    {
      "caption": {
        "base_caption": "costume",
        "char_captions": []
      }
    }
  ],
  "director_reference_strength_values": [
    1,
    1
  ],
  "director_reference_secondary_strength_values": [
    0,
    0
  ],
  "director_reference_information_extracted": [
    1,
    1
  ]
}

NAI V4.5 单图参考 parameters 片段:

json
{
  "director_reference_images": [
    "BASE64_OF_REFERENCE_PNG"
  ],
  "director_reference_descriptions": [
    {
      "caption": {
        "base_caption": "character",
        "char_captions": []
      }
    }
  ],
  "director_reference_strength_values": [
    1
  ],
  "director_reference_secondary_strength_values": [
    0.3
  ],
  "director_reference_information_extracted": [
    1
  ]
}

下载 V5 精准参考 Python 脚本 · 下载 V4.5 精准参考 Python 脚本

6. 独立多角色提示词(v4_prompt & v4_negative_prompt)

在 /ai/generate-image 中,多角色通过 parameters.v4_prompt 与 parameters.v4_negative_prompt 传入(V4/V4.5 最多 6 人,V5 最多 32 人)。base_caption 填写全局场景,char_captions 按相同人物顺序对齐正负面词;use_coords: false 为自动排布位置,true 则使用 centers(0.0 – 1.0 归一化坐标)。

json
{
  "input": "2girls, watercolor, sunlit cafe terrace, masterpiece",
  "model": "nai-diffusion-5-full",
  "action": "generate",
  "parameters": {
    "width": 1216,
    "height": 832,
    "steps": 23,
    "n_samples": 1,
    "scale": 5,
    "seed": 42,
    "sampler": "k_euler_ancestral",
    "negative_prompt": "lowres, blurry",
    "v4_prompt": {
      "caption": {
        "base_caption": "2girls, watercolor, sunlit cafe terrace, masterpiece",
        "char_captions": [
          {
            "char_caption": "girl, silver hair, blue dress, holding teacup, smile",
            "centers": [
              {
                "x": 0.3,
                "y": 0.5
              }
            ]
          },
          {
            "char_caption": "girl, short black hair, white shirt, reading book",
            "centers": [
              {
                "x": 0.7,
                "y": 0.5
              }
            ]
          }
        ]
      },
      "use_coords": true,
      "use_order": true
    },
    "v4_negative_prompt": {
      "caption": {
        "base_caption": "lowres, blurry",
        "char_captions": [
          {
            "char_caption": "extra fingers",
            "centers": [
              {
                "x": 0.3,
                "y": 0.5
              }
            ]
          },
          {
            "char_caption": "glasses",
            "centers": [
              {
                "x": 0.7,
                "y": 0.5
              }
            ]
          }
        ]
      },
      "use_coords": true,
      "use_order": true
    }
  }
}

下载完整双角色 Python 脚本

7. 附:/v1/nai/* 扩展接口字段对照(切勿混入原生接口)

下方字段仅用于 /v1/nai/estimate 与 /v1/nai/generations(图片先通过 POST /v1/nai/assets 上传获取 asset_id),切勿写进 /ai/generate-image:

用途/v1/nai/* 顶层扩展字段
模型指定model: "nai-v4.5" + model_variant: "nai-diffusion-5-full"
提示词 / CFG / 张数prompt / guidance / count
底图与蒙版source_image_asset_id / inpaint_mask_asset_id / source_strength / source_noise
精准参考(V5 最多 8 张 / V4.5 最多 4 张)references: [{ asset_id, role: "precise", description: "character", strength, fidelity, information_extracted: 1 }]
Vibe 风格参考(仅 V4/V4.5/V3)references: [{ asset_id, role: "style", strength: 0.6, information_extracted: 0.7 }]
多角色提示词characters: [{ prompt, negative_prompt, x, y }] + auto_character_positions

查看 /v1/nai/* 完整上传 → 报价 → 生成 → SSE 示例

8. 常见报错与排查表

报错信息 / 状态码原因与修复方法
401 Unauthorized检查 Authorization: Bearer sk-... 是否填写了有效的本站 API Key(鉴权网关返回 {"error": ...})。
402 Payment Required账号余额或 NAI 点数不足,请充值或切换免费试用支持规格。
403 Forbidden检查 API Key 模型白名单是否包含 nai-v4.5,以及账号权限状态。
unsupported or invalid parameters.<key>传入了未知字段、null 值(如 seed: null)或已开启的 autoSmea: true。请移除未知字段并省略空值。
director_reference_secondary_strength_values (V5 requires 0)调用 NAI V5 精准参考时,secondary_strength_values 每项必须填 0。
director_reference_information_extracted (each entry must be 1)精准参考的 information_extracted 每项必须固定填 1。
action does not match image/mask parametersgenerate 不能带 image/mask;img2img 必带 image;infill 必带 image + mask 且模型加 -inpainting。
409 Conflict / 504 Gateway Timeout409 为幂等键参数冲突或并发占用;504 请查看响应头 X-Retry-Same-Key(true 复用原键取图,false 为终态超时已退款,重交须换新 UUID)。