How to retrieve granted credits count with JS SDK?
Is it possible to retrieve the number of granted credits included in a feature, using the js SDK?
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