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?
I'll help you find out if it's possible to retrieve the number of granted credits in a feature using the JavaScript SDK. Let me explore the codebase.
Let me try with a different approach:
Let me search for files related to credits and features:
Let me search for JavaScript/TypeScript SDK files and the feature/credit structure:
Now let me look at the customer API response models to understand what fields are available for the SDK:
Perfect! Now let me check the current/latest customer API response schema to see if there are any newer versions:
Good! Now let me check what the main customer response looks like to understand the structure better:
Perfect! Now let me look at the SDK to see what methods are available for the JavaScript SDK:
Let me check the shared package which likely contains the SDK types and client:
Now let me search for documentation or example usage of the SDK with customer data:
Let me search for documentation or examples about the API response:
Perfect! Now I have a clear picture. Let me check the API balance response structure one more time to confirm:
I already have the content from before. Let me look for the customer response examples in documentation:
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
Claude Haiku 4.5