Skip to content

API request

If you need to use the API control token, you need to apply to the reseller or administrator first, and you can use it only after the administrator opens it.

Contact email: support@bsf.ai

Please include the reason for your request and the account to be opened.


All token management endpoints require user authentication (UserAuth middleware) and support two methods:

  • Session Cookie (automatically included after browser login)
  • Access Token (passed via the Authorization header, suitable for curl / programmatic access)

When using Access Token authentication, include the following headers:

HeaderRequiredDescription
AuthorizationYesUser Access Token

All curl examples below use Access Token authentication. Replace:

  • https://api.bsf.ai/ — Your service URL
  • {ACCESS_TOKEN} — Your user Access Token

Base path: /api/token


Data Structure

Token Object

FieldTypeDescription
idintToken ID (primary key)
user_idintOwner user ID
keystringAPI Key (48 characters, auto-generated, prefixed with sk-)
statusintStatus: 1 Enabled / 2 Disabled / 3 Expired / 4 Quota exhausted
namestringToken name (max 50 characters)
created_timeint64Creation time (Unix timestamp)
accessed_timeint64Last access time (Unix timestamp)
expired_timeint64Expiration time (Unix timestamp), -1 means never expires
remain_quotaintRemaining quota
unlimited_quotaboolWhether quota is unlimited
used_quotaintConsumed quota
model_limits_enabledboolWhether model restrictions are enabled
model_limitsstringAllowed models (comma-separated, e.g. gpt-4,claude-3-opus)
allow_ipsstringIP allowlist (newline \n separated, supports CIDR notation)
groupstringChannel group
cross_group_retryboolCross-group retry (only effective for auto group)

API Endpoints

1. List Tokens

GET /api/token/?p={page}&size={pageSize}

Query Parameters:

ParameterTypeRequiredDescription
pintNoPage number, starting from 0
sizeintNoItems per page

Response Example:

json
{
  "success": true,
  "message": "",
  "data": {
    "page": 0,
    "page_size": 10,
    "total": 2,
    "items": [
      {
        "id": 1,
        "name": "my-token",
        "status": 1,
        "key": "",
        "created_time": 1710000000,
        "expired_time": -1,
        "remain_quota": 500000,
        "unlimited_quota": false,
        "used_quota": 12000,
        "model_limits_enabled": false,
        "model_limits": "",
        "allow_ips": "",
        "group": "",
        "cross_group_retry": false
      }
    ]
  }
}

Note: The key field is cleared in list responses and will not return the actual API Key.

curl Example:

bash
curl -X GET 'https://api.bsf.ai/api/token/?p=0&size=10' \
  -H 'Authorization: {ACCESS_TOKEN}'

2. Search Tokens

GET /api/token/search?keyword={keyword}&token={tokenKey}&p={page}&size={pageSize}

Query Parameters:

ParameterTypeRequiredDescription
keywordstringNoFuzzy search by name
tokenstringNoSearch by Key
pintNoPage number
sizeintNoItems per page

Search is rate-limited (SearchRateLimit middleware). Fuzzy search requires at least 2 characters and allows up to 2 wildcards.

curl Example:

bash
# Search by name
curl -X GET 'https://api.bsf.ai/api/token/search?keyword=production&p=0&size=10' \
  -H 'Authorization: {ACCESS_TOKEN}'

# Search by Key
curl -X GET 'https://api.bsf.ai/api/token/search?token=abc123&p=0&size=10' \
  -H 'Authorization: {ACCESS_TOKEN}'

3. Get a Single Token

GET /api/token/{id}

Path Parameters:

ParameterTypeRequiredDescription
idintYesToken ID

Response Example:

json
{
  "success": true,
  "message": "",
  "data": {
    "id": 1,
    "name": "my-token",
    "key": "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "status": 1,
    "remain_quota": 500000,
    "..."
  }
}

curl Example:

bash
curl -X GET 'https://api.bsf.ai/api/token/1' \
  -H 'Authorization: {ACCESS_TOKEN}'

4. Create a Token

POST /api/token/
Content-Type: application/json

Request Body:

