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 field | Controls |
|---|---|
| budget | Maximum service purchases in atomic units |
| purchaseStep | The increment used to raise cumulative purchases |
| requestTtl | Signed resource-request lifetime in seconds |
| expiresAt | When application acceptance stops, in Unix seconds |
| settlementInterval | Time between settlement ticks |
| broadcastTimeout | When an unresolved broadcast is marked uncertain |
| autoClose / autoFinalize | Lifecycle 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.
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.
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.
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();