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

> Abonniere einen aktiven Chat-Stream für KI-Antworten in Echtzeit

Abonniere einen aktiven Chat-Stream, um KI-Antworten in Echtzeit zu erhalten. Das ist eine Alternative zum Polling über den [Get Status](/docs/de/api/get-status)-Endpoint.

## Anfrage

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

### Header

| Header | Erforderlich | Beschreibung |
| - | - | - |
| `Authorization` | Ja | `Bearer macaly_...` |

### Pfadparameter

| Parameter | Typ | Beschreibung |
| - | - | - |
| `chatId` | string | Die von `POST /api/chat` zurückgegebene Chat-ID |

### Beispielanfrage

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

## Antwort

Gibt einen Server-Sent-Events-Stream (SSE) mit dem Verarbeitungsfortschritt und den KI-Antworten zurück.

### Stream-Format

Der Stream nutzt das [Vercel AI SDK v4](https://ai-sdk.dev/docs/ai-sdk-ui/stream-protocol#data-stream-protocol) Data-Stream-Protokoll. Jeder Chunk ist mit einem Typ-Präfix versehen:

| Präfix | Beschreibung |
| - | - |
| `0:` | Textinhalt |
| `2:` | Datenobjekte (wie `{ chatId }`) |
| `8:` | Anmerkungen und Metadaten |

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

## Statuscodes

| Status | Beschreibung |
| - | - |
| 200 | Erfolgreich (Streaming-Antwort) |
| 204 | Kein aktiver Stream (Verarbeitung abgeschlossen oder nicht gestartet) |
| 401 | Ungültiger oder fehlender API-Schlüssel |
| 403 | Chat gehört nicht zu deinem Team |
| 404 | Chat nicht gefunden |
| 429 | Anfragelimit erreicht |

## Hinweise

* Der Stream endet, wenn die Verarbeitung abgeschlossen ist
* Rufst du diesen Endpoint auf, nachdem die Verarbeitung beendet ist, erhältst du eine `204 No Content`-Antwort
* Für einfachere Implementierungen kannst du stattdessen den [Get Status](/docs/de/api/get-status)-Endpoint per Polling abfragen


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