# OHLCV candles API

> Open, high, low, close and volume for a token or a pool, from 1 minute to 1 day, built from every indexed swap.

## Get a token's candles

[`GET /tokens/:chain/:address/candles`](https://app.aurelia.build/docs/token-candles) answers from the pool that charts the token, and names that pool. A pool's own candles are at [`GET /pools/:chain/:address/candles`](https://app.aurelia.build/docs/pool-candles).

Hourly candles in USD:

```bash
curl -s -H 'Authorization: Bearer <key>' 'https://app.aurelia.build/api/tokens/solana/So11111111111111111111111111111111111111112/candles?res=1h&currency=usd'
```

| Parameter | Values | Default |
|---|---|---|
| `res` | `1m`, `5m`, `15m`, `1h`, `4h`, `1d` | `1m` |
| `from`, `to` | Unix seconds, at most 5,000 bars apart | The last 500 bars, up to now |
| `currency` | `usd`, or `quote` for the pool's quote token | `usd` |
| `fill` | `1` repeats the previous close through empty intervals | Off |

## What a bar holds

| Field | What it is |
|---|---|
| `t` | The bar's start, unix seconds |
| `o`, `h`, `l`, `c` | Open, high, low and close |
| `v` | Volume in USD |
| `n` | Number of trades: the priced ones in `usd`, all of them in `quote` |

## Draw a chart

The bars map directly onto a candlestick series in a charting library. A price history is the bars' closes.

Bars for a candlestick chart:

```javascript
const res = await fetch(
  "https://app.aurelia.build/api/tokens/solana/So11111111111111111111111111111111111111112/candles?res=1h",
  { headers: { Authorization: "Bearer <key>" } },
);
const { bars } = await res.json();
const candles = bars.map(({ t, o, h, l, c }) => ({ time: t, open: o, high: h, low: l, close: c }));
```

## Keep the last bar live

1. Subscribe to the [candles channel](https://app.aurelia.build/docs/live-candles) with `token` or `pool`, `res` and `currency`, and wait for `subscribed`, which names the pool.
2. Load the bars so far from `GET /pools/:chain/:address/candles` for that pool.
3. Apply each `candle` message by its `t`: it replaces the bar with that `t`, or adds it.

Subscribe:

```json
{"op":"subscribe","id":"c","channel":"candles","chain":"solana","token":"So11111111111111111111111111111111111111112","res":"1m","currency":"usd"}
```

## Related

- [Token candles reference](https://app.aurelia.build/docs/token-candles).
- [Live candles](https://app.aurelia.build/docs/live-candles): ordering and gaps.
- [Token price API](https://app.aurelia.build/guides/token-price-api): the latest price alone.
