TypeScript client
The @agora/contract package, a typed client for every API route with the schemas the API validates against.
@agora/contract is the API's contract as a package: the zod schemas the API validates with, a client with one method per route, and the fixtures. Responses are parsed with the same schemas, so a change in the API fails at the call instead of deep in your code. It runs in the browser, in server code and in scripts.
| Package | Version | Contract version | Runtime dependencies |
|---|---|---|---|
@agora/contract | 0.1.2 | 0.1.2 | zod |
The client
import { createClient } from '@agora/contract'
const api = createClient(process.env.AGORA_API!, {
headers: { 'X-Fixture-Session': crypto.randomUUID() }, // fixture mode only
})
const config = await api.config()
const instruments = await api.instruments()
const page = await api.settlements({ instrument: 'CALDER', limit: 20 })createClient(baseUrl, options) takes these options, all optional:
| Option | What it does |
|---|---|
fetch | The fetch to use. Defaults to the global one. |
credentials | Passed to fetch, for a deployment that keeps a session cookie. |
headers | Headers sent with every request. |
fixture | A fixture scenario sent with every request, as X-Fixture. |
fixtureHeader | The header name for the scenario, X-Fixture by default. |
Every method maps to one route, named in the client column of the routes table. Reads take the path parameter, then an optional query object: api.instrument('HALDEN31'), api.candles('CALDER', { interval: 'M5' }), api.repos({ wallet }). Builds take the request body: api.buildTakeOffer({ wallet, offerId, units }). api.eventsUrl(query) returns the URL for an EventSource, with the scenario and session carried in the query, since an EventSource can't set headers.
Errors
A failed request throws ApiClientError:
import { ApiClientError } from '@agora/contract'
try {
await api.buildWithdraw({ wallet, amount: '50000000' })
} catch (e) {
if (e instanceof ApiClientError) {
console.log(e.status, e.code, e.body.error.message, e.body.error.programError?.name)
}
}code is the envelope's error.code and body the whole envelope, as listed in API errors. A response that isn't an envelope, such as a proxy's error page, still becomes one, with UPSTREAM_UNAVAILABLE for a 5xx and INTERNAL_ERROR otherwise.
Schemas and helpers
Every schema in the API reference is exported under its name (Account, Instrument, Settlement, ActionBuild, TxStatus and the rest), as a zod schema and as a TypeScript type of the same name. So are the primitives (Address, U64, Timestamp), the request schemas in buildRequests, and:
| Export | What it is |
|---|---|
settled(status) | true once a transaction status is confirmed, finalized, failed or expired. |
SettlementEvent | The schema of one settlement message on the live stream. |
SCENARIOS | The fixture scenario names. |
CONTRACT_VERSION | The contract's version. |
U64_MAX | 18446744073709551615n. |
errors | The error codes with their HTTP statuses (errors.status) and the envelope's schema (errors.ApiError). |
@agora/contract/fixtures exports fixtureWallets (the sample members: holder, lender, issuer and others), fixtureAddress(seed), fixtureSignature(seed), fixtureTransaction(label) and fixtureErrorEnvelopes, for tests against the fixture API. @agora/contract/openapi.json is the OpenAPI document.