On this page

SessionManager Reference

Prepare purchases, accept resource requests and recover settlement after a restart.

Create a manager#

createSessionManager takes a backend, durable store, session terms and policy. Add the session authority signer for buyer preparation and an optional lifecycle backend when needed. The merchant needs a backend that can submit claims.

Keep terms, policy and ledger unchanged across restarts. A manager rejects a changed configuration for a live session. Deleting the ledger would lose reservations and replay protection.

Policy fieldControls
budgetMaximum service purchases in atomic units
purchaseStepThe increment used to raise cumulative purchases
requestTtlSigned resource-request lifetime in seconds
expiresAtWhen application acceptance stops, in Unix seconds
settlementIntervalTime between settlement ticks
broadcastTimeoutWhen an unresolved broadcast is marked uncertain
autoClose / autoFinalizeLifecycle behavior; defaults false / true

Prepare and receive#

prepare takes requestId, requestDigest and amount. When needed, it signs a cumulative purchase increment, then signs the resource request and saves its reservation. receive checks both signatures against content and price calculated by the merchant.

Retrying an ID with the same content and price reuses its preparation or receipt. Changing those values fails. The buyer keeps a prepared request's reservation even if it is never delivered.

javascript
const bundle = await buyer.prepare({ requestId, requestDigest, amount });
const receipt = await merchant.receive(bundle, {
  requestId, requestDigest: serverDigest, amount: serverPrice,
});

Check status and settle#

status and recover save new chain observations. settle claims the highest accepted voucher. tick checks its settlement interval and watches closure. Other lifecycle methods call the configured driver.

After a broadcast interruption, call recover to check chain state. An uncertain retry must be the same operation with the same settlement amount.

Purchasing service credit and billing each request
Purchasing service credit and billing each request

Run a settlement worker#

startWorker schedules ticks and reports failures through onError. It handles payment state only; your application runs, caches and retries service jobs. Stop the worker cleanly when shutting down.

javascript
await merchant.recover();
const worker = merchant.startWorker({
  intervalMs: 5000,
  onError: error => console.error(error.code, error.message),
});
// After stopping your application from accepting new work:
await worker.stop();