# Midjourney Image Generation

Generate and edit images through the native Midjourney task endpoints. Tasks run asynchronously: submit a task to obtain a task ID, poll its status, and trigger follow-up actions (upscale, variation, region inpainting, zoom, and pan) from the `buttons` returned by the previous task.

## Endpoints & Authentication

Send all requests to `https://vip.brioi.com` with your API key:

```http
Authorization: Bearer YOUR_API_KEY
Content-Type: application/json
```

| Operation | Method & Path |
|---|---|
| Text-to-image (Imagine) | `POST /mj/submit/imagine` |
| Multi-image blend (Blend) | `POST /mj/submit/blend` |
| Image-to-prompt (Describe) | `POST /mj/submit/describe` |
| Button action (upscale, variation, reroll, etc.) | `POST /mj/submit/action` |
| Modal confirmation (inpainting, custom zoom, etc.) | `POST /mj/submit/modal` |
| Fetch task status | `GET /mj/task/{task_id}/fetch` |
| Batch fetch tasks | `POST /mj/task/list-by-condition` with body `{"ids": ["TASK_ID_1", "TASK_ID_2"]}` |
| Download image | `GET /mj/image/{task_id}` |

Do not include `model` in the request body; the operation type is determined by the submission path and button `customId`.

Batch fetch returns an array of task objects using the same schema as single-task fetch, scoped to tasks belonging to the current API key's account; nonexistent IDs are omitted from the response. `GET /mj/image/{task_id}` does not require an API key—anyone with the URL can download the image, so do not share it publicly.

## Text-to-Image (Imagine)

```bash
curl -X POST "https://vip.brioi.com/mj/submit/imagine" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "botType": "MID_JOURNEY",
    "prompt": "a red glass cube on a plain white background, studio light --ar 1:1 --v 7"
  }'
```

| Parameter | Type | Required | Description |
|---|---|---:|---|
| `prompt` | string | Yes | Scene description. Append Midjourney parameters at the end, such as `--ar 16:9`, `--v 7`, or `--stylize 100`. When using reference images, place public image URLs at the beginning of `prompt`. |
| `botType` | string | No | Set to `MID_JOURNEY`. |
| `state` | string | No | Custom caller metadata string (not an idempotency key). |

Model version is controlled by `--v` in the prompt. Omitting `--v` uses the default version; check `properties.finalPrompt` in the completed task for the full resolved prompt and parameters.

## Multi-Image Blend

Blend multiple images into a new 2×2 grid:

```bash
curl -X POST "https://vip.brioi.com/mj/submit/blend" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "botType": "MID_JOURNEY",
    "base64Array": [
      "https://example.com/image-1.png",
      "https://example.com/image-2.png"
    ],
    "dimensions": "SQUARE"
  }'
```

| Parameter | Description |
|---|---|
| `base64Array` | 2–3 publicly accessible image URLs. |
| `dimensions` | Output aspect ratio: `PORTRAIT`, `SQUARE`, or `LANDSCAPE`. |

Oversized input images may cause the task to fail (with `failReason` reporting an execution error); input images around `1024×1024` are recommended.

## Image-to-Prompt (Describe)

Generate candidate text prompts from an input image:

```bash
curl -X POST "https://vip.brioi.com/mj/submit/describe" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "botType": "MID_JOURNEY",
    "link": "https://example.com/image.png"
  }'
```

Once the task succeeds, read the generated descriptions from `promptEn` or `properties.finalPrompt`, separated by newlines. Descriptions may be returned in Chinese—do not assume they are always English. `Describe` does not generate a new image; `imageUrl` is not the output.

## Submission Response

```json
{
  "code": 1,
  "description": "Submitted successfully",
  "result": "1795258475697171",
  "properties": {}
}
```

