The UsageClient provides methods for reporting and querying metered feature usage. It works in conjunction with the EntitlementsClient — report usage after a successful entitlement check, and revert it if downstream operations fail.
Revert (roll back) previously reported usage. Decrements the customer’s usage meter. Essential for maintaining accurate billing when downstream operations fail after usage was already recorded.
const result = await revstack.usage.revert(params);
For operations where you know the exact usage after execution:
try { // 1. Perform the operation const result = await expensiveOperation(); // 2. Report actual usage after success await revstack.usage.report({ customerId: userId, featureId: "api-calls", amount: result.callsMade, }); return result;} catch (error) { // No usage to revert since we didn't report any throw error;}
// Get all meters for a customerconst meters = await revstack.usage.getMeters(userId);// Display in a usage dashboardfor (const meter of meters) { const entitlement = await revstack.entitlements.get( meter.entitlementId ); console.log(`${entitlement.name}:`); console.log(` Current: ${meter.currentUsage}`); console.log(` Resets: ${new Date(meter.resetAt).toLocaleDateString()}`);}