Midjourney

用 Midjourney 出图。接口兼容 midjourney-proxy 协议,所有操作都是异步的,固定三步:提交任务 → 查询结果 → 点按钮做后续操作(放大、变换、重绘等)。

项 值
Base URL https://www.nextai-code.com/mj(不带 /v1)
鉴权 Authorization: Bearer YOUR_API_KEY
Content-Type application/json

已对接 midjourney-proxy 的客户端,把地址换成 https://www.nextai-code.com、密钥换成 NextAI Code 的 API Key 即可;也可以用 mj-api-secret: YOUR_API_KEY 头鉴权(仅在不带 Authorization 头时生效)。

快速示例

1. 提交绘图

BASH
curl https://www.nextai-code.com/mj/submit/imagine \
  -H "Authorization: Bearer $NEXTAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"prompt": "a cat sitting by the window, watercolor --ar 16:9"}'
JSON
{"code": 1, "description": "提交成功", "result": "1712205491372", "properties": {}}

code 为 1 表示提交成功,result 是任务 ID。

2. 查询结果

BASH
curl https://www.nextai-code.com/mj/task/1712205491372/fetch \
  -H "Authorization: Bearer $NEXTAI_API_KEY"

status 变为 SUCCESS 后,imageUrl 是图片地址,buttons 列出可以继续点的按钮。

3. 点按钮:放大第 1 张

BASH
curl https://www.nextai-code.com/mj/submit/action \
  -H "Authorization: Bearer $NEXTAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "taskId": "1712205491372",
    "customId": "MJ::JOB::upsample::1::f6a3..."
  }'

customId 从第 2 步返回的 buttons[].customId 原样复制。返回新的任务 ID,回到第 2 步查询。

Python

PYTHON
import os, time, requests

BASE = "https://www.nextai-code.com/mj"
H = {"Authorization": f"Bearer {os.environ['NEXTAI_API_KEY']}"}

def submit(path, body):
    r = requests.post(f"{BASE}{path}", json=body, headers=H, timeout=90)
    data = r.json()
    if r.status_code != 200 or data.get("code") != 1:
        raise RuntimeError(f"提交失败: HTTP {r.status_code} {data}")
    return data["result"]

def wait(task_id, timeout=600):
    deadline = time.time() + timeout
    while time.time() < deadline:
        r = requests.get(f"{BASE}/task/{task_id}/fetch", headers=H, timeout=30)
        r.raise_for_status()
        task = r.json()
        if task.get("status") in ("SUCCESS", "FAILURE"):
            return task
        time.sleep(10)
    raise TimeoutError(f"等待超时,稍后用 task_id={task_id} 继续查询")

# 1. 提交 + 2. 查询
task = wait(submit("/submit/imagine", {"prompt": "a cat sitting by the window, watercolor"}))
if task["status"] != "SUCCESS":
    raise SystemExit(task.get("failReason"))
print(task["imageUrl"])

# 3. 点 U1 按钮放大第 1 张
u1 = next(b for b in task["buttons"] if b["label"] == "U1")
upscaled = wait(submit("/submit/action", {"taskId": task["id"], "customId": u1["customId"]}))
print(upscaled["imageUrl"])

# 下载图片:平台地址带 API Key,其他域名的地址直接下载
url = upscaled["imageUrl"]
img = requests.get(url, headers=H if url.startswith("https://www.nextai-code.com/") else None, timeout=60)
img.raise_for_status()
with open("mj.png", "wb") as f:
    f.write(img.content)

第 1 步:提交任务

所有提交接口都是 POST,成功时返回:

JSON
{"code": 1, "description": "提交成功", "result": "任务ID", "properties": {}}
字段 说明
code 1 表示提交成功,其他值见错误
description 说明文字
result 任务 ID,用于第 2 步查询
properties 附加信息,可能为空

请按 code == 1 判断成功,不要只看 HTTP 状态码。

选择提交接口

你要做什么 路径 必填字段
文生图 /mj/submit/imagine prompt
多图混合 /mj/submit/blend base64Array
图生提示词 /mj/submit/describe base64
缩短提示词 /mj/submit/shorten prompt
图片编辑 /mj/submit/edits 按 midjourney-proxy 协议
基于已有任务生成视频 /mj/submit/video taskId
上传图片,换取可在提示词中引用的地址 /mj/submit/upload-discord-images 按 midjourney-proxy 协议
换脸 /mj/insight-face/swap sourceBase64、targetBase64

图片字段一律传 data URL,如 data:image/png;base64,iVBORw0KGgo...。

