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

# Abonneren op stream

> Abonneer u op een actieve chatstream voor realtime AI-antwoorden

Abonneer u op een actieve chatstream om realtime AI-antwoorden te ontvangen. Dit is een alternatief voor het opvragen van het endpoint [Status ophalen](/docs/nl/api/get-status).

## Verzoek

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

### Headers

| Header          | Vereist | Beschrijving        |
| --------------- | ------- | ------------------- |
| `Authorization` | Ja      | `Bearer macaly_...` |

### Padparameters

| Parameter | Type   | Beschrijving                                          |
| --------- | ------ | ----------------------------------------------------- |
| `chatId`  | string | De chat-ID die is geretourneerd door `POST /api/chat` |

### Voorbeeldverzoek

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

## Respons

Retourneert een Server-Sent Events (SSE)-stream met workflowvoortgang en AI-antwoorden.

### Streamformaat

De stream gebruikt het data stream-protocol van de [Vercel AI SDK v4](https://ai-sdk.dev/docs/ai-sdk-ui/stream-protocol#data-stream-protocol). Elke chunk heeft een typeprefix:

| Prefix | Beschrijving                       |
| ------ | ---------------------------------- |
| `0:`   | Tekstinhoud                        |
| `2:`   | Data-objecten (zoals `{ chatId }`) |
| `8:`   | Annotaties en metadata             |

### Voorbeeldstream

```
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."
```

## Statuscodes

| Status | Beschrijving                                                |
| ------ | ----------------------------------------------------------- |
| 200    | Succes (streamingrespons)                                   |
| 204    | Geen actieve stream (workflow voltooid of nog niet gestart) |
| 401    | Ongeldige of ontbrekende API-sleutel                        |
| 403    | Chat behoort niet tot uw team                               |
| 404    | Chat niet gevonden                                          |
| 429    | Limiet bereikt                                              |

## Opmerkingen

* De stream stopt wanneer de workflow is voltooid
* Als u dit endpoint aanroept nadat de workflow is afgelopen, krijgt u een `204 No Content`-respons
* Voor eenvoudigere implementaties kunt u overwegen het endpoint [Status ophalen](/docs/nl/api/get-status) te gebruiken