| `code` | Meaning |
|---|---|
| `1` | Submitted successfully; `result` is the task ID. |
| `21` | Additional modal prompt required (see [Region Inpainting](#region-inpainting) below); `result` is the pending task ID. |
| `22` | Queued; `result` is the task ID—continue polling as normal. |
| Other | Submission failed; inspect `description`. |

A successful submission only indicates that the task was accepted, not that image generation is complete. If a submission request times out or disconnects, query existing tasks first rather than immediately resubmitting.

## Polling Task Status

```bash
curl "https://vip.brioi.com/mj/task/1795258475697171/fetch" \
  -H "Authorization: Bearer YOUR_API_KEY"
```

Poll every 5–10 seconds.

| `status` | Action |
|---|---|
| Empty string `""`, `NOT_START`, `SUBMITTED`, `IN_PROGRESS` | In progress; continue polling. |
| `MODAL` | Waiting for follow-up prompt; call `POST /mj/submit/modal`. |
| `SUCCESS` | Completed; read `imageUrl` and `buttons`. |
| `FAILURE`, `CANCEL` | Terminated; read `failReason`. |

Stop polling once the task enters `SUCCESS`, `FAILURE`, or `CANCEL`. Newly submitted tasks may briefly return an empty `status` and `0%` `progress` while queued—continue polling.

## Results & Action Buttons

Excerpt from a completed poll response:

```json
{
  "id": "1795258475697171",
  "action": "IMAGINE",
  "status": "SUCCESS",
  "progress": "100%",
  "imageUrl": "https://vip.brioi.com/mj/image/1795258475697171",
  "properties": {
    "finalPrompt": "a red glass cube on a plain white background, studio light --ar 1:1 --v 7 --stylize 100 --fast"
  },
  "buttons": [
    { "label": "U1", "customId": "MJ::JOB::upsample::1::6b01786b99f03bf628bb91f3" },
    { "label": "V1", "customId": "MJ::JOB::variation::1::6b01786b99f03bf628bb91f3" },
    { "label": "", "customId": "MJ::JOB::reroll::0::6b01786b99f03bf628bb91f3::SOLO" }
  ]
}
```

| Field | Description |
|---|---|
| `imageUrl` | Result image URL. Imagine, variation, and inpainting tasks return a 2×2 composite grid; use `U1`–`U4` to extract and upscale an individual image. |
| `buttons[]` | Follow-up actions available on this task. Pass `customId` verbatim—never construct it manually or reuse values across tasks. |
| `properties.finalPrompt` | Full resolved prompt and parameters executed by Midjourney; useful for verifying model version and aspect ratio. |
| `failReason` | Failure reason, populated only when the task fails. |

Download the image:

```bash
curl -L "https://vip.brioi.com/mj/image/1795258475697171" -o result.png
```

Returned images may be PNG or WebP—save the file according to the response `Content-Type` header.

## Button Actions

Upscales, variations, and rerolls all go through `POST /mj/submit/action`, with the specific action determined by the parent button's `customId`:

```bash
curl -X POST "https://vip.brioi.com/mj/submit/action" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "taskId": "1795258475697171",
    "customId": "MJ::JOB::upsample::1::6b01786b99f03bf628bb91f3",
    "enableRemix": false
  }'
```

| Parameter | Description |
|---|---|
| `taskId` | ID of the immediate parent task; must already be in `SUCCESS` status and include the target button. |
| `customId` | Copied verbatim from `buttons[].customId` on the parent task. |
| `enableRemix` | Set to `false` for standard buttons; set to `true` for operations requiring a follow-up prompt (such as region inpainting or custom zoom). |

Save the new task ID returned in the submission response and poll that new task ID—do not poll the parent `taskId` for the child result.

Common button actions:

| Parent Task | Button | Effect |
|---|---|---|
| 2×2 grid | `U1`–`U4` | Upscale the selected quadrant into a standalone image. |
| 2×2 grid | `V1`–`V4` | Generate four new variations based on the selected quadrant. |
| 2×2 grid | Reroll (`::reroll::`) | Regenerate four images from the original prompt. |
| Upscaled single image | `Upscale (Subtle)` / `Upscale (Creative)` | Upscale 2× (e.g., `1024×1024` to `2048×2048`) with subtle or creative refinement. |
| Upscaled single image | `Vary (Subtle)` / `Vary (Strong)` | Generate four new subtle or strong variations. |
| Upscaled single image | `Vary (Region)` | Repaint a masked region (see next section). |
| Upscaled single image | `Zoom Out 2x` / `Zoom Out 1.5x` | Outpaint outward while preserving the aspect ratio. |
| Upscaled single image | `Custom Zoom` | Outpaint at a custom zoom factor with a modal prompt (see below). |
| Upscaled single image | Pan arrows (`::pan_left::`, `::pan_right::`, `::pan_up::`, `::pan_down::`) | Extend the canvas in the chosen direction (horizontal pans widen the image; vertical pans increase height). |

## Region Inpainting

Region inpainting is a two-step flow: submit the `Vary (Region)` button action from an upscaled single image, then submit the repaint prompt and mask.

Step 1 — Submit the action with `enableRemix` set to `true`:

```json
{
  "taskId": "1795261838736225",
  "customId": "MJ::Inpaint::1::6b01786b99f03bf628bb91f3::SOLO",
  "enableRemix": true
}
```

The response returns `code: 21` and `description: "Waiting for window confirm"`, where `result` is the pending task ID.

Step 2 — Call `POST /mj/submit/modal`:

```json
{
  "taskId": "1795262079107250",
  "prompt": "a small golden star inside the cube",
  "maskBase64": "iVBORw0KGgoAAAANSUhEUgAA..."
}
```

| Parameter | Description |
|---|---|
| `taskId` | `result` returned in Step 1. |
| `prompt` | Content to generate inside the masked region. |
| `maskBase64` | Raw base64-encoded PNG mask without a `data:image/png;base64,` prefix. Must match the dimensions of the source single image; white areas are repainted and black areas are preserved. |

After submitting the modal, poll that same `taskId` until `SUCCESS`. Its status may briefly remain `MODAL` during processing—do not resubmit.

## Custom Zoom

Custom zoom also runs in two steps. First submit the `Custom Zoom` button action from an upscaled single image with `enableRemix: true` (returning `code: 21`), then call `POST /mj/submit/modal` with the zoom factor appended to the prompt:

```json
{
  "taskId": "ACTION_RESPONSE_RESULT",
  "prompt": "a red glass cube on a plain white background, studio light --zoom 1.5"
}
```

Specify the outpaint multiplier after `--zoom` (for example, `1.5`), then poll that same task ID.

## Limits & Error Handling

- Animation buttons (`Animate (High motion)`, `Animate (Low motion)`) are unavailable and return an `unknown_action` error.
- Buttons are scoped strictly to the task that returned them; `customId` values cannot be swapped across tasks.
- Validation or query errors return HTTP `400` with a body shaped like `{"code": 4, "description": "Error description"}`—inspect `description` to diagnose (`task_no_found` indicates the queried task does not exist).
- `401` indicates a missing or invalid API key, `403` indicates insufficient balance/quota or missing model permission, `429` indicates rate limiting or server busy (`code` `30`), and `503` indicates the service is temporarily unavailable.