FieldTypeRequiredDescription
namestringYesToken name, max 50 characters
expired_timeint64NoExpiration time (Unix timestamp), -1 for never expires
remain_quotaintNoRemaining quota (for non-unlimited tokens, range 0 ~ 1,000,000,000 × QuotaPerUnit)
unlimited_quotaboolNoWhether quota is unlimited
model_limits_enabledboolNoWhether to enable model restrictions
model_limitsstringNoAllowed models (comma-separated)
allow_ipsstringNoIP allowlist (newline-separated, supports CIDR)
groupstringNoChannel group
cross_group_retryboolNoCross-group retry

Request Example:

json
{
  "name": "production-key",
  "expired_time": 1735689600,
  "remain_quota": 1000000,
  "unlimited_quota": false,
  "model_limits_enabled": true,
  "model_limits": "gpt-4,gpt-4o,claude-3-opus",
  "allow_ips": "192.168.1.0/24\n10.0.0.1",
  "group": "default"
}

Response:

json
{
  "success": true,
  "message": ""
}

Each user has a maximum token limit. Creation will fail if the limit is exceeded.

curl Example:

bash
# Create a token with quota and model restrictions
curl -X POST 'https://api.bsf.ai/api/token/' \
  -H 'Authorization: {ACCESS_TOKEN}' \
  -H 'Content-Type: application/json' \
  -d '{
    "name": "production-key",
    "expired_time": 1735689600,
    "remain_quota": 1000000,
    "unlimited_quota": false,
    "model_limits_enabled": true,
    "model_limits": "gpt-4,gpt-4o,claude-3-opus",
    "allow_ips": "192.168.1.0/24\n10.0.0.1",
    "group": "default"
  }'

# Create an unlimited, never-expiring token
curl -X POST 'https://api.bsf.ai/api/token/' \
  -H 'Authorization: {ACCESS_TOKEN}' \
  -H 'Content-Type: application/json' \
  -d '{
    "name": "unlimited-key",
    "expired_time": -1,
    "unlimited_quota": true
  }'

5. Update a Token

5a. Update All Modifiable Fields

PUT /api/token/
Content-Type: application/json

Request Body:

FieldTypeRequiredDescription
idintYesToken ID to update
namestringNoToken name
expired_timeint64NoExpiration time
remain_quotaintNoRemaining quota
unlimited_quotaboolNoWhether quota is unlimited
model_limits_enabledboolNoWhether to enable model restrictions
model_limitsstringNoAllowed models
allow_ipsstringNoIP allowlist
groupstringNoChannel group
cross_group_retryboolNoCross-group retry

curl Example:

bash
# Update token name, quota, and model restrictions
curl -X PUT 'https://api.bsf.ai/api/token/' \
  -H 'Authorization: {ACCESS_TOKEN}' \
  -H 'Content-Type: application/json' \
  -d '{
    "id": 1,
    "name": "renamed-key",
    "remain_quota": 2000000,
    "unlimited_quota": false,
    "model_limits_enabled": true,
    "model_limits": "gpt-4o,claude-3-5-sonnet",
    "expired_time": 1767225600
  }'

5b. Update Status Only

PUT /api/token/?status_only=1
Content-Type: application/json

Request Body:

FieldTypeRequiredDescription
idintYesToken ID
statusintYes1 Enable / 2 Disable

Restrictions:

  • Expired tokens cannot be enabled unless the expiration time is updated
  • Quota-exhausted tokens cannot be enabled unless they have unlimited quota

curl Example:

bash
# Disable a token
curl -X PUT 'https://api.bsf.ai/api/token/?status_only=1' \
  -H 'Authorization: {ACCESS_TOKEN}' \
  -H 'Content-Type: application/json' \
  -d '{"id": 1, "status": 2}'

# Enable a token
curl -X PUT 'https://api.bsf.ai/api/token/?status_only=1' \
  -H 'Authorization: {ACCESS_TOKEN}' \
  -H 'Content-Type: application/json' \
  -d '{"id": 1, "status": 1}'

Response:

json
{
  "success": true,
  "message": "",
  "data": { "...updated token object..." }
}

