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

# Create chat

> Creazione di una nuova chat e avvio del lavoro con l'IA

Crea una nuova chat e avvia l'elaborazione da parte dell'IA.

## Richiesta

```
POST /api/chat
```

### Header

| Header | Obbligatorio | Descrizione |
| - | - | - |
| `Authorization` | Sì | `Bearer macaly_...` |
| `Content-Type` | Sì | `application/json` |

### Parametri del corpo della richiesta

| Campo | Tipo | Obbligatorio | Predefinito | Descrizione |
| - | - | - | - | - |
| `content` | string | Sì | - | Il messaggio o prompt dell'utente |
| `stream` | boolean | No | `true` | Se `false`, restituisce JSON invece di uno stream |
| `backend` | string | No | `"DEFAULT"` | Backend da utilizzare |
| `executionMode` | `"auto"` \| `"planning"` \| `"build"` | No | - | Determina come l'IA affronta il compito |
| `model` | `"gpt-5.6-terra"` \| `"gpt-6.1-sol"` \| `"gpt-6-luna"` \| `"grok-4-6"` \| `"kimi-k3"` \| `"sonnet-5-5"` \| `"opus-5-5"` \| `"blueai-glm-5-3"` \| `"blueai-glm-5-3-flash"` | No | - | Modello AI da utilizzare. Se non specificato, viene usato il modello configurato per il team |
| `reasoningEffort` | `"medium"` \| `"high"` | No | - | Determina quanto a fondo il modello ragiona sul compito |
| `experimental_attachments` | array | No | `[]` | Allegati di file (formato descritto di seguito) |

#### Formato degli allegati

Ogni elemento dell'array `experimental_attachments` ha la struttura seguente:

| Campo | Tipo | Obbligatorio | Descrizione |
| - | - | - | - |
| `url` | string | Sì | URL del file da allegare |
| `name` | string | No | Nome visualizzato dell'allegato |
| `contentType` | string | No | Tipo MIME del file |

### Esempio di richiesta

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST https://www.macaly.com/api/chat \
    -H "Authorization: Bearer macaly_abc123..." \
    -H "Content-Type: application/json" \
    -d '{
      "content": "Create a landing page for a coffee shop",
      "stream": false
    }'
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch('https://www.macaly.com/api/chat', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer macaly_abc123...',
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      content: 'Create a landing page for a coffee shop',
      stream: false,
    }),
  });

  const data = await response.json();
  console.log(data.chatId);
  ```

  ```python Python theme={null}
  import requests

  response = requests.post(
      'https://www.macaly.com/api/chat',
      headers={
          'Authorization': 'Bearer macaly_abc123...',
          'Content-Type': 'application/json',
      },
      json={
          'content': 'Create a landing page for a coffee shop',
          'stream': False,
      },
  )

  data = response.json()
  print(data['chatId'])
  ```
</CodeGroup>

## Risposta

### Risposta JSON (con `stream: false`)

```json theme={null}
{
  "chatId": "abc123def456",
  "url": "https://www.macaly.com/chat/abc123def456",
  "streamId": "stream_xyz789",
  "assistantMessageId": "msg_123"
}
```

| Campo | Tipo | Descrizione |
| - | - | - |
| `chatId` | string | L'identificatore univoco della chat |
| `url` | string | URL diretta per visualizzare la chat |
| `streamId` | string \| undefined | Identificatore dello stream a cui sottoscriversi. Presente solo per i backend basati su elaborazione |
| `assistantMessageId` | string \| undefined | ID del messaggio dell'assistente in fase di generazione. Presente solo per i backend basati su elaborazione |

### Risposta in streaming (con `stream: true`, predefinito)

Quando lo streaming è attivo, la risposta è uno stream di testo:

```
2:[{"chatId":"abc123def456"}]
0:"Starting to build..."
...
```

La prima riga contiene l'ID della chat nel formato `2:[{"chatId":"..."}]`.

#### Estrarre l'ID della chat dallo stream

```javascript theme={null}
const text = await response.text();
const match = text.match(/2:\[{"chatId":"([^"]+)"/);
const chatId = match?.[1];
```

## Codici di stato

| Codice | Descrizione |
| - | - |
| 200 | Operazione riuscita: chat creata |
| 400 | Corpo della richiesta non valido |
| 401 | Chiave API non valida o mancante |
| 402 | Crediti insufficienti |
| 403 | Accesso negato: permessi insufficienti per il team specificato |
| 422 | Errore di validazione |
| 429 | Limite di richieste raggiunto |
| 500 | Errore del server |

## Passaggi successivi

Dopo aver creato una chat:

1. **Segui l'avanzamento** con [`GET /api/chat/{chatId}/status`](/docs/it/api/get-status)
2. **Sottoscrivi lo stream** con [`GET /api/chat/{chatId}/subscribe`](/docs/it/api/subscribe-stream) per gli aggiornamenti in tempo reale
3. **Invia messaggi successivi** con [`POST /api/chat/{chatId}/publish`](/docs/it/api/publish-message)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.