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

> Subscribe to an active chat stream for real-time AI responses

Subscribe to an active chat stream to receive real-time AI responses. This is an alternative to polling the [Get Status](/en/api/get-status) endpoint.

## Request

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

### Headers

| Header          | Required | Description         |
| --------------- | -------- | ------------------- |
| `Authorization` | Yes      | `Bearer macaly_...` |

### Path Parameters

| Parameter | Type   | Description                                |
| --------- | ------ | ------------------------------------------ |
| `chatId`  | string | The chat ID returned from `POST /api/chat` |

### Example Request

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

## Response

Returns a Server-Sent Events (SSE) stream with workflow progress and AI responses.

### Stream Format

The stream uses the [Vercel AI SDK v4](https://ai-sdk.dev/docs/ai-sdk-ui/stream-protocol#data-stream-protocol) data stream protocol. Each chunk is prefixed with a type indicator:

| Prefix | Description                      |
| ------ | -------------------------------- |
| `0:`   | Text content                     |
| `2:`   | Data objects (like `{ chatId }`) |
| `8:`   | Annotations and metadata         |

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

## Status Codes

| Status | Description                                          |
| ------ | ---------------------------------------------------- |
| 200    | Success (streaming response)                         |
| 204    | No active stream (workflow completed or not started) |
| 401    | Invalid or missing API key                           |
| 403    | Chat does not belong to your team                    |
| 404    | Chat not found                                       |
| 429    | Rate limited                                         |

## Notes

* The stream will end when the workflow completes
* If you call this endpoint after the workflow has finished, you'll get a `204 No Content` response
* For simpler implementations, consider polling the [Get Status](/en/api/get-status) endpoint instead
