TypeScript SDK
@treason/sdk: a small, typed client with safe retries, idempotency and no dependencies.
npm install @treason/sdkNot yet published to npm. From the repository:
npm run sdk:build, thennpm install ./packages/sdk. Requires Node 18 or newer (it uses the built-infetch). It has no runtime dependencies.
#The client
import { Treason } from "@treason/sdk";
const treason = new Treason({
apiKey: process.env.TREASON_API_KEY, // default: TREASON_API_KEY
baseUrl: "https://api.treason.app", // default: TREASON_API_URL, then https://api.treason.app
timeoutMs: 15_000, // per request
maxRetries: 3, // transient failures only
});Pass your own fetch to route through a proxy or to fake the network in tests.
Types for every request and response are generated from the same OpenAPI description the API reference is generated from, so they cannot drift from the API.
#Alerts
| Method | Does |
|---|---|
alerts.create(alert, { idempotencyKey? }) | create or de-duplicate; an idempotency key is attached for you |
alerts.get(id) | the alert, its events, and every notification attempts |
alerts.list({ status, alias, limit, before }) | one page; next is the cursor for before |
alerts.iterate({ status, alias, pageSize }) | every match, as an async iterator |
alerts.ack(id, { user? }) / close / note(id, text) | lifecycle |
alerts.waitFor(id, { until, timeoutMs, pollMs, signal }) | wait for a human; resolves { reached, alert } |
alerts.summary() | counts by status |
page({ ...alert, wait? }) | create, and optionally wait: { acknowledged, timedOut, alert } |
for await (const alert of treason.alerts.iterate({ status: "open" })) {
console.log(alert.tinyId, alert.message);
}#Heartbeats, on call, schedules
await treason.heartbeats.upsert("nightly", { intervalSeconds: 86_400, graceSeconds: 900 }); // admin key
await treason.heartbeats.ping("nightly"); // alert key
const { schedules } = await treason.oncall.get("payments");
await treason.schedules.addOverride(id, { userId, startsAt: new Date(), endsAt: later });Also: heartbeats.get/list/delete, schedules.list/get/create/replace/delete/removeOverride.
#Configuration endpoints
For tooling and "config as code". All need an admin key (or read for reads).
users (create/update/delete/list/get, addPhone, addSlack, verifyContact, setRules, pairingCode), teams, policies, integrations, webhookEndpoints, apiKeys, and account (org, updateOrg, usage, audit).
#Errors
Everything the SDK throws extends TreasonError.
| Class | When | Useful fields |
|---|---|---|
TreasonApiError | the API answered with an error | status, code (validation_failed, not_found, forbidden, unauthorized, conflict, ...), message |
TreasonRateLimitError | 429 after retries ran out | retryAfterSeconds |
TreasonNetworkError | could not connect, or timed out | cause |
TreasonError | anything else (no key configured, aborted, bad signature) | code |
try {
await treason.alerts.get(id);
} catch (e) {
if (e instanceof TreasonApiError && e.status === 404) return null;
throw e;
}#Retries
The SDK retries network errors and 429, 502, 503, 504, with exponential backoff and jitter, honouring retry-after. It only retries what is safe to repeat:
GET,PUTandDELETEalways;POSTonly when it carries an idempotency key.alerts.createandpageattach one automatically, and reuse the same key for every attempt, so a retry can never create a second alert;- heartbeat pings, which are idempotent.
Everything else fails on the first error.
#Webhook helpers
import { webhooks } from "@treason/sdk";
const event = webhooks.constructEvent(rawBody, headers["x-treason-signature"], secret); // throws if not genuine
const header = webhooks.signPayload(secret, body); // for testsSee Webhooks.
#Why not generate a client for every language?
The API is plain JSON over HTTPS with a published OpenAPI document, so any generator (openapi-generator, oapi-codegen, openapi-python-client) works. We hand-write the TypeScript SDK because the parts that matter, retries, idempotency and waiting, are behaviour, not types.