> ## Documentation Index
> Fetch the complete documentation index at: https://apidoc.cometapi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Midjourney API を始める

> CometAPI で Midjourney API を使うためのクイックスタート: /mj/submit/imagine を送信し、/mj/task/{id}/fetch をポーリングして、その後 /mj/submit/action と modal ボタンを使用します。

## コアコンセプトを理解する

MidJourney API は **Discord のボタン操作をシミュレート** します。一般的な REST API とは異なり、各操作が次のステップ用の新しいボタンを返す **状態機械** として動作します。

### 4 つのコア API

| API                                                                                      | 用途                               | 使用するタイミング              |
| ---------------------------------------------------------------------------------------- | -------------------------------- | ---------------------- |
| [`POST /mj/submit/imagine`](/api/image/midjourney/imagine)                               | テキストから画像への生成                     | すべてのワークフローの開始点         |
| [`GET /mj/task/\{id\}/fetch`](/api/image/midjourney/task-fetching-api/fetch-single-task) | タスク状態の照会とボタンの取得                  | 送信のたびに実行（完了までポーリング）    |
| [`POST /mj/submit/action`](/api/image/midjourney/action)                                 | ボタンをクリックする（upscale、vary、zoom など） | 画像に対して操作したいとき          |
| [`POST /mj/submit/modal`](/api/image/midjourney/modal)                                   | 追加入力を送信する                        | status が `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       │
           └──────────────────┘
```

***

## 重要な概念: Buttons と customId

成功した各タスクは `buttons` 配列を返します。各ボタンには `customId` があり、これを使って次のアクションをトリガーします。

**`/mj/task/\{id\}/fetch` からのレスポンス例:**

```json theme={null}
{
  "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": "🔄" }
  ]
}
```

<Warning>
  `customId` は固定値ではありません。タスクごとに変わります。必ず `buttons` 配列から取得してください。
</Warning>

***

## ステージ別ボタンリファレンス

### 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` の形式は、プロンプトで指定した MJ バージョン（例: `--v 6.1` と `--v 5.2`）によって異なる場合があります。必ず API レスポンスからボタンを読み取ってください。

<Warning>
  Inpaint（Vary Region）ボタンは Upscale 後にのみ表示されます。
</Warning>

***

## 完全な例: 生成してアップスケールする

### ステップ 1: imagine リクエストを送信

```bash theme={null}
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"] }
  }'
```

**レスポンス:**

```json theme={null}
{ "code": 1, "result": "1768464763141701" }
```

### ステップ 2: タスクステータスをポーリング

```bash theme={null}
curl -X GET 'https://api.cometapi.com/mj/task/1768464763141701/fetch' \
  -H "Authorization: Bearer $COMETAPI_KEY"
```

**レスポンス（完了時）:**

```json theme={null}
{
  "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 をクリックしてアップスケール

```bash theme={null}
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"
  }'
```

**レスポンス:**

```json theme={null}
{ "code": 1, "result": "1768464800000000" }
```

### ステップ 4: 新しいタスクをポーリングして結果を取得

```bash theme={null}
curl -X GET 'https://api.cometapi.com/mj/task/1768464800000000/fetch' \
  -H "Authorization: Bearer $COMETAPI_KEY"
```

***

## Modal が必要なのはいつですか？

[`/mj/submit/action`](/api/image/midjourney/action) を呼び出したときに、タスクステータスが `SUCCESS` ではなく `MODAL` になった場合は、追加の入力を提供するために [`/mj/submit/modal`](/api/image/midjourney/modal) を呼び出す必要があります。

### 確認済みの Modal 操作

| Operation   | Button         | What to Submit                      |
| ----------- | -------------- | ----------------------------------- |
| Inpaint     | Vary (Region)  | `maskBase64`（PNG マスク）+ `prompt`     |
| Custom Zoom | 🔍 Custom Zoom | `prompt`（例: "your prompt --zoom 2"） |

**例: Inpaint フロー**

```bash theme={null}
# 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`](/api/image/midjourney/blend)         | 2〜5 枚の画像を 1 枚にブレンド   |
| [`POST /mj/submit/describe`](/api/image/midjourney/describe)   | 画像からプロンプト（Prompt）を生成 |
| [`POST /mj/submit/video`](/api/image/midjourney/submit-video)  | 画像を動画に変換             |
| [`POST /mj/submit/edits`](/api/image/midjourney/submit-editor) | マスクを使って画像を編集         |

***

## トラブルシューティングのヒント

API の設計とワークフローに基づいて、遭遇しやすい一般的な問題を以下に示します:

| 問題                          | 考えられる原因                | 解決策                                                                                                                      |
| --------------------------- | ---------------------- | ------------------------------------------------------------------------------------------------------------------------ |
| Vary（Region）ボタンが見つからない      | 4 分割グリッド画像を見ている        | 先にアップスケールします（U1〜U4 をクリック）。その後でボタンを確認してください                                                                               |
| タスクステータスが `MODAL` のまま止まっている | 操作に追加の入力が必要            | 必要なデータを指定して [`/mj/submit/modal`](/api/image/midjourney/modal) を呼び出してください                                                 |
| `customId` が動作しない           | 古い値またはハードコードされた値を使っている | 必ず [`/mj/task/\{id\}/fetch`](/api/image/midjourney/task-fetching-api/fetch-single-task) のレスポンスから新しい `customId` を取得してください |
| `buttons` 配列が空              | タスクがまだ進行中              | ボタンにアクセスする前に `status: "SUCCESS"` になるまで待ってください                                                                            |
