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

# Subscribe to stream

> Sottoscrizione allo stream di una chat attiva per ricevere le risposte dell'IA in tempo reale

Sottoscrivi lo stream di una chat attiva per ricevere le risposte dell'IA in tempo reale. È un'alternativa al controllo periodico dell'endpoint [Get Status](/docs/it/api/get-status).

## Richiesta

```
GET /api/chat/{chatId}/subscribe
```

### Header

| Header | Obbligatorio | Descrizione |
| - | - | - |
| `Authorization` | Sì | `Bearer macaly_...` |

### Parametri del percorso

| Parametro | Tipo | Descrizione |
| - | - | - |
| `chatId` | string | L'ID della chat restituito da `POST /api/chat` |

### Esempio di richiesta

<CodeGroup>
  ```bash cURL theme={null}
  curl -N https://www.macaly.com/api/chat/abc123/subscribe \
    -H "Authorization: Bearer macaly_abc123..."
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch(
    `https://www.macaly.com/api/chat/${chatId}/subscribe`,
    {
      headers: {
        'Authorization': 'Bearer macaly_abc123...',
      },
    }
  );

  if (response.status === 204) {
    console.log('No active stream');
    return;
  }

  const reader = response.body.getReader();
  const decoder = new TextDecoder();

  while (true) {
    const { done, value } = await reader.read();
    if (done) break;

    const chunk = decoder.decode(value);
    for (const line of chunk.split('\n')) {
      if (line.startsWith('0:')) {
        // Text content
        const text = JSON.parse(line.slice(2));
        process.stdout.write(text);
      }
    }
  }
  ```

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

  response = requests.get(
      f'https://www.macaly.com/api/chat/{chat_id}/subscribe',
      headers={
          'Authorization': 'Bearer macaly_abc123...',
      },
      stream=True,
  )

  if response.status_code == 204:
      print('No active stream')
  else:
      for line in response.iter_lines():
          if line:
              line = line.decode('utf-8')
              if line.startswith('0:'):
                  import json
                  text = json.loads(line[2:])
                  print(text, end='')
  ```
</CodeGroup>

## Risposta

Restituisce uno stream Server-Sent Events (SSE) con l'avanzamento dell'elaborazione e le risposte dell'IA.

### Formato dello stream

Lo stream usa il protocollo data stream di [Vercel AI SDK v4](https://ai-sdk.dev/docs/ai-sdk-ui/stream-protocol#data-stream-protocol). Ogni blocco è preceduto da un indicatore di tipo:

| Prefisso | Descrizione |
| - | - |
| `0:` | Contenuto testuale |
| `2:` | Oggetti dati (come `{ chatId }`) |
| `8:` | Annotazioni e metadati |

### Esempio di stream

```
2:[{"chatId":"abc123"}]
0:"I'll create a landing page for you."
0:" Let me start by setting up the project structure."
8:{"type":"tool-call","toolName":"WriteFile",...}
0:" Done! Your landing page is ready."
```

## Codici di stato

| Codice | Descrizione |
| - | - |
| 200 | Operazione riuscita (risposta in streaming) |
| 204 | Nessuno stream attivo (elaborazione completata o non avviata) |
| 401 | Chiave API non valida o mancante |
| 403 | La chat non appartiene al tuo team |
| 404 | Chat non trovata |
| 429 | Limite di richieste raggiunto |

## Note

* Lo stream termina quando l'elaborazione è completata
* Se chiami questo endpoint dopo la fine dell'elaborazione, ricevi una risposta `204 No Content`
* Per implementazioni più semplici, valuta invece di seguire l'avanzamento tramite l'endpoint [Get Status](/docs/it/api/get-status)


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