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

# Charts

> Render OHLCV candlestick token charts with the Codex API, from historical bars to real-time updates over WebSockets as new trades land.

In this recipe we'll show you how to use the Codex api to render a token chart, complete with OHLCV data, and more.

This data powers the charts on [defined.fi](https://www.defined.fi/token/bsc/0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c).

![Chart](https://mintcdn.com/codex-dfdf2708/sIg_rUgIrhUCd0wd/images/defined-chart.png?fit=max\&auto=format\&n=sIg_rUgIrhUCd0wd\&q=85\&s=bc8761cd7434092ed160402ad0d5c1fb)

## Fetch

Start by implementing the following query to fetch the OHLCV data for a given token.

[Test this query in the Explorer →](https://docs.codex.io/explore.md)

```graphql theme={null} theme={null}
query ChartData {
  getBars(
    symbol: "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c:56"
    from: 1750353512
    to: 1750439882
    resolution: "1"
  ) {
    o
    h
    l
    c
    t
    volume
  }
}
```

## Render

```json Sample Response expandable theme={null}
{
  "data": {
    "getBars": {
      "o": [
        639.962297445,
        639.948026102,
      ],
      "h": [
        640.343357275,
        640.081660228,
      ],
      "l": [
        639.795898952,
        639.914833559,
      ],
      "c": [
        639.948026102,
        639.983598149,
      ],
      "t": [
        1750353300,
        1750353600,
      ],
      "volume": [
        "577495.066850454",
        "302268.420570785",
      ]
    }
  }
}
```

The result uses the "response-as-a-table" pattern, which means you can use the `o`, `h`, `l`, `c`, `t`, and `volume` fields to render the chart. This is adapted from the TradingView documentation, and is intended to be used with their charting library [here](https://www.tradingview.com/charting-library-docs/latest/connecting_data/UDF/#response-as-a-table-concept).

## Realtime updates

Now that we have all the data we need to render a chart, we can use the [onTokenBarsUpdated](https://docs.codex.io/api-reference/subscriptions/ontokenbarsupdated.md) subscription to get realtime updates for all pairs of a given token.

Note, you can also use [onBarsUpdated](https://docs.codex.io/api-reference/subscriptions/onbarsupdated.md) to get realtime updates for a specific pair.

[Test this query in the Explorer →](https://docs.codex.io/explore.md)

```graphql theme={null} theme={null}
subscription OnTokenBarsUpdated {
  onTokenBarsUpdated(tokenId: "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c:56") {
    aggregates {
      r1 {
        usd {
          o
          h
          l
          c
          t
          volume
        }
      }
    }
  }
}
```

After receiving the realtime updates, you can use the `o`, `h`, `l`, `c`, `t`, and `volume` fields to update the chart.

## Confirmed vs. Unconfirmed data

The bars subscriptions accept an optional `commitmentLevel` argument. Omitting it gives you `confirmed` behavior by default.

* **`confirmed`** (default): best for charts where accuracy matters more than latency, like historical analysis or finalized trade data.
* **`processed`**: best for live trading UIs and sniper bots, where the lowest possible latency matters more than perfect accuracy. Processed events may later be reorged out.

**`processed` is available on Solana, Base, and Robinhood.** On Base it returns Flashblocks updates.

For the lowest latency on `processed` bars, connect to the [edge node](https://docs.codex.io/concepts/edge-nodes.md) for your network.

> [!NOTE]
>
> Event subscriptions (not bars) use a separate [`EventCommitmentLevel`](https://docs.codex.io/api-reference/enums/eventcommitmentlevel) with its own values — `Confirmed` and `Processed`. See [Commitment levels](https://docs.codex.io/concepts/subscriptions.md#commitment-levels) on the Subscriptions page for the tradeoffs.

## Aggregated Charts

Create token charts with aggregated data across all valid pairs with [getTokenBars](https://docs.codex.io/api-reference/queries/gettokenbars.md). Note that this data has limited historical data, back to timestamp `1753121580`.

For real-time aggregate charts, subscribe to [onTokenBarsUpdated](https://docs.codex.io/api-reference/subscriptions/ontokenbarsupdated.md).

> [!TIP]
>
> You can refer to this [datafeed](https://gist.github.com/bradens/bfe449f8ea88fca8a1952cfe242b5e21) example using the SDK for `onTokenBarsUpdated` to get you started with a chart rendering subscription.

Check out all of our charting queries and subscriptions in the [api reference](https://docs.codex.io/api-reference/introduction.md)

* [onTokenBarsUpdated](https://docs.codex.io/api-reference/subscriptions/ontokenbarsupdated.md)
* [onBarsUpdated](https://docs.codex.io/api-reference/subscriptions/onbarsupdated.md)
* [getBars](https://docs.codex.io/api-reference/queries/getbars.md)
* [getTokenBars](https://docs.codex.io/api-reference/queries/gettokenbars.md)


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