了解核心概念
MidJourney API 模擬 Discord 按鈕互動。不同於典型的 REST API,它以狀態機的方式運作,其中每個操作都會回傳下一步可用的新按鈕。4 個核心 API
| API | 用途 | 何時使用 |
|---|---|---|
POST /mj/submit/imagine | 文字轉圖像生成 | 所有工作流程的起點 |
GET /mj/task/\{id\}/fetch | 查詢任務狀態並取得按鈕 | 每次 submit 之後(輪詢直到完成) |
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) 🖌️ | ✅ 是(遮罩) |
| Zoom Out 2x / 1.5x 🔍 | ❌ 否 |
| Custom Zoom 🔍 | ✅ 是(prompt) |
| ⬅️➡️⬆️⬇️ Pan | ❌ 否 |
| Animate 🎞️ | ❌ 否 |
| 🔄 Reroll | ❌ 否 |
Note: 按鈕標籤與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 遮罩)+ 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" |