> ## 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.

# Iniziare con Midjourney API

> Avvio rapido per Midjourney API su CometAPI: invia /mj/submit/imagine, interroga /mj/task/{id}/fetch, poi usa /mj/submit/action e i pulsanti modal.

## Comprendere il concetto fondamentale

La MidJourney API **simula le interazioni dei pulsanti di Discord**. A differenza delle tipiche API REST, funziona come una **macchina a stati** in cui ogni operazione restituisce nuovi pulsanti per il passaggio successivo.

### Le 4 API fondamentali

| API                                                                                      | Scopo                                                | Quando usarla                                       |
| ---------------------------------------------------------------------------------------- | ---------------------------------------------------- | --------------------------------------------------- |
| [`POST /mj/submit/imagine`](/api/image/midjourney/imagine)                               | Generazione text-to-image                            | Punto di partenza per tutti i flussi di lavoro      |
| [`GET /mj/task/\{id\}/fetch`](/api/image/midjourney/task-fetching-api/fetch-single-task) | Controllare lo stato del task e ottenere i pulsanti  | Dopo ogni invio (interroga finché non è completato) |
| [`POST /mj/submit/action`](/api/image/midjourney/action)                                 | Fare clic su un pulsante (upscale, vary, zoom, ecc.) | Quando vuoi operare su un'immagine                  |
| [`POST /mj/submit/modal`](/api/image/midjourney/modal)                                   | Inviare input aggiuntivo                             | Solo quando lo stato è `MODAL`                      |

***

## Diagramma completo del flusso di lavoro

```
┌─────────────────────────────────────────────────────────────────────────────┐
│                         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       │
           └──────────────────┘
```

***

## Concetto chiave: Pulsanti e customId

Ogni task completato con successo restituisce un array `buttons`. Ogni pulsante ha un `customId` che usi per attivare l'azione successiva.

**Esempio di risposta da `/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` non è un valore fisso. Cambia per ogni task. Ottienilo sempre dall'array `buttons`.
</Warning>

***

## Riferimento dei pulsanti per fase

### Dopo IMAGINE (immagine a griglia 4)

Questi pulsanti vengono restituiti quando la generazione iniziale dell'immagine è completata:

| Button | customId Pattern                | Action                          | Result                               |
| ------ | ------------------------------- | ------------------------------- | ------------------------------------ |
| U1-U4  | `MJ::JOB::upsample::1::xxx`     | Upscale di una singola immagine | Immagine singola ad alta risoluzione |
| V1-V4  | `MJ::JOB::variation::1::xxx`    | Genera variazioni               | Nuova griglia 4                      |
| 🔄     | `MJ::JOB::reroll::0::xxx::SOLO` | Rigenera tutto                  | Nuova griglia 4                      |

### Dopo UPSCALE (immagine singola)

Dopo l'upscaling, hai accesso agli strumenti di modifica:

| Label                             | Needs Modal?    |
| --------------------------------- | --------------- |
| Upscale (Subtle) / Upscale (2x)   | ❌ No            |
| Upscale (Creative) / Upscale (4x) | ❌ No            |
| Vary (Subtle) 🪄                  | ❌ No            |
| Vary (Strong) 🪄                  | ❌ No            |
| Vary (Region) 🖌️                 | ✅ Sì (maschera) |
| Zoom Out 2x / 1.5x 🔍             | ❌ No            |
| Custom Zoom 🔍                    | ✅ Sì (prompt)   |
| ⬅️➡️⬆️⬇️ Pan                      | ❌ No            |
| Animate 🎞️                       | ❌ No            |
| 🔄 Reroll                         | ❌ No            |

> **Nota:** Le etichette dei pulsanti e i formati `customId` possono variare a seconda della versione MJ specificata nel tuo prompt (ad esempio, `--v 6.1` vs `--v 5.2`). Leggi sempre i pulsanti dalla risposta dell'API.

<Warning>
  Il pulsante Inpaint (Vary Region) appare solo dopo Upscale.
</Warning>

***

## Esempio completo: genera e fai upscale

### Passaggio 1: Invia una richiesta 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"] }
  }'
```

**Risposta:**

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

### Passaggio 2: Esegui il polling dello stato del task

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

**Risposta (quando completato):**

```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" },
    ...
  ]
}
```

### Passaggio 3: Fai clic su U1 per eseguire l'upscale

```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"
  }'
```

**Risposta:**

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

### Passaggio 4: Esegui il polling del nuovo task e ottieni il risultato

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

***

## Quando è richiesto Modal?

Quando chiami [`/mj/submit/action`](/api/image/midjourney/action) e lo stato del task diventa `MODAL` invece di `SUCCESS`, devi chiamare [`/mj/submit/modal`](/api/image/midjourney/modal) per fornire input aggiuntivo.

### Operazioni Modal confermate

| Operation   | Button         | What to Submit                                |
| ----------- | -------------- | --------------------------------------------- |
| Inpaint     | Vary (Region)  | `maskBase64` (maschera PNG) + `prompt`        |
| Custom Zoom | 🔍 Custom Zoom | `prompt` (ad esempio, "your prompt --zoom 2") |

**Esempio: flusso 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,..."
  }'
```

***

## Selezione della modalità di velocità

Aggiungi il prefisso di velocità al percorso:

| Modalità | Prefisso del percorso | Esempio                       |
| -------- | --------------------- | ----------------------------- |
| Fast     | `/mj-fast`            | `/mj-fast/mj/submit/imagine`  |
| Turbo    | `/mj-turbo`           | `/mj-turbo/mj/submit/imagine` |
| Relax    | (predefinito)         | `/mj/submit/imagine`          |

***

## Altri punti di ingresso

Queste API sono **punti di ingresso indipendenti** che non seguono il flusso imagine → action:

| API                                                            | Scopo                                     |
| -------------------------------------------------------------- | ----------------------------------------- |
| [`POST /mj/submit/blend`](/api/image/midjourney/blend)         | Unisce da 2 a 5 immagini in una sola      |
| [`POST /mj/submit/describe`](/api/image/midjourney/describe)   | Genera un prompt a partire da un'immagine |
| [`POST /mj/submit/video`](/api/image/midjourney/submit-video)  | Converte un'immagine in video             |
| [`POST /mj/submit/edits`](/api/image/midjourney/submit-editor) | Modifica un'immagine con una maschera     |

***

## Suggerimenti per la risoluzione dei problemi

In base al design e al flusso di lavoro dell'API, ecco alcuni problemi comuni che potresti incontrare:

| Problema                                      | Causa probabile                            | Soluzione                                                                                                                                      |
| --------------------------------------------- | ------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| Impossibile trovare il pulsante Vary (Region) | Stai visualizzando un'immagine a griglia 4 | Esegui prima l'upscale (fai clic su U1-U4), poi controlla i pulsanti                                                                           |
| Stato del task bloccato su `MODAL`            | L'operazione richiede input aggiuntivi     | Chiama [`/mj/submit/modal`](/api/image/midjourney/modal) con i dati richiesti                                                                  |
| `customId` non funziona                       | Stai usando un valore obsoleto o hardcoded | Ottieni sempre un `customId` aggiornato dalla risposta di [`/mj/task/\{id\}/fetch`](/api/image/midjourney/task-fetching-api/fetch-single-task) |
| Array `buttons` vuoto                         | Il task è ancora in corso                  | Attendi `status: "SUCCESS"` prima di accedere ai pulsanti                                                                                      |
