Skip to content

Guides

The TypeScript SDK

Typed calls for every operation, with retries, idempotency, pagination, typed errors and webhook verification.

View as Markdown

The SDK is made from the same OpenAPI document as this site, so its types are the API's. It runs on Node 20.3 and later and on edge runtimes, on a server only: it refuses to start in a browser page, because a key must never be there.

Shell
npm i https://rcms-tickets-test.web.app/sdk/rcms-tickets-sdk-0.1.0.tgz

It is an ES module with its types, and it needs nothing else: import it, from an .mjs file or a project with "type": "module".

TypeScript
import { Tickets } from '@rcmstickets/sdk';

const tickets = new Tickets(process.env.RCMS_TICKETS_KEY); // not set: it throws

A rt_test_ key goes to the test host. A live key needs { baseUrl }, and live mode is closed.

Every operation#

tickets.<resource>.<method>(…) for every operation an API key can call; tickets.request('operationId', args) calls one by its OpenAPI operation id. A route that takes only a dashboard session, a device token or a holder's link answers an authentication error, because the SDK always sends the key.

TypeScript
const types = await tickets.ticketTypes.list({ event });
console.log(types.data.length); // 2

const same = await tickets.request('listTicketTypes', { event });
console.log(same.data.map((t) => t.id).join() === types.data.map((t) => t.id).join()); // true

Retries and idempotency#

A network failure, a 429, a 5xx, idempotency_key_in_use and lock_timeout are retried twice, honouring Retry-After. Every POST, PATCH and PUT carries an Idempotency-Key the client makes, and every retry of one call reuses it, so a retried write happens once. Pass idempotencyKey to choose your own, for example one made from your own order id:

TypeScript
const holdKey = `docs-sdk-hold-${crypto.randomUUID()}`; // Unique per run: the same key with a new body is refused.
const a = await tickets.holds.create({ event, items: [{ ticket_type: ga, quantity: 1 }] }, { idempotencyKey: holdKey });
const b = await tickets.holds.create({ event, items: [{ ticket_type: ga, quantity: 1 }] }, { idempotencyKey: holdKey });
console.log(a.id === b.id); // true

A DELETE carries no key. Idempotency and retries has the rules.

Pages#

A list answers a Page: page.data is the rows, await page.nextPage() the next, and the page is an async iterable.

TypeScript
let seen = 0;
for await (const type of await tickets.ticketTypes.list({ event, limit: 1 })) seen += 1;
console.log(seen); // 2

Lists page forward with starting_after and, where the list allows it, backward with ending_before. page.toArray({ limit }) collects up to 10,000 rows unless you say more.

Errors#

A failed call throws a TicketsError with status, type, code, param, docUrl, requestId, declineCode and retryAfter, and a subclass by type (StateError, InvalidRequestError, AuthenticationError, PermissionError, IdempotencyError, RateLimitError, PaymentError, ApiError). Match on code, not on the message.

TypeScript
import { TicketsError } from '@rcmstickets/sdk';

try {
  await tickets.holds.create({ event, items: [{ ticket_type: ga, quantity: 11 }] });
} catch (error) {
  if (!(error instanceof TicketsError)) throw error;
  console.log(error.status, error.code, error.is('quantity_unavailable') || error.is('order_maximum_exceeded')); // 409 order_maximum_exceeded true
  console.log(error.requestId?.startsWith('req_')); // true
}

TicketsConnectionError means no whole answer came: the write may or may not have happened, so retry it with the same idempotency key. See errors.

Helpers#

  • tickets.checkout.begin, .complete, .release and .run are the two calls of a checkout with the right retry behaviour built in: complete makes its key from the hold and a hash of the whole order, so a double click is one order and a corrected email is a new attempt.
  • tickets.bulk.ticketTypes, .addOns and .codes take any number of items, send them in calls of 500, and send the items a call did not reach again, with the same item keys, until the work is done.
  • tickets.webhooks.verify checks a delivery (Webhooks).