文生图 imagine

字段 类型 必填 说明
prompt string 是 提示词,可带 Midjourney 参数,如 --ar 16:9、--v 6
base64Array string[] 否 垫图,元素为 data URL
botType string 否 MID_JOURNEY 或 NIJI_JOURNEY
state string 否 自定义字符串,查询任务时原样出现在 state 字段

混图 blend

字段 类型 必填 说明
base64Array string[] 是 2–5 张图片的 data URL
dimensions string 否 比例:PORTRAIT / SQUARE / LANDSCAPE

图生提示词 describe

请求体 {"base64": "data:image/png;base64,..."}。任务成功后,生成的提示词在任务的 prompt 或 properties.finalPrompt 中。

其他提交接口

  • shorten: 请求体 {"prompt": "..."},结果在任务详情中
  • video: 必填 taskId(已成功的图片任务),结果在任务的 videoUrl / videoUrls[].url
  • upload-discord-images: 响应的 result 是地址数组而非任务 ID,提交成功即完成,无需查询
  • insight-face/swap: sourceBase64 为人脸来源图,targetBase64 为目标图,两者都必填

midjourney-proxy 协议中的其他字段可以照常传,是否生效取决于 Midjourney 服务。结果请用轮询获取,不要依赖 notifyHook 回调。

第 2 步:查询结果

查询单个任务

GET /mj/task/{id}/fetch

JSON
{
  "id": "1712205491372",
  "action": "IMAGINE",
  "customId": "",
  "botType": "",
  "prompt": "a cat sitting by the window, watercolor --ar 16:9",
  "promptEn": "a cat sitting by the window, watercolor --ar 16:9",
  "description": "提交成功",
  "state": "",
  "submitTime": 1712205491372,
  "startTime": 1712205495000,
  "finishTime": 1712205540000,
  "imageUrl": "https://www.nextai-code.com/mj/image/1712205491372",
  "videoUrl": "",
  "videoUrls": null,
  "status": "SUCCESS",
  "progress": "100%",
  "failReason": "",
  "buttons": [
    {"customId": "MJ::JOB::upsample::1::f6a3...", "emoji": "", "label": "U1", "type": 2, "style": 2},
    {"customId": "MJ::JOB::variation::1::f6a3...", "emoji": "", "label": "V1", "type": 2, "style": 2},
    {"customId": "MJ::JOB::reroll::0::f6a3...::SOLO", "emoji": "🔄", "label": "", "type": 2, "style": 2}
  ],
  "maskBase64": "",
  "properties": {"finalPrompt": "...", "finalZhPrompt": ""}
}
字段 说明
id 任务 ID
action 动作,如 IMAGINE、UPSCALE、VARIATION、REROLL、DESCRIBE、BLEND
status 任务状态,见下表
progress 进度,如 0%、45%、100%
submitTime / startTime / finishTime Unix 毫秒
imageUrl 图片地址,见下文「下载图片」
videoUrl / videoUrls[].url 视频任务的结果
failReason 失败原因
buttons[] 可以继续点的按钮,customId 用于第 3 步,label 如 U1、V2
properties.finalPrompt 最终使用的提示词

只能查询本账号提交的任务(同一账号下的任意 API Key 均可),查不到时返回 task_no_found。

状态:

status 含义
NOT_START / SUBMITTED 已提交,等待执行
IN_PROGRESS 生成中,imageUrl 可能是进度图
MODAL 等待补充内容(局部重绘 / 自定义扩图,见第 3 步)
SUCCESS 完成,终态
FAILURE 失败,终态,额度自动退还

轮询建议: 任务进度大约每 15 秒更新一次,每 5–15 秒查询一次即可。任务超过 1 小时仍未完成会被标记为 FAILURE(failReason 为 任务超时(超过1小时))。

批量查询

POST /mj/task/list-by-condition

BASH
curl https://www.nextai-code.com/mj/task/list-by-condition \
  -H "Authorization: Bearer $NEXTAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"ids": ["1712205491372", "1712205512345"]}'

返回任务数组,元素结构同单个查询。只返回本账号的任务;ids 为空或都不存在时返回 []。

下载图片

imageUrl 通常是平台地址 https://www.nextai-code.com/mj/image/{id},请求时带上 API Key:

BASH
curl https://www.nextai-code.com/mj/image/1712205491372 \
  -H "Authorization: Bearer $NEXTAI_API_KEY" \
  --fail-with-body -o mj.png
  • 任务未完成时地址带 ?rand=...,返回的是进度图;以 SUCCESS 后的地址为准
  • 按响应的 Content-Type 决定保存后缀
  • 任务不存在时返回 400 {"error": "midjourney_task_not_found"}

imageUrl 不是 nextai-code.com 域名时,直接下载即可,不要带 NextAI Code 的 API Key。需要长期保留的图片请及时保存到自己的存储。

查询图片 seed

GET /mj/task/{id}/image-seed,返回结构同提交响应,seed 在 result 中。

第 3 步:点按钮做后续操作

图片完成后,从 buttons[] 里挑一个按钮,把它的 customId 交给 action 接口,平台会自动识别动作。

action(推荐)

POST /mj/submit/action

字段 类型 必填 说明
taskId string 是 按钮所属的任务 ID
customId string 是 从 buttons[].customId 原样复制,不要自己拼

返回新的任务 ID,回到第 2 步查询。常见按钮:

按钮 customId 中的关键字 动作
U1–U4 upsample 放大第 N 张
V1–V4 variation 以第 N 张为基础变换
Vary (Subtle) / Vary (Strong) low_variation / high_variation 弱 / 强变换
⬅️ ➡️ ⬆️ ⬇️ pan 平移扩展
🔄 reroll 重新生成
Zoom Out Outpaint 扩图
Custom Zoom CustomZoom 自定义扩图,需要补充内容,见下文
Vary (Region) Inpaint 局部重绘,需要补充内容,见下文

前提条件:

  • 原任务必须属于当前账号,否则返回 task_not_found
  • 原任务必须已 SUCCESS,否则返回 task_status_not_success

局部重绘与自定义扩图(两步)

Inpaint 和 CustomZoom 按钮点完后需要再补一步内容:

  1. 调 action 提交按钮。响应的 code 为 21,result 是新的任务 ID,表示等待补充内容。这一步不计费
  2. 调 POST /mj/submit/modal 补充内容:
字段 类型 必填 说明
taskId string 是 第 1 步返回的任务 ID
prompt string 否 新提示词;自定义扩图时可写 --zoom 1.5 等参数
maskBase64 string 局部重绘时必填 重绘区域蒙版的 data URL

modal 返回新的任务 ID,回到第 2 步查询。

change(旧客户端)

不想解析 customId 时,可以按动作名和序号操作:POST /mj/submit/change

JSON
{"taskId": "1712205491372", "action": "UPSCALE", "index": 1}
字段 类型 必填 说明
taskId string 是 原任务 ID
action string 是 UPSCALE / VARIATION / REROLL
index integer 是 第几张图,1–4。REROLL 也要传(如 1)

缺字段时分别返回 task_id_is_required、action_is_required、index_is_required。

计费

每个操作按次计费,计费项名为 mj_<动作小写>,如 mj_imagine、mj_upscale、mj_variation、mj_modal;换脸为 swap_face。限制了可用模型的 API Key,需要放行对应计费项才能调用。

  • 只有提交成功(HTTP 200 且 code 为 1)才扣费,提交失败不扣费;Inpaint / CustomZoom 的第一步不扣费,在 modal 时按 mj_modal 扣费
  • 任务最终 FAILURE 时自动退还
  • 查询、下载不计费

错误

请求未通过平台校验时返回 HTTP 400:

JSON
{"description": "task_not_found ", "type": "upstream_error", "code": 4}

description 末尾可能带空格,匹配前先去除首尾空白。

code 含义 处理
1 提交成功 —
4 请求参数错误、任务不存在、额度不足(quota_not_enough)等,description 说明原因 按原因修正
5 平台请求 Midjourney 服务失败 稍后重试
21 仅 Inpaint / CustomZoom 的 action:等待 modal 补充内容 调 /mj/submit/modal
23 Midjourney 队列已满 稍后重试
24 提示词含敏感词,properties.bannedWord 给出命中的词 修改提示词
其他 Midjourney 服务返回的错误,description 为原因 按原因处理

常见 description:

description 原因
prompt_is_required imagine 缺少 prompt
custom_id_is_required / unknown_action:... action 缺少或无法识别 customId
task_not_found / task_no_found 任务不存在或不属于当前账号
task_status_not_success 原任务尚未成功,不能点按钮
sour_base64_and_target_base64_is_required 换脸缺少图片
模型 mj_xxx 未配置按次定价... 该操作暂未开放,请联系客服

API Key 无效、额度不足、API Key 无权使用该计费项等错误返回 OpenAI 格式 {"error": {...}},见错误码。

相关文档