6. Delete a Token

DELETE /api/token/{id}

Path Parameters:

ParameterTypeRequiredDescription
idintYesToken ID

curl Example:

bash
curl -X DELETE 'https://api.bsf.ai/api/token/1' \
  -H 'Authorization: {ACCESS_TOKEN}'

Response:

json
{
  "success": true,
  "message": ""
}

Deletion is a soft delete — data is not immediately purged.


7. Batch Delete Tokens

POST /api/token/batch
Content-Type: application/json

Request Body:

json
{
  "ids": [1, 2, 3]
}

Response:

json
{
  "success": true,
  "message": "",
  "data": 3
}

data is the number of tokens actually deleted.

curl Example:

bash
curl -X POST 'https://api.bsf.ai/api/token/batch' \
  -H 'Authorization: {ACCESS_TOKEN}' \
  -H 'Content-Type: application/json' \
  -d '{"ids": [1, 2, 3]}'

Token Usage Query

This endpoint uses Bearer Token authentication (i.e., the API Key queries its own usage) and does not require user login.

GET /api/usage/token/
Authorization: Bearer sk-xxxxxxxx

curl Example:

bash
curl -X GET 'https://api.bsf.ai/api/usage/token/' \
  -H 'Authorization: Bearer sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

Response Example:

json
{
  "code": true,
  "message": "ok",
  "data": {
    "object": "token_usage",
    "name": "my-token",
    "total_usd_granted": 2.024,
    "total_usd_used": 0.024,
    "total_usd_available": 2.0,
    "unlimited_quota": false,
    "model_limits": {
      "gpt-4": true,
      "claude-3-opus": true
    },
    "model_limits_enabled": true,
    "expires_at": 1735689600,
    "user_usd_available": 88.5
  }
}
FieldDescription
total_usd_grantedToken total granted quota (USD), equals total_usd_available + total_usd_used
total_usd_usedToken used quota (USD)
total_usd_availableToken remaining available quota (USD)
unlimited_quotaWhether quota is unlimited
model_limitsModel restriction mapping
expires_atExpiration time, 0 means never expires
user_usd_availableUser account remaining balance (USD). When unlimited quota is enabled, this field represents the actual usable limit

Token Daily Usage Statistics

GET /api/token/{id}/usage?start_date={start}&end_date={end}

Requires user login authentication (same as token management endpoints).

Path Parameters:

ParameterTypeRequiredDescription
idintYesToken ID

Query Parameters:

ParameterTypeRequiredDescription
start_datestringNoStart date, format YYYY-MM-DD
end_datestringNoEnd date, format YYYY-MM-DD

Limits:

  • Defaults to the last 1 day if no parameters are provided
  • Maximum 7 days; automatically truncated to start_date + 7 days if exceeded
  • Results are cached for 10 minutes to reduce database load

Response Example:

json
{
  "success": true,
  "data": {
    "token_id": 1,
    "token_name": "my-token",
    "start_date": "2026-03-20",
    "end_date": "2026-03-26",
    "daily": [
      {
        "date": "2026-03-20",
        "usd": 0.35,
        "requests": 12,
        "prompt_tokens": 5000,
        "completion_tokens": 2000
      }
    ]
  }
}

daily Field Descriptions:

FieldDescription
dateDate (YYYY-MM-DD)
usdDaily spend (USD)
requestsNumber of requests
prompt_tokensInput token count
completion_tokensOutput token count

curl Examples:

bash
# Query last 1 day (default)
curl -X GET 'https://api.bsf.ai/api/token/1/usage' \
  -H 'Authorization: {ACCESS_TOKEN}'

# Query specific date range (max 7 days)
curl -X GET 'https://api.bsf.ai/api/token/1/usage?start_date=2026-03-20&end_date=2026-03-26' \
  -H 'Authorization: {ACCESS_TOKEN}'

Quota Unit Conversion

UnitDescription
quotaInternal unit
USDUser-facing unit
Conversion1 USD = 500,000 quota (i.e., $0.002 / 1K tokens)

Error Response Format

json
{
  "success": false,
  "message": "error message"
}