# For AI agents

Everything an agent needs to call the Chainmetric API, on one page.

## Basics

- Base URL: `https://api.chainmetric.io`.
- Auth: send `Authorization: <key>`, the bare key.
- `Authorization: Bearer <key>` is refused with `403` at the edge. Do not add `Bearer`.
- Chain ids: Ethereum `1`, BNB Chain `56`, Base `8453`, Solana `1399811149`.
- EVM addresses: 40 hex characters, lowercase, no `0x`. Solana addresses: base58, case-sensitive.
- Every answer is JSON in one envelope; the document is in `data`.
- A value that cannot be computed exactly is `null`, never `0`.
- The full spec is at [/openapi.json](/openapi.json). Every page is also Markdown at `<page>.md`.

## From question to route

| Question | Route |
|---|---|
| Price, market cap, volume, holders of a token | `GET /v2/token/{chain_id}/{address}/stats` |
| Recent buys and sells of a token | `GET /v2/token/{chain_id}/{address}/trades` |
| Price history or a chart | `GET /v2/token/{chain_id}/{address}/ohlcv?interval=1h` |
| Liquidity added or removed | `GET /v2/token/{chain_id}/{address}/liquidity` |
| Top holders of a token | `GET /v2/token/{chain_id}/{address}/positions` |
| Who funded a wallet | `GET /v2/wallet/{chain_id}/{address}/funding?hops=root` |
| What a wallet holds of one token | `GET /v2/wallet/{chain_id}/{wallet}/position/{token}` |
| What launched just now | `GET /v2/pilot/trenches/{chain_id}?view=launches` |
| Live trades, resumable | `GET /v2/stream/token/{chain_id}/{address}/replay` |

Trades, liquidity and holders answer on EVM chains; Solana support for them is in progress.

## Errors and retries

- Every error is `{"error": "...", "message": "...", "statusCode": 400}`.
- `400`: the request is wrong. Fix it; do not retry unchanged.
- `401`: the key is missing, unknown or revoked. `404`: unknown chain, token or resource.
- `429`: the key's rate limit is reached. Back off exponentially and retry.
- `503`: busy or catching up. Wait `Retry-After` seconds when sent, then retry.
- `500`, `502`, `504`: retry with exponential backoff, at most a few times.

## Streams

- `/v2/stream/...` routes are server-sent events: `event:` and `data:` lines, one JSON envelope per event.
- Resumable streams send `id:` lines. Reconnect with header `Last-Event-ID: <last id>` to resume.
- The trades and liquidity replay streams also start from history with `from_block` or `from_seq`.
- A `: keepalive` comment arrives every 20 seconds.

## Three calls

```sh
curl -s -H "Authorization: $CHAINMETRIC_KEY" https://api.chainmetric.io/v2/token/1/c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2/stats
curl -s -H "Authorization: $CHAINMETRIC_KEY" "https://api.chainmetric.io/v2/token/1/c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2/trades?limit=3"
curl -s -H "Authorization: $CHAINMETRIC_KEY" "https://api.chainmetric.io/v2/pilot/trenches/1399811149?view=curves&limit=3"
```
