> ## Documentation Index
> Fetch the complete documentation index at: https://docs.aihubmix.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Retrieve Account Information via API

> Manage keys, view accounts, and available model lists in the command line using the AiHubMix CLI utility script.

The AiHubMix CLI is a collection of utility scripts that allows you to manage your AiHubMix API keys, query account information, and use AI services without going through a web interface. Essentially, it encapsulates API calls (using curl or Python requests) for command-line convenience.

<Tip>
  We recommend the next-generation command-line tool [AIHubMix CLI](/en/api/aihubmix-cli): a single binary with zero runtime dependencies (no Python required), resource-oriented commands like `aihubmix keys list`, and `jq`- and AI Agent-friendly output. This page retains the legacy Python script usage for reference.
</Tip>

## Prerequisites

Before using the AiHubMix CLI, you need to:

1. An [AIHubMix account](https://aihubmix.com)
2. Generate an Access Token by clicking "Generate System Access Token" on the [AIHubMix Settings page](https://aihubmix.com/setting);
3. Install the necessary Python dependencies:

```bash theme={null}
pip install -U requests openai
```

<Info>
  The aihubmix\_cli.py script can be [downloaded here](https://github.com/jerlinn/inferHub)
</Info>

## Features Overview

The AIHubMix CLI offers the following key functionalities:

### API Endpoint Overview

| Endpoint                     | HTTP Method | Description                                             |
| ---------------------------- | ----------- | ------------------------------------------------------- |
| `/api/user/self`             | GET         | Retrieve current user information and account balance   |
| `/api/token/`                | GET         | Retrieve a list of all Keys                             |
| `/api/token/`                | POST        | Create a new API Key                                    |
| `/api/token/`                | PUT         | Update an existing API Key                              |
| `/api/token/{token_id}`      | GET         | Retrieve detailed information for a specific Key        |
| `/api/token/{token_id}`      | DELETE      | Delete a specific Key                                   |
| `/api/token/search`          | GET         | Search for Keys (use `?keyword=search_term`)            |
| `/api/user/token`            | GET         | Retrieve user Keys                                      |
| `/api/user/available_models` | GET         | Retrieve a list of models available to the current user |

### Retrieve Balance Information

<CodeGroup>
  ```shell theme={null}
  # Using curl to retrieve balance, balance is quota / 500000
  curl -X GET "https://aihubmix.com/api/user/self" \
    -H "Authorization: YOUR_ACCESS_TOKEN" \
    -H "Content-Type: application/json"
  ```

  ```py theme={null}
  import requests
  import json

  # API Configuration
  api_url = "https://aihubmix.com"
  access_token = "YOUR_ACCESS_TOKEN"
  headers = {
      "Authorization": access_token,
      "Content-Type": "application/json"
  }

  # Retrieve account balance
  response = requests.get(f"{api_url}/api/user/self", headers=headers)
  if response.status_code == 200:
      data = response.json()
      if data.get("success", False):
          user_data = data.get("data", {})
          quota = user_data.get('quota', 0)
          usd_balance = quota / 500000  # $1 equals 500,000 quota
          print(f"Username: {user_data.get('username', 'Unknown')}")
          print(f"Display Name: {user_data.get('display_name', 'Unknown')}")
          print(f"Current Quota: {quota}")
          print(f"Available USD: ${usd_balance:.2f}")
      else:
          print(f"Request failed: {data.get('message', 'Unknown error')}")
  else:
      print(f"Request failed, status code: {response.status_code}")
  ```

  ```shell theme={null}
  python aihubmix_cli.py --url "https://aihubmix.com" --token "YOUR_ACCESS_TOKEN" --action get_balance
  ```

  ```shell theme={null}
  # Retrieve remaining quota for Key
  curl 'https://aihubmix.com/dashboard/billing/remain' \
    -H 'authorization: Bearer sk-***' \
  ```
</CodeGroup>

### Key Management

#### Create New Key

<CodeGroup>
  ```shell theme={null}
  curl -X POST "https://aihubmix.com/api/token/" \
    -H "Authorization: YOUR_ACCESS_TOKEN" \
    -H "Content-Type: application/json" \
    -d '{
      "name": "New Key Name",
      "expired_time": -1,
      "remain_quota": 500000,
      "unlimited_quota": false,
      "subnet": ""
    }'
  ```

  ```py theme={null}
  import requests
  import json

  # API Configuration
  api_url = "https://aihubmix.com"
  access_token = "YOUR_ACCESS_TOKEN"
  headers = {
      "Authorization": access_token,
      "Content-Type": "application/json"
  }

  # Create New Key
  payload = {
      "name": "New Key Name",
      "expired_time": -1,  # Never expires
      "remain_quota": 500000,
      "unlimited_quota": False,
      "subnet": ""
  }

  response = requests.post(f"{api_url}/api/token/", headers=headers, json=payload)
  if response.status_code == 200:
      data = response.json()
      if data.get("success", False):
          token_data = data.get("data", {})
          print(f"New Key: {token_data.get('key', 'Unknown')}")
          print(f"Key ID: {token_data.get('id', 'Unknown')}")
      else:
          print(f"Failed to create Key: {data.get('message', 'Unknown error')}")
  else:
      print(f"Request failed, status code: {response.status_code}")
  ```

  ```shell theme={null}
  python aihubmix_cli.py --url "https://aihubmix.com" --token "YOUR_ACCESS_TOKEN" --action create_token --name "New Key Name" --expires -1 --quota 500000
  ```
</CodeGroup>

#### Retrieve Key List

<CodeGroup>
  ```shell theme={null}
  curl -X GET "https://aihubmix.com/api/token/?num=20" \ # Adjust num parameter to change the number of output data.
    -H "Authorization: YOUR_ACCESS_TOKEN" \
    -H "Content-Type: application/json"
  ```

  ```py theme={null}
  import requests
  import json

  # API Configuration
  api_url = "https://aihubmix.com"
  access_token = "YOUR_ACCESS_TOKEN"
  headers = {
      "Authorization": access_token,
      "Content-Type": "application/json"
  }

  # Retrieve Key List
  response = requests.get(f"{api_url}/api/token/", headers=headers)
  if response.status_code == 200:
      data = response.json()
      if data.get("success", False):
          tokens_data = data.get("data", [])
          print(f"Key List (Total {len(tokens_data)}):")
          for token in tokens_data:
              print(f"Key ID: {token.get('id', 'Unknown')}")
              print(f"Key Name: {token.get('name', 'Unknown')}")
              print(f"Key: {token.get('key', 'Unknown')}")
              print("---")
      else:
          print(f"Failed to retrieve Key list: {data.get('message', 'Unknown error')}")
  else:
      print(f"Request failed, status code: {response.status_code}")
  ```

  ```shell theme={null}
  python aihubmix_cli.py --url "https://aihubmix.com" --token "YOUR_ACCESS_TOKEN" --action get_tokens
  ```
</CodeGroup>

#### Search for Key

<CodeGroup>
  ```shell theme={null}
  curl -X GET "https://aihubmix.com/api/token/search?keyword=search_term" \
    -H "Authorization: YOUR_ACCESS_TOKEN" \
    -H "Content-Type: application/json"
  ```

  ```py theme={null}
  import requests
  import json

  # API Configuration
  api_url = "https://aihubmix.com"
  access_token = "YOUR_ACCESS_TOKEN"
  headers = {
      "Authorization": access_token,
      "Content-Type": "application/json"
  }

  # Search Keyword
  query = "search_term"

  # Search Key
  response = requests.get(f"{api_url}/api/token/search?keyword={query}", headers=headers)
  if response.status_code == 200:
      data = response.json()
      if data.get("success", False):
          tokens_data = data.get("data", [])
          print(f"Search Results (Total {len(tokens_data)}):")
          for token in tokens_data:
              print(f"Key ID: {token.get('id', 'Unknown')}")
              print(f"Key Name: {token.get('name', 'Unknown')}")
              print(f"Key: {token.get('key', 'Unknown')}")
              print("---")
      else:
          print(f"Failed to search Key: {data.get('message', 'Unknown error')}")
  else:
      print(f"Request failed, status code: {response.status_code}")
  ```

  ```shell theme={null}
  python aihubmix_cli.py --url "https://aihubmix.com" --token "YOUR_ACCESS_TOKEN" --action search_tokens --query "search_term"
  ```
</CodeGroup>

#### Update Key

<CodeGroup>
  ```shell theme={null}
  curl -X PUT "https://aihubmix.com/api/token/" \
    -H "Authorization: YOUR_ACCESS_TOKEN" \
    -H "Content-Type: application/json" \
    -d '{
      "id": "Key_ID",
      "name": "New Name",
      "expired_time": 86400,
      "remain_quota": 100000,
      "status": 1
    }'
  ```

  ```py theme={null}
  import requests
  import json

  # API Configuration
  api_url = "https://aihubmix.com"
  access_token = "YOUR_ACCESS_TOKEN"
  headers = {
      "Authorization": access_token,
      "Content-Type": "application/json"
  }

  # Update Key
  key_id = "Key_ID"  # Replace with the actual Key ID
  payload = {
      "id": key_id,
      "name": "New Name",
      "expired_time": 86400,  # Expires after 24 hours
      "remain_quota": 100000,
      "status": 1  # 1-Enabled, 0-Disabled
  }

  response = requests.put(f"{api_url}/api/token/", headers=headers, json=payload)
  if response.status_code == 200:
      data = response.json()
      if data.get("success", False):
          print(f"Key updated successfully")
          if "data" in data:
              token_data = data.get("data", {})
              print(f"Name: {token_data.get('name', 'Unknown')}")
              print(f"Expiration Time: {token_data.get('expired_time', 'Unknown')}")
      else:
          print(f"Failed to update Key: {data.get('message', 'Unknown error')}")
  else:
      print(f"Request failed, status code: {response.status_code}")
  ```

  ```shell theme={null}
  python aihubmix_cli.py --url "https://aihubmix.com" --token "YOUR_ACCESS_TOKEN" --action update_token --id "Key_ID" --name "New Name" --expires 86400 --quota 100000 --status 1
  ```
</CodeGroup>

#### Delete Key

<CodeGroup>
  ```shell theme={null}
  curl -X DELETE "https://aihubmix.com/api/token/Key_ID" \
    -H "Authorization: YOUR_ACCESS_TOKEN" \
    -H "Content-Type: application/json"
  ```

  ```py theme={null}
  import requests
  import json

  # API Configuration
  api_url = "https://aihubmix.com"
  access_token = "YOUR_ACCESS_TOKEN"
  headers = {
      "Authorization": access_token,
      "Content-Type": "application/json"
  }

  # Delete Key
  key_id = "Key_ID"  # Replace with the actual Key ID

  response = requests.delete(f"{api_url}/api/token/{key_id}", headers=headers)
  if response.status_code == 200:
      data = response.json()
      if data.get("success", False):
          print(f"Key deleted successfully")
      else:
          print(f"Failed to delete Key: {data.get('message', 'Unknown error')}")
  else:
      print(f"Request failed, status code: {response.status_code}")
  ```

  ```shell theme={null}
  python aihubmix_cli.py --url "https://aihubmix.com" --token "YOUR_ACCESS_TOKEN" --action delete_token --id "Key_ID"
  ```
</CodeGroup>

#### Retrieve User Key

<CodeGroup>
  ```shell theme={null}
  curl -X GET "https://aihubmix.com/api/user/token" \
    -H "Authorization: YOUR_ACCESS_TOKEN" \
    -H "Content-Type: application/json"
  ```

  ```py theme={null}
  import requests
  import json

  # API Configuration
  api_url = "https://aihubmix.com"
  access_token = "YOUR_ACCESS_TOKEN"
  headers = {
      "Authorization": access_token,
      "Content-Type": "application/json"
  }

  # Retrieve User Key
  response = requests.get(f"{api_url}/api/user/token", headers=headers)
  if response.status_code == 200:
      data = response.json()
      if data.get("success", False):
          user_token = data.get("data", {}).get("token")
          print(f"User Key: {user_token}")
      else:
          print(f"Failed to retrieve User Key: {data.get('message', 'Unknown error')}")
  else:
      print(f"Request failed, status code: {response.status_code}")
  ```

  ```shell theme={null}
  python aihubmix_cli.py --url "https://aihubmix.com" --token "YOUR_ACCESS_TOKEN" --action get_user_token
  ```
</CodeGroup>

### Model Management

#### Retrieve User's Available Models

<CodeGroup>
  ```shell theme={null}
  curl -X GET "https://aihubmix.com/api/user/available_models" \
    -H "Authorization: YOUR_ACCESS_TOKEN" \
    -H "Content-Type: application/json"
  ```

  ```py theme={null}
  import requests
  import json

  # API Configuration
  api_url = "https://aihubmix.com"
  access_token = "YOUR_ACCESS_TOKEN"
  headers = {
      "Authorization": access_token,
      "Content-Type": "application/json"
  }

  # Retrieve User's Available Models
  response = requests.get(f"{api_url}/api/user/available_models", headers=headers)
  if response.status_code == 200:
      data = response.json()
      if data.get("success", False):
          models_data = data.get("data", [])
          print(f"User's Available Model List (Total {len(models_data)}):")
          for i, model in enumerate(models_data, 1):
              print(f"  {i}. {model}")
      else:
          print(f"Failed to retrieve user's available models: {data.get('message', 'Unknown error')}")
  else:
      print(f"Request failed, status code: {response.status_code}")
  ```

  ```shell theme={null}
  python aihubmix_cli.py --url "https://aihubmix.com" --token "YOUR_ACCESS_TOKEN" --action get_available_models
  ```
</CodeGroup>

## Output in JSON Format

All CLI commands support outputting results in JSON format, making it easier for programmatic processing:

```bash theme={null}
python aihubmix_cli.py --url "https://aihubmix.com" --token "YOUR_ACCESS_TOKEN" --action get_balance --json
```

## Troubleshooting

If you encounter issues, you can try the following solutions:

1. **Connection Issues**: If the main domain fails to connect, try using an alternative domain:

   ```bash theme={null}
   python aihubmix_cli.py --url "https://api.aihubmix.com" --token "YOUR_ACCESS_TOKEN" --action get_balance
   ```
2. **Invalid Access Token**: Ensure that the provided access token is a valid key obtained from the AIHubMix website. The format of the access token is usually like `fd***`.
3. **Insufficient Permissions**: Some operations may require specific permissions, so ensure your account has adequate permissions.
4. **Request Failure**: Check your network connection or try again later.

## Notes

* The access token is different from the regular API Key used to access AI models.
* Each user has their own system access token, and the access level is determined by the user's role (regular user, administrator, or root user).

***

Last updated: 2026-06-01
