Open the app

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.

PackageVersionContract versionRuntime dependencies
@agora/contract0.1.20.1.2zod

The client

ts
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:

OptionWhat it does
fetchThe fetch to use. Defaults to the global one.
credentialsPassed to fetch, for a deployment that keeps a session cookie.
headersHeaders sent with every request.
fixtureA fixture scenario sent with every request, as X-Fixture.
fixtureHeaderThe 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:

ts
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:

ExportWhat it is
settled(status)true once a transaction status is confirmed, finalized, failed or expired.
SettlementEventThe schema of one settlement message on the live stream.
SCENARIOSThe fixture scenario names.
CONTRACT_VERSIONThe contract's version.
U64_MAX18446744073709551615n.
errorsThe 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.