# StonkAPI for agents

This guide is for AI agents that call [StonkAPI](https://stonkapi.xyz), a JSON API for quote-paired token markets on Solana. Human and HTML docs: https://stonkapi.xyz/docs. Markdown: https://stonkapi.xyz/docs.md. OpenAPI: https://stonkapi.xyz/openapi.json. Full reference: https://stonkapi.xyz/llms-full.txt.

If you are working on the StonkAPI codebase itself, use the repository root `AGENTS.md` instead.

## Auth

Every /v1 route needs an API key, sent as Authorization: Bearer sa_... or X-StonkAPI-Key. Get one at /signup. Browser requests must come from an allowlisted Origin (stonkapi.xyz is always allowed). Server-side calls need no Origin. Key changes (revoke, rotate, domains) can take up to 60 seconds to apply.

Do not put the key in a query string, a page URL, or a log. For browser calls, allowlist the page origin on the key; server-side calls need no Origin. stonkapi.xyz is always allowed.

```bash
curl https://stonkapi.xyz/v1/health \
  -H "Authorization: Bearer sa_YOUR_KEY"
```

## Endpoints

Only GET, HEAD, OPTIONS. Other methods return a JSON 405. Unknown /v1 paths return a JSON 404.

- `GET /v1/trending` — Jupiter 1h trending, hydrated with stonkfun token data. Listing stonks only. Key required. Thin if Jupiter has few listing hits.
- `GET /v1/leaders` — Current leader per quote pair: highest market cap among the top 500 stonkfun tokens. count is null (stonkfun has no per-pair counts). Key required.
- `GET /v1/pairs` — Quote catalog, live from stonkfun /pairs. Category is crypto, stock, stablecoin, meme, etf, or lst. etf is assigned in /admin (may be empty). Sector optional. count is null; use /v1/pairs/{mint} total for a pair's token count.
- `GET /v1/pairs/{mint}` — Tokens on one quote, live from stonkfun. Newest first, 200 rows by default, with total (the full upstream count) and pair. page/pageSize (default 25, max 100) page upstream. Unknown pair = 404.
- `GET /v1/tokens` — Newest listing stonks, live from stonkfun. 200 rows by default, with no total. page/pageSize (default 25, max 100) page through all tokens; total is then included.
- `GET /v1/tokens/{mint}` — One listing stonk by CA, live from stonkfun. Unknown or unlisted mint = 404.
- `GET /v1/runners?quote={quote_mint}` — 1h runner score per quote mint. Missing or invalid quote = 400. Empty if liq under 2 thousand USD or under 20 trades. sort=score or volume.
- `GET /v1/verified` — Jupiter verified list, merged with StonkAPI history. Listing stonks only. dashboard is a Jupiter URL. Market fields are present when the row is hydrated from stonkfun.
- `GET /v1/stocks` — Stock quote assets (xStock, PreStock, Sunrise, Backpack). No per-pair count field; use /v1/pairs/{mint} total.
- `GET /v1/projects` — Curated project pairs (StonkAPI list). Count is live tokens on that pair from stonkfun.
- `GET /v1/community` — Community Mode launches: live scan of recent, top-volume and top-cap stonkfun tokens, merged with StonkAPI history.
- `GET /v1/rewards` — Reward 1 (100 bps) and Reward 3 (300 bps) plus distributed total, from stonkfun reward launches merged with StonkAPI history. mode is reward; reward is Reward 1 or Reward 3.
- `GET /v1/dex` — Paid profiles and boosts, merged with StonkAPI history. Ads excluded. kinds are Boost and/or Paid. Newest listing first.
- `GET /v1/health` — Upstream freshness from stonkfun.xyz: pair and token counts, upstream generatedAt and age, newest token launch time, cache time, and the last upstream error. No count field. Key required.

Every successful list uses the envelope `{ ok, generatedAt, count, data }`, where `data` is an object keyed by the collection name (for example `data.tokens`). Mint is the full CA on every row; `ca` is the same value. /v1/health has no count field.

## Paging

List routes accept optional page (1-based) and pageSize. Omit both for the default list. On /v1/tokens and /v1/pairs/{mint}, paging goes upstream: default pageSize 25, max 100, and total is the full upstream count. Unpaged /v1/tokens returns 200 rows and no total; unpaged /v1/pairs/{mint} returns up to 200 rows and still includes total and pair. Other lists are sliced from the cached result: default pageSize 50, max 200, and total is the length of that list. Paged responses add page, pageSize and total.

Walk large lists with `page` and `pageSize` rather than requesting unpaged data in a tight loop. The TypeScript client has `paginateTokens` and `paginatePair`.

## Caching and rate etiquette

Market data comes live from stonkfun.xyz's public API. Responses are cached in memory and at the Cloudflare edge for 10 seconds to 10 minutes per route. A once-a-minute cron refreshes the busiest lists so the first request after a cache expires stays fast. If upstream is slow or fails, the last good copy is served with X-Cache: STALE-ERROR and the real X-Data-Age. If there is no copy, the route returns JSON {ok:false, error:'Upstream busy, retry shortly.'} with 503 (when upstream is rate-limited) or 502 (other upstream errors) and a Retry-After header, never an empty-body 500. X-Cache is MEM, EDGE, MISS, SWR, STALE or STALE-ERROR. X-Data-Age is the age of that copy in milliseconds. All StonkAPI traffic shares stonkfun's 60 requests a minute. API keys, admin pair categories, the curated projects list, and history for community, rewards, dex and verified still come from StonkAPI's own store.

StonkAPI does not publish a per-key rate limit. To stay within the shared upstream budget:

- Reuse a response while `X-Cache` is `MEM`, `EDGE`, `SWR`, `STALE` or `STALE-ERROR`, and check `X-Data-Age` (milliseconds).
- Prefer /v1/health to see upstream freshness instead of probing many list routes.
- Do not poll faster than the route's cache time (10 seconds on the busiest token lists, longer on others).
- Runners can be an empty list when the market is thin; that is not an error.

## Errors

Bodies are JSON and include `ok: false` and an `error` string. Typical cases:

- 401 missing: `{"ok":false,"error":"API key required. Get one at /signup."}`
- 401 invalid: `{"ok":false,"error":"Invalid API key."}`
- 403: `{"ok":false,"error":"Domain not allowlisted for this key."}`
- 404 path: `{"ok":false,"error":"Not found. See /docs or /openapi.json."}`
- 404 token: `{"ok":false,"error":"Token not found.","generatedAt":1796000000000,"count":0,"data":{"tokens":[]}}`
- 404 pair: `{"ok":false,"error":"Pair not found.","generatedAt":1796000000000,"count":0,"data":{"tokens":[]}}`
- 400 runners: `{"ok":false,"generatedAt":1796000000000,"count":0,"error":"quote mint required","data":{"runners":[]}}`
- 405: `{"ok":false,"error":"Method POST not allowed. Use GET, HEAD or OPTIONS."}`
- 502: `{"ok":false,"error":"Upstream busy, retry shortly.","generatedAt":1796000000000}`
- 503: `{"ok":false,"error":"Upstream busy, retry shortly.","generatedAt":1796000000000}`

If upstream is busy or failing and no cached copy exists, the API returns JSON 503 (rate-limited) or 502 (other upstream errors) with Retry-After, never an empty-body 500. Treat those as retryable, with backoff. The TypeScript client retries 429, 502, 503 and other 5xx by default, and honours Retry-After.

## TypeScript client

TypeScript client: one zero-dependency file, typed for every /v1 route, with paging params, the {ok, generatedAt, count, data} envelope, typed errors and an apiKey option. Download /sdk/stonkapi.ts (or /sdk/stonkapi.mjs for plain JavaScript) and import it. Works on Node 18+, Bun, Deno, Cloudflare Workers and browsers (allowlist the browser origin on your key). Package source: packages/stonkapi-client, name stonkapi, not yet published to npm.

```typescript
import { StonkAPI, StonkAPIError } from "./stonkapi"

const stonk = new StonkAPI({ apiKey: process.env.STONKAPI_KEY! })

try {
  const { data, count, generatedAt } = await stonk.tokens({ page: 1, pageSize: 25 })
  console.log(count, new Date(generatedAt), data.tokens[0].symbol)
} catch (e) {
  if (e instanceof StonkAPIError) console.error(e.status, e.kind, e.message)
  else throw e
}
```

## Discoverability

- OpenAPI: https://stonkapi.xyz/openapi.json
- API catalog (RFC 9727): https://stonkapi.xyz/.well-known/api-catalog
- Plugin-style manifest: https://stonkapi.xyz/.well-known/ai-plugin.json
- llms.txt: https://stonkapi.xyz/llms.txt

Contact: https://stonkapi.xyz/contact
