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

# Real-time account events

> StreamAccountEvents - one connection that sends the state of your accounts, then every order, fill, balance and position change as it happens.

`StreamAccountEvents` · **read key**

One long-lived connection that sends the state of every account the key reaches, then each change as it happens: orders, cancels, fills, balance and positions.
It replaces polling [GetAccountSnapshot](https://hypernova.xyz/docs/api/accounts.md#getaccountsnapshot), [ListOpenOrders](https://hypernova.xyz/docs/api/orders.md#listopenorders) and [ListTrades](https://hypernova.xyz/docs/api/history.md#listtrades).
Demo accounts are not on the stream.

A reader that follows this page, in Python and in Node.js, is in [hypernova-core/hypernova-api](https://github.com/hypernova-core/hypernova-api).

> [!NOTE]
>
> **Enabled per user during the pilot.** If a connect ends with `permission_denied` and the kind `EVENT_STREAM_DISABLED`, your key is fine and the stream is not turned on for you yet. Ask us in the [Hypernova Discord](https://discord.gg/PBvZbvRTrN).

## The connection

This is the one endpoint that is not a plain JSON call: the request, and every message of the response, is a frame.

```
POST https://hypernova.xyz/api/v1/StreamAccountEvents
Content-Type: application/connect+json
Authorization: Bearer hn_live_…
```

A frame is five bytes followed by a JSON object:

| Bytes | Holds | In a first-connect request |
| - | - | - |
| 1 | The flag | `00` |
| 4 | The length of the JSON, big-endian | `00 00 00 02` |
| That many | The JSON object | `{}` |

* **The request** is a single frame with flag `0`. Its JSON is `{}` on a first connect, or `{"resumeToken":"…"}` with the token you last saved from `ready`.
* **The response** answers HTTP 200, then sends frames for as long as the stream is open. Flag `0` is an event. Flag `2` is the last frame: `{}` when the stream ended normally, or an `error` when it did not.
* A request with any other content type answers HTTP 415 and no frame. A compressed request, and a request field the endpoint does not define, are refused in the last frame, as `unimplemented` and `invalid_argument`.

To watch a stream from a terminal, in bash or zsh:

```bash theme={null}
printf '\x00\x00\x00\x00\x02{}' | curl -sS -N -X POST "https://hypernova.xyz/api/v1/StreamAccountEvents" \
  -H "Authorization: Bearer $HN_API_KEY" \
  -H "Content-Type: application/connect+json" \
  --data-binary @- --output -
```

curl prints each frame as it arrives, header bytes included, so it shows you the stream but is not a client.
`--output -` is needed because the frames are binary, and curl otherwise refuses to print them to a terminal.
A connect that the stream itself refuses, as for a bad key, still answers HTTP 200, with one last frame:

```json theme={null}
{ "error": { "code": "unauthenticated", "message": "user is unauthenticated" } }
```

## Events

Every event is a JSON object with exactly one of the members below, and an `accountId` beside it unless the event concerns the whole connection.

| Member | Carries | How to apply it |
| - | - | - |
| `account` | The account, as [ListAccounts](https://hypernova.xyz/docs/api/accounts.md#listaccounts) returns it. Sent once per account at connect, and not again while the stream is open. | These are the accounts the connection covers. |
| `balance` | The balance, as [GetAccountSnapshot](https://hypernova.xyz/docs/api/accounts.md#getaccountsnapshot) has it. | Replace the account's last balance. |
| `positions` | The whole list of open positions, in `positions.positions`. | Replace the account's whole list. It is never a change to add or subtract. |
| `openOrders` | What [ListOpenOrders](https://hypernova.xyz/docs/api/orders.md#listopenorders) returns, in `openOrders.orders`. Sent at connect only. | Replace your open orders for the account. The list holds at most 1,000, so an order missing from it is not proven closed. |
| `order` | One order. | Insert or update by `orderId`. A finished order never reopens, so ignore a later OPEN for it. A cancelled order can arrive here with no reason. |
| `orderCancelled` | `orderId` and `reason`, an [OrderCancelReason](https://hypernova.xyz/docs/api/enumerations.md#ordercancelreason). | Take the order out of your open orders. A later OPEN for it is stale. |
| `trade` | One fill, as [ListTrades](https://hypernova.xyz/docs/api/history.md#listtrades) returns it. | Insert once by `tradeId`. Never add it to positions or balance yourself. |
| `ready` | `resumeToken` and `tradesComplete`. | Apply everything held since the connection opened. See [Resuming](#resuming). |
| `heartbeat` | Nothing. Sent every 20 seconds, so a quiet stream can be told from a dead one. | Restart your 45-second silence timer. |

The JSON of successive frames on a first connect, with nested objects shortened to `{ … }`:

```text theme={null}
{"accountId":"019f6a2e-41c8-7a1d-9b3e-5c2f8d1a7e40","account":{ … }}
{"accountId":"019f6a2e-41c8-7a1d-9b3e-5c2f8d1a7e40","balance":{ … }}
{"accountId":"019f6a2e-41c8-7a1d-9b3e-5c2f8d1a7e40","positions":{"positions":[ … ]}}
{"accountId":"019f6a2e-41c8-7a1d-9b3e-5c2f8d1a7e40","openOrders":{"orders":[ … ]}}
{"ready":{"resumeToken":"…","tradesComplete":false}}
{"accountId":"019f6a2e-41c8-7a1d-9b3e-5c2f8d1a7e40","order":{ … }}
{"accountId":"019f6a2e-41c8-7a1d-9b3e-5c2f8d1a7e40","trade":{ … }}
{"heartbeat":{}}
```

* Balance and positions are sampled, so they can lag a fill: a position can still show its old size just after the trade that closed it.
* A live `order` or `trade` event can be missing or repeated, and the kinds are not ordered against each other: a fill can arrive before its order update.
* Two OPEN updates for one order can arrive in either order. When they conflict, read [ListOpenOrders](https://hypernova.xyz/docs/api/orders.md#listopenorders) before acting on either.
* [ListOrderHistory](https://hypernova.xyz/docs/api/orders.md#listorderhistory) settles an order whose final update never came. It can list an order as FILLED while the fill is still being booked, and that row can still turn REJECTED or CANCELLED, so confirm an execution with a trade that carries the same `orderId`.

## How a connection starts

A connection always begins the same way:

1. One `account` event for every account the key reaches.
2. `balance`, `positions` and `openOrders` for each account that is active or suspended. Any other account, such as one that has passed or failed, sends no snapshot, but step 3 still replays its fills, its closing trade included, so keep reading its `trade` events.
3. Any fills recorded since the token in the request, as `trade` events.
4. `ready`.

Hold everything before `ready` aside and apply it together when `ready` arrives.
If the connection ends before `ready`, discard what you held and keep your previous state, marked stale.

After `ready` the stream is live.
`order`, `orderCancelled` and `trade` are sent as they happen.
`balance` and `positions` are checked once a second and sent only when they changed, so an account with no open position is silent apart from heartbeats.
Live events are sent only for the accounts that were active or suspended when the connection opened.

## Resuming

`ready` tells you how to continue:

* Save `resumeToken` once you have applied the snapshot, and send it on your next connect. The fills recorded since then are replayed before that connection's `ready`.
* `tradesComplete` is `true` when the replay covered every recorded fill since your token. It is `false` on a first connect, when the token is more than an hour old, when more than 1,000 fills were recorded since it, or when trade history could not be read in time. When it is `false`, page [ListTrades](https://hypernova.xyz/docs/api/history.md#polling-for-new-trades) for each account before you save the token.
* A replay reaches back a minute past your token on purpose, so a fill can arrive twice. Keep only the first copy of each `tradeId`.

## Reconnecting

A stream does not stay open indefinitely, so a client is a loop:

* We end every stream after five to six minutes, with a last frame of `{}`. Reconnect at once with your saved token.
* A stream can also stop with no last frame, for example during a deploy on our side. Treat it the same way.
* If nothing arrives for 45 seconds, not even a heartbeat, treat the connection as dead and reconnect.
* On an `error` in the last frame, act on its `code` as below.

## Stream errors

Check the HTTP status before you decode any frame.
A connect refused before the stream starts answers a status other than 200 with a body that is not a frame: 415 with a plain-text body for a content type other than `application/connect+json`, and the edge's 429 and 403 from [Refusals that carry no error body](https://hypernova.xyz/docs/api/conventions.md#refusals-that-carry-no-error-body).
A reader that feeds such a body to its frame decoder loses the actual error.
On a stream that answered 200, an error arrives in the last frame, never as the HTTP status.
The frame's `metadata` holds what a plain call would send as response headers, each as a list of strings: the kind in `X-Hn-Error-Kind`, and `Retry-After` where the table says so.
Match those names without regard to case.

| `code` | Kind | What it means | What to do |
| - | - | - | - |
| `unauthenticated` | | The key is unknown, revoked or switched off. An open stream has its key checked again about every 20 seconds and ends this way at the first check we can complete. If that check cannot be completed, the stream runs on until its five to six minutes are up, and the reconnect is refused. | Stop until you have a new key. |
| `unauthenticated` | `ACCOUNT_REQUIRED` | The key's owner has never held an Assessment or funded account. | Stop. The key is fine, so do not rotate it. |
| `permission_denied` | `EVENT_STREAM_DISABLED` | The key is valid and the stream is not enabled for its owner. | Stop, and ask us to enable it. |
| `resource_exhausted` | `TOO_MANY_STREAMS` | You are at the cap on open streams. | Close one, or wait and retry. |
| `resource_exhausted` | | More connects than the limit allows. `metadata` carries `Retry-After` in seconds. | Wait that long, then reconnect. |
| `failed_precondition` | | The key reaches no Assessment or funded account. | Stop. |
| `unavailable` | `ACCOUNT_SNAPSHOT_UNAVAILABLE` | One account's state could not be read. `metadata` names it in `X-Hn-Account-Id`. | Reconnect with backoff. |
| `unavailable` | | A dependency of ours is unreachable. | Reconnect with backoff and jitter. |
| `deadline_exceeded` | | Reading your accounts at connect took longer than ten seconds. | Reconnect with backoff. |
| `internal` | | A fault on our side. | Reconnect with backoff, and tell us if it persists. |

Events cost nothing against any limit, and opening a stream draws on a budget of its own, given under [Rate limits](https://hypernova.xyz/docs/api/conventions.md#rate-limits).
A client that stops without closing its connection can hold its place under the stream cap until that stream's five to six minutes are up.


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