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

# Stream Folio

> Stream real-time portfolio updates via Server-Sent Events

# Stream Folio

Opens a Server-Sent Events (SSE) stream that sends the sub-account's full portfolio on connect, then streams incremental updates as positions change.

## Request

```
GET /accounts/:account_id/folio/stream
```

### Path parameters

| Parameter    | Type   | Description        |
| ------------ | ------ | ------------------ |
| `account_id` | string | The sub-account ID |

## Response

The response is a `text/event-stream` with two event types.

### `snapshot` event

Sent immediately on connect. Contains the full folio as a JSON array of `[key, value]` pairs:

```
event: snapshot
data: [["balances/token/base/USDC/0xaded...78ef","100e6"],["prices/token/WETH","3391.3"],...]
```

### `update` event

Sent whenever folio data changes. Contains only the changed key/value pairs:

```
event: update
data: [["balances/token/base/USDC/0xaded...78ef","150e6"],["prices/token/WETH","3400.1"]]
```

### Keepalive

A comment line is sent every 30 seconds to keep the connection alive:

```
: keepalive
```

## Examples

<CodeGroup>
  ```bash cURL theme={null}
  curl -N https://prime-api.legend.xyz/accounts/acc_2o0yiljtp378/folio/stream \
    -H "Authorization: Bearer $LEGEND_QUERY_KEY" \
    -H "Accept: text/event-stream"
  ```

  ```typescript TypeScript theme={null}
  const response = await fetch(
    "https://prime-api.legend.xyz/accounts/acc_2o0yiljtp378/folio/stream",
    {
      headers: {
        Authorization: `Bearer ${LEGEND_QUERY_KEY}`,
        Accept: "text/event-stream",
      },
    }
  );

  const reader = response.body!.getReader();
  const decoder = new TextDecoder();
  let buffer = "";

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

    buffer += decoder.decode(value, { stream: true });
    const lines = buffer.split("\n");
    buffer = lines.pop()!;

    let eventType = "";
    for (const line of lines) {
      if (line.startsWith("event: ")) {
        eventType = line.slice(7);
      } else if (line.startsWith("data: ")) {
        const pairs: [string, string][] = JSON.parse(line.slice(6));
        console.log(`${eventType}: ${pairs.length} pairs`);
      }
    }
  }
  ```

  ```python Python theme={null}
  import httpx
  import json

  url = "https://prime-api.legend.xyz/accounts/acc_2o0yiljtp378/folio/stream"
  headers = {
      "Authorization": f"Bearer {LEGEND_QUERY_KEY}",
      "Accept": "text/event-stream",
  }

  with httpx.stream("GET", url, headers=headers) as response:
      event_type = ""
      for line in response.iter_lines():
          if line.startswith("event: "):
              event_type = line[7:]
          elif line.startswith("data: "):
              pairs = json.loads(line[6:])
              print(f"{event_type}: {len(pairs)} pairs")
  ```

  ```rust Rust theme={null}
  // Stream folio updates via SSE
  let url = format!("/accounts/{}/folio/stream", account_id);
  // Use reqwest with EventSource or the stream_folio helper
  ```
</CodeGroup>

<Note>
  The stream sends all folio data as flat key/value pairs. See the [Folio concepts page](/concepts/folio) for the full key/value reference and value encoding details.
</Note>

## Errors

| Status | Code                | Description           |
| ------ | ------------------- | --------------------- |
| 404    | `account_not_found` | Account doesn't exist |
