API

API overview

The read-only /api/v1 API: base URL, caching, units, paging and errors.

The API serves everything the engine writes to the database, plus live on-chain reads. It is read-only, except for two admin calls. It holds no keys and sends no transactions.

It is a single framework-free handler (lib/server/api.ts) served by Next.js at app/api/v1/[...path]/route.ts, and optionally by the engine itself.

Base URL

text
https://<your-site>/api/v1

The app calls NEXT_PUBLIC_API_URL + "/api/v1", which is the same origin unless that build-time variable points elsewhere.

Conventions

MethodsGET for reads, POST for the two admin calls, OPTIONS answers 204
CORSaccess-control-allow-origin: *, methods GET, POST, OPTIONS, headers content-type, authorization
UnitsAmounts are in the asset's own units; USD is USD; times are ISO 8601 strings (history points use epoch milliseconds)
Rate limitingNone in code. Edge caching is the protection: a crowd watching costs about one database read per second

Caching

Each endpoint has a cache lifetime (s-maxage). On Vercel the response carries:

text
cache-control: public, max-age=0, s-maxage=<maxAge>, stale-while-revalidate=<5 × maxAge>

Errors and admin calls are no-store. Lifetimes range from 1 s (open orders) to 60 s (config). Some reads are also cached inside each server instance: the HYPE price 5 s, market names 10 min, on-chain reads 10 s, the Montra ladder 12 s.

Paging

List endpoints that can grow without bound (/vault/fills, /ledger) are paged by id, newest first:

QueryTypeDefault
limitinteger 1–50050
cursorpositive integer: returns rows with id < cursornone
market, strategy, kindfilters (≤ 40 characters)none
sinceepoch milliseconds (fills only)none
ts
interface Page<T> { items: T[]; nextCursor: string | null }   // nextCursor is null on the last page

Errors

Every error has the same shape:

json
{ "error": { "code": "DATABASE_NOT_CONFIGURED", "message": "…" } }
HTTPCodeWhen
400VALIDATION_ERRORBad query parameters or admin body
401UNAUTHORIZEDWrong admin token
403ADMIN_DISABLEDADMIN_TOKEN not set, or shorter than 24 characters
404NOT_FOUNDUnknown route, strategy or Montra market
503DATABASE_NOT_CONFIGUREDThe endpoint needs the database and DATABASE_URL is not set
502UPSTREAM_ERRORA data source did not answer (database, RPC, upstream API)

Endpoints

Mock data

To see the app full of data without a database or an engine, open /app?mock=1 (?mock=off to leave; also ?mock=paper, halted, offline). A simulated engine in the browser answers the app's API calls: it replays 30 days of trading against real price history, then keeps trading live prices, starting from $50,000. Market data stays live.

Danger

Mock data is invented and nothing on screen says so. It only exists in the visitor's browser tab. Never set NEXT_PUBLIC_MOCK_DATA=1 on the public site: it would show invented numbers to everyone.