理解核心概念
MidJourney API 模拟 Discord 按钮交互。与典型的 REST API 不同,它以状态机的方式工作,其中每次操作都会返回下一步所需的新按钮。4 个核心 API
| API | 用途 | 何时使用 |
|---|---|---|
POST /mj/submit/imagine | 文生图生成 | 所有工作流的起点 |
GET /mj/task/\{id\}/fetch | 查询任务状态并获取按钮 | 每次提交后都使用(轮询直到完成) |
POST /mj/submit/action | 点击按钮(upscale、vary、zoom 等) | 当你想对图像进行操作时 |
POST /mj/submit/modal | 提交额外输入 | 仅当状态为 MODAL 时使用 |
完整工作流图
┌─────────────────────────────────────────────────────────────────────────────┐
│ MIDJOURNEY API WORKFLOW │
└─────────────────────────────────────────────────────────────────────────────┘
┌──────────────────┐
│ POST /submit/ │ ← Step 1: Submit prompt, get task_id
│ imagine │
└────────┬─────────┘
│ Returns: { "result": "task_id_1" }
▼
┌──────────────────┐
│ GET /task/{id}/ │ ← Step 2: Poll until status = "SUCCESS"
│ fetch │
└────────┬─────────┘
│ Returns: imageUrl + buttons[] (U1,U2,U3,U4,V1,V2,V3,V4,🔄)
▼
┌──────────────────┐
│ POST /submit/ │ ← Step 3: Click a button using customId
│ action │
└────────┬─────────┘
│ Returns: { "result": "task_id_2" }
▼
┌──────────────────┐
│ GET /task/{id}/ │ ← Step 4: Poll the new task
│ fetch │
└────────┬─────────┘
│
├─── status = "SUCCESS" → Done! Get imageUrl
│
└─── status = "MODAL" → Need additional input (see Step 5)
│
▼
┌──────────────────┐
│ POST /submit/ │ ← Step 5: Submit mask/prompt for special operations
│ modal │
└────────┬─────────┘
│ Returns: { "result": "task_id_3" }
▼
┌──────────────────┐
│ GET /task/{id}/ │ ← Step 6: Poll until SUCCESS
│ fetch │
└──────────────────┘
关键概念:按钮和 customId
每个成功的任务都会返回一个buttons 数组。每个按钮都有一个 customId,你可以用它来触发下一步操作。
来自 /mj/task/\{id\}/fetch 的响应示例:
{
"status": "SUCCESS",
"imageUrl": "https://api.cometapi.com/mj/image/xxx",
"buttons": [
{ "customId": "MJ::JOB::upsample::1::abc123", "label": "U1" },
{ "customId": "MJ::JOB::upsample::2::abc123", "label": "U2" },
{ "customId": "MJ::JOB::variation::1::abc123", "label": "V1" },
{ "customId": "MJ::JOB::reroll::0::abc123", "emoji": "🔄" }
]
}
customId 不是固定值。它会随着每个任务而变化。请始终从 buttons 数组中获取它。按阶段划分的按钮参考
IMAGINE 之后(4 宫格图像)
当你的初始图像生成完成后,会返回这些按钮:| Button | customId Pattern | Action | Result |
|---|---|---|---|
| U1-U4 | MJ::JOB::upsample::1::xxx | 放大单张图像 | 高分辨率单图 |
| V1-V4 | MJ::JOB::variation::1::xxx | 生成变体 | 新的 4 宫格 |
| 🔄 | MJ::JOB::reroll::0::xxx::SOLO | 重新生成全部 | 新的 4 宫格 |
UPSCALE 之后(单张图像)
放大后,你可以使用这些编辑工具:| Label | Needs Modal? |
|---|---|
| Upscale (Subtle) / Upscale (2x) | ❌ 否 |
| Upscale (Creative) / Upscale (4x) | ❌ 否 |
| Vary (Subtle) 🪄 | ❌ 否 |
| Vary (Strong) 🪄 | ❌ 否 |
| Vary (Region) 🖌️ | ✅ 是(mask) |
| Zoom Out 2x / 1.5x 🔍 | ❌ 否 |
| Custom Zoom 🔍 | ✅ 是(prompt) |
| ⬅️➡️⬆️⬇️ Pan | ❌ 否 |
| Animate 🎞️ | ❌ 否 |
| 🔄 Reroll | ❌ 否 |
注意: 按钮标签和customId格式可能会因你在 prompt 中指定的 MJ 版本而有所不同(例如--v 6.1与--v 5.2)。请始终从 API 响应中读取按钮信息。
Inpaint(Vary Region)按钮只会在 Upscale 之后出现。
完整示例:生成并放大
第 1 步:提交 imagine 请求
curl -X POST 'https://api.cometapi.com/mj/submit/imagine' \
-H "Authorization: Bearer $COMETAPI_KEY" \
-H 'Content-Type: application/json' \
-d '{
"botType": "MID_JOURNEY",
"prompt": "a cute cat --v 6.1",
"accountFilter": { "modes": ["FAST"] }
}'
{ "code": 1, "result": "1768464763141701" }
第 2 步:轮询任务状态
curl -X GET 'https://api.cometapi.com/mj/task/1768464763141701/fetch' \
-H "Authorization: Bearer $COMETAPI_KEY"
{
"status": "SUCCESS",
"imageUrl": "https://api.cometapi.com/mj/image/1768464763141701",
"buttons": [
{ "customId": "MJ::JOB::upsample::1::5f20922e-xxx", "label": "U1" },
{ "customId": "MJ::JOB::upsample::2::5f20922e-xxx", "label": "U2" },
...
]
}
第 3 步:点击 U1 进行放大
curl -X POST 'https://api.cometapi.com/mj/submit/action' \
-H "Authorization: Bearer $COMETAPI_KEY" \
-H 'Content-Type: application/json' \
-d '{
"taskId": "1768464763141701",
"customId": "MJ::JOB::upsample::1::5f20922e-xxx"
}'
{ "code": 1, "result": "1768464800000000" }
第 4 步:轮询新任务并获取结果
curl -X GET 'https://api.cometapi.com/mj/task/1768464800000000/fetch' \
-H "Authorization: Bearer $COMETAPI_KEY"
什么时候需要 Modal?
当你调用/mj/submit/action 且任务状态变为 MODAL 而不是 SUCCESS 时,你必须调用 /mj/submit/modal 来提供额外输入。
已确认需要 Modal 的操作
| Operation | Button | What to Submit |
|---|---|---|
| Inpaint | Vary (Region) | maskBase64(PNG mask)+ prompt |
| Custom Zoom | 🔍 Custom Zoom | prompt(例如 "your prompt --zoom 2") |
# 1. Click Vary (Region) button via Action API
curl -X POST 'https://api.cometapi.com/mj/submit/action' \
-H "Authorization: Bearer $COMETAPI_KEY" \
-H 'Content-Type: application/json' \
-d '{"taskId": "xxx", "customId": "MJ::Inpaint::xxx", "enableRemix": true}'
# 2. Poll and see status = "MODAL"
curl -X GET 'https://api.cometapi.com/mj/task/new_task_id/fetch'
# Response: { "status": "MODAL" }
# 3. Submit mask and prompt via Modal API
curl -X POST 'https://api.cometapi.com/mj/submit/modal' \
-H "Authorization: Bearer $COMETAPI_KEY" \
-H 'Content-Type: application/json' \
-d '{
"taskId": "new_task_id",
"prompt": "replace with golden crown",
"maskBase64": "data:image/png;base64,..."
}'
速度模式选择
在路径中添加速度前缀:| 模式 | 路径前缀 | 示例 |
|---|---|---|
| Fast | /mj-fast | /mj-fast/mj/submit/imagine |
| Turbo | /mj-turbo | /mj-turbo/mj/submit/imagine |
| Relax | (默认) | /mj/submit/imagine |
其他入口点
这些 API 是独立入口点,不遵循 imagine → action 流程:| API | 用途 |
|---|---|
POST /mj/submit/blend | 将 2-5 张图片混合为一张 |
POST /mj/submit/describe | 根据图片生成 prompt |
POST /mj/submit/video | 将图片转换为视频 |
POST /mj/submit/edits | 使用蒙版编辑图片 |
故障排查提示
基于 API 设计和工作流,以下是你可能遇到的常见问题:| 问题 | 可能原因 | 解决方案 |
|---|---|---|
| 找不到 Vary (Region) 按钮 | 正在查看 4 宫格图片 | 先放大(点击 U1-U4),然后再检查按钮 |
任务状态卡在 MODAL | 操作需要额外输入 | 使用所需数据调用 /mj/submit/modal |
customId 无法使用 | 使用了过时或硬编码的值 | 始终从 /mj/task/\{id\}/fetch 的响应中获取最新的 customId |
buttons 数组为空 | 任务仍在处理中 | 在访问按钮之前,先等待 status: "SUCCESS" |