How to retrieve granted credits count with JS SDK?

dev
7ca5724Merge pull request #521 from useautumn/feat/atmn-nuke-push-server
This post might have stale content, as dev is 1479 commits ahead.

Is it possible to retrieve the number of granted credits included in a feature, using the js SDK?

Avatar of anthropic/claude-haiku-4.5
Claude Haiku 4.5
commented

Based on my exploration of the codebase, I now have enough information to answer your question. Let me provide a comprehensive answer:

##Answer

Yes, it is possible to retrieve the number of granted credits included in a feature using the JavaScript SDK. The granted credits are accessible through the granted_balance field in the API response.

###How to Access It

When you fetch a customer using the Autumn SDK, you get access to their balances through the balances field in the response. Each balance object contains:

ApiBalance {
  feature_id: string;
  granted_balance: number;  // ← The included/granted credits
  purchased_balance: number;
  current_balance: number;
  usage: number;
  // ... other fields
}

The granted_balance represents the total amount of usage that was granted (included) in the customer's plan(s) for a feature. This is the sum of all included_usage values across the customer's attached products.

###Example Usage

// Assuming you have a customer response
const customer = await autumnClient.getCustomer({ customer_id });

// Access balances for a specific feature
const featureBalances = customer.balances['feature_id'];

if (featureBalances) {
  console.log('Granted Credits:', featureBalances.granted_balance);
  console.log('Current Balance:', featureBalances.current_balance);
  console.log('Usage:', featureBalances.usage);
}

###Breakdown by Plan (Optional)

If a customer has multiple subscriptions with different reset intervals, you can also get a breakdown per subscription using the optional breakdown field:

featureBalances.breakdown?.forEach(breakdownItem => {
  console.log('Interval:', breakdownItem.interval);
  console.log('Granted for this interval:', breakdownItem.granted_balance);
});

The granted_balance is calculated from the included_usage field defined in your product items, and it's automatically aggregated across all the customer's attached products/plans.


END OF POST

How to retrieve granted credits count with JS SDK? — useautumn/autumn