Skip to main content

API Overview

The Balance Query API retrieves your account’s current quota usage, including total quota, used quota, remaining balance, and request count. This API helps you easily monitor your account balance, enabling proactive and flexible balance alert management.

How to Get Authorization Token

This API authenticates with a personal access token (starting with nap_). It is different from the API Key you use to call models.
1

Open Personal Access Tokens

Go to your profile page at api.apiyi.com/account/profile. Under Account Options, click Personal Access Tokens → Create Token.
2

Select permissions

This API only needs Profile · Read. The default expiry is 30 days, so we recommend choosing a longer period.
3

Save the token

Copy the token right after it is created and put it in the Authorization header.
Create a personal access token: name, expiry, IP allowlist and permissions
Legacy system tokens are scheduled to retire on 2026-11-11 (UTC+8). AccessTokens generated under “Account Options - System Token” keep working until then. After that date they return 401 ACCESS_TOKEN_LEGACY_RETIRED. We will review usage and may extend the date, but we recommend switching to a personal access token now. Just replace the header value. Your code stays the same. For details on expiry, IP allowlists and permissions, see What Is a Personal Access Token?

API Information

Request Details

Request Headers

Request Parameters

This is a GET request and does not require any request body parameters.

Response Details

Success Response Example

Key Response Fields

Quota Conversion

Conversion Rule

500,000 quota = $1.00 USD
Calculation Formulas:
  • USD amount = quota ÷ 500,000
  • Remaining quota = quota (quota represents current remaining balance)
  • Remaining USD = quota ÷ 500,000
Examples:
  • quota: 24997909 → $49.99 USD (current remaining balance)
  • used_quota: 10027091 → $20.05 USD (used amount)

Error Responses

HTTP 401 - Authentication Failed

Reason: The token is invalid, expired or deleted. Changing the account password, two-factor authentication or passkey revokes every personal access token. Solution: Create a new personal access token in the console. Two other 401 responses carry an explicit message:

HTTP 403 - Permission Denied

Reason: The personal access token doesn’t have Profile · Read. Solution: Create a new token with that permission. Permissions can’t be changed after creation.

Code Examples

cURL Example

Important: The --compressed option is required because the API returns gzip-compressed content, otherwise you’ll receive garbled output.
Quick Test (replace YOUR_TOKEN_HERE):
Note: The -s option hides progress bar, --compressed automatically decompresses gzip response

Python Example (Basic)

Python Example (Optimized)

This adds the following on top of the basic example:

Error Handling

Complete exception handling and error capture

Environment Variables

Secure token management, avoid hardcoding

Formatted Output

Beautiful table display and number formatting

Auto Conversion

Automatic USD amount calculation
Save the code below as quota.py and it is ready to run:
Usage:
Output Example:

FAQ

Follow the steps in “How to Get Authorization Token” above: create a personal access token on your profile page and select “Profile · Read”. Legacy system tokens are scheduled to retire on 2026-11-11 (UTC+8). See What Is a Personal Access Token?
No, the balance query API does not consume your quota.
We recommend a query interval of at least 1 second to avoid triggering rate limits.
This field returns pricing information for various AI models. You can ignore it if you only need balance information.
The quota field represents the current remaining balance. If quota is 0 or close to 0, your account balance is insufficient and needs recharging.
Issue: Executing curl returns garbled text, or jq reports “Invalid numeric literal”Reason: The API returns gzip-compressed content (Content-Encoding: gzip), and curl doesn’t automatically decompress it.Solution: Add the --compressed option to make curl decompress automatically:

Important Notes

Security Reminders
  • Never hardcode Authorization tokens in your code
  • Use environment variables or configuration files to manage sensitive information
  • Don’t commit code containing tokens to public repositories
Request Limits
  • Set reasonable request timeout (recommended: 10 seconds)
  • Avoid excessively frequent query requests

Exception Handling Recommendations

  • Always handle network exceptions, timeouts, and authentication failures
  • Log errors for easier troubleshooting

Response Format Notes

  • API returns gzip-compressed content; curl requires the --compressed option
  • Python’s requests library automatically handles gzip decompression without additional configuration