SapaganDocs sapagan.com

SDKs

Server (TypeScript)

A typed client for the native API, for your backend.

The typed TypeScript client is generated from the same openapi.json as the API reference. Every route is typed, errors are typed classes, and a function can supply the token so a rotating token needs no new client.

Create a client#

import { createKentonganClient } from "@kentongan/sdk";

const sapagan = createKentonganClient({
  baseUrl: process.env.SAPAGAN_URL!,
  token: process.env.SAPAGAN_KEY!, // or () => string | Promise<string>
});

baseUrl is the API origin. Routes are /v1/... below it. token is a server key, a store key, or a function that returns one (for a store token that rotates). Other options: fetch, headers, and workspace for console sessions.

Helpers#

HelperRoute
sapagan.customersGET /v1/customers/{id} and its relationships
sapagan.scopesGET /v1/scopes/{id}
sapagan.scopeThe store-facing routes: policy, usage, block and appeals, plan, ledger, health, flows, reports, products
sapagan.templatesPush templates: list, create, update, preview
sapagan.broadcastsList, read, create, update, submit, schedule, cancel, stats

Any route#

sapagan.http is a typed openapi-fetch client over the whole generated contract, including routes without a helper:

const { data, error } = await sapagan.http.POST("/v1/batch", {
  body: {
    items: [{ type: "profile", id: "user-42", attributes: { name: "Budi Santoso" } }],
  },
});

Errors#

A non-2xx answer becomes a typed error with status, code, details and the requestId to quote to support.

ClassStatus
InvalidRequestError400
UnauthorizedError401
ForbiddenError403
NotFoundError404
ConflictError409
UnprocessableError422
RateLimitedError429
ServerError5xx

All extend ApiError.

import { NotFoundError } from "@kentongan/sdk";

try {
  await sapagan.customers.get("user-42");
} catch (e) {
  if (e instanceof NotFoundError) {
    // the key cannot see this person
  } else throw e;
}

See Errors and limits for the codes.