# Crypto WebSocket API

> One socket for every chain and channel. Open it once, then subscribe to as many things as your plan allows.

## Connect and subscribe

Open `wss://app.aurelia.build/api/live` with your key in the URL as `apiKey`, because browsers cannot set headers on a WebSocket. Each subscription has an id you choose, and every message for it carries that id.

Follow a token's trades:

```javascript
const ws = new WebSocket("wss://app.aurelia.build/api/live?apiKey=<key>");
ws.onopen = () => {
  ws.send(JSON.stringify({ op: "subscribe", id: "sol", channel: "trades", chain: "solana", token: "So11111111111111111111111111111111111111112" }));
};
ws.onmessage = (e) => {
  const m = JSON.parse(e.data);
  if (m.type === "trades") for (const t of m.data) console.log(t.side, t.amount, t.priceUsd);
};
```

## Channels

| Channel | Follows | Read |
|---|---|---|
| `trades` | Every swap of a token, a pool or a wallet | [Trades](https://app.aurelia.build/docs/live-trades) |
| `candles` | The bar in progress, then each bar's final values | [Candles](https://app.aurelia.build/docs/live-candles) |
| `token`, `pool` | A token's or pool's page, and the price between updates | [Token and pool](https://app.aurelia.build/docs/live-token) |
| `holders` | A token's 100 largest holders | [Holders](https://app.aurelia.build/docs/live-holders) |
| `balances`, `positions` | A wallet's holdings and PnL | [Balances and positions](https://app.aurelia.build/docs/live-wallets) |
| `pulse` | New launches, curves and graduations | [Pulse lists](https://app.aurelia.build/docs/live-pulse) |

## Recover from gaps

Nothing is replayed. After a `resubscribed` message, what was indexed during the outage was not sent: snapshot channels send their snapshot again, and for trades and candles you read the recent ones over REST and merge them.

## Errors and closing

- An `error` message ends that subscription only. The socket stays open.
- `4401`: the key was refused. `4402`: your credits ran out, so add credit and connect again.
- Unsubscribe with `{"op":"unsubscribe","id":"sol"}`. It is free.

## What it costs

Opening a socket costs 1 credit, each subscription 1, and each data message 0.01. A free account gets 5 sockets of 25 subscriptions each, and a paid one 5 sockets of 100. See [authentication and limits](https://app.aurelia.build/docs/auth).

## Related

- [WebSocket reference](https://app.aurelia.build/docs/live).
- [DEX trades API](https://app.aurelia.build/guides/dex-trades-api).
- [OHLCV candles API](https://app.aurelia.build/guides/ohlcv-candles-api).
