Credits Module (Credits)
The Credits module provides low-level credit operation capabilities.
API List
| Method | Return Value | Description |
|---|---|---|
sdk.credits.getBalance() | Promise<{ quota: number }> | Query credit balance (network request) |
sdk.credits.getCachedBalance() | number | Get cached balance (synchronous, 0 before first call) |
sdk.credits.onChange(callback) | () => void | Listen for credit changes |
Detailed Usage
Query Balance
typescript
const { quota } = await sdk.credits.getBalance()
console.log('Credits balance:', quota)
// Synchronously get the cached value from the last query
const cached = sdk.credits.getCachedBalance()WARNING
The return field name is quota (corresponding to the backend /ai/v1/credit/getBalance interface), not balance.
Listen for Changes
typescript
const unsubscribe = sdk.credits.onChange((balance) => {
updateCreditsDisplay(balance)
})
unsubscribe() // Stop listeningBilling vs Credits
| Module | Positioning | Typical Use Case |
|---|---|---|
credits | Low-level credit operations | Scenarios requiring direct credit query/deduction (e.g., custom actions like AI generation) |
billing | High-level unified billing | Standard operations like export and download, automatically handling free quota → credits fallback |
The billing module is recommended for most scenarios.