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

# API 调用示例

> 我们支持各种模型的 API 调用，仅需一个 Key 即可通用各大 AI，省去同时维护多个 API Key 的麻烦，同时我们坚持提供比官方更低的价格

## 域名与认证

<Tip>
  注意以下示例需要将 Authorization 中换成实际的登录码
</Tip>

<Tip>
  主域名：`https://ai.liaobots.work`<br />
  备用域名：`https://ai.liaobots1.work`、`https://ai.liaobots2.work`、`https://ai.liaobots3.work`<br />
  切换域名时保留原路径后缀不变，例如 `/v1`、`/v1beta`
</Tip>

## 查询余额

通过 API 查询当前登录码的积分余额，返回总额度和剩余余额。

以下两个路径等价，任选其一即可：`/api/v1/credits` 与 `/v1/credits`。

```shell Curl theme={null}
curl 'https://ai.liaobots.work/api/v1/credits' \
  -H 'Authorization: Bearer <authcode>'

# 等价写法
curl 'https://ai.liaobots.work/v1/credits' \
  -H 'Authorization: Bearer <authcode>'
```

返回示例：

```json theme={null}
{
  "data": {
    "balance": 85.5,
    "amount": 100.0
  }
}
```

| 字段        | 说明     |
| --------- | ------ |
| `balance` | 剩余可用积分 |
| `amount`  | 累计总额度  |

### 查询积分使用记录

在查询余额接口后添加 `include_usage=true` 可以同时返回详细扣款记录。接口支持通过 `pageSize` 控制每页数量，通过返回的 `nextId` 继续翻页。

```shell Curl theme={null}
curl 'https://ai.liaobots.work/api/v1/credits?include_usage=true&pageSize=50' \
  -H 'Authorization: Bearer <authcode>'
```

返回示例：

```json theme={null}
{
  "data": {
    "balance": 85.5,
    "amount": 100.0,
    "usage": {
      "pageSize": 50,
      "nextId": "1234567890",
      "list": [
        {
          "id": 1234567891,
          "tokens": 1200,
          "input_tokens": 800,
          "output_tokens": 400,
          "model": "gpt-5.4",
          "price": 0.12,
          "cache_hit_tokens": 0,
          "cache_creation_tokens": 0,
          "create_time": 1760000000000
        }
      ]
    }
  }
}
```

| 字段                                   | 说明               |
| ------------------------------------ | ---------------- |
| `usage.pageSize`                     | 本次返回的每页数量        |
| `usage.nextId`                       | 下一页游标，为空表示没有更多记录 |
| `usage.list[].model`                 | 消耗对应的模型或服务       |
| `usage.list[].price`                 | 本次扣除的积分          |
| `usage.list[].tokens`                | 本次总 token 数      |
| `usage.list[].input_tokens`          | 输入 token 数       |
| `usage.list[].output_tokens`         | 输出 token 数       |
| `usage.list[].cache_hit_tokens`      | 命中缓存的 token 数    |
| `usage.list[].cache_creation_tokens` | 创建缓存的 token 数    |
| `usage.list[].create_time`           | 扣款时间，Unix 毫秒时间戳  |

## OpenAI 格式（Chat Completions）

### 流式响应

<CodeGroup>
  ```shell Curl theme={null}
  curl --location 'https://ai.liaobots.work/v1/chat/completions' \
  --header 'Authorization: Bearer <authcode>' \
  --header 'Content-Type: application/json' \
  --data '{
      "model": "gpt-5.4",
      "messages": [
          {
              "role": "user",
              "content": "Hi"
          }
      ],
      "temperature": 1,
      "stream": true
  }'
  ```

  ```python Python theme={null}
  from openai import OpenAI

  client = OpenAI(
      api_key='authcode',
      base_url='https://ai.liaobots.work/v1'
  )


  stream = client.chat.completions.create(
      model="gpt-5.4",
      messages=[
          {
              "role": "user",
              "content": "Hi"
          }
      ],
      stream=True,
  )


  for event in stream:
      if event.choices[0].delta.content:
          print(event.choices[0].delta.content)
  ```
</CodeGroup>

### 非流式响应

<CodeGroup>
  ```shell Curl theme={null}
  curl --location 'https://ai.liaobots.work/v1/chat/completions' \
  --header 'Authorization: Bearer <authcode>' \
  --header 'Content-Type: application/json' \
  --data '{
      "model": "gpt-5.4",
      "messages": [
          {
              "role": "user",
              "content": "Hi"
          }
      ],
      "temperature": 1,
      "stream": false
  }'
  ```

  ```python Python theme={null}
  from openai import OpenAI

  client = OpenAI(
      api_key='authcode',
      base_url='https://ai.liaobots.work/v1'
  )


  stream = client.chat.completions.create(
      model="gpt-5.4",
      messages=[
          {
              "role": "user",
              "content": "Hi"
          }
      ],
      stream=False,
  )


  print(stream.choices[0].message.content)
  ```
</CodeGroup>

## Claude 格式（Anthropic Messages）

### 流式响应

<CodeGroup>
  ```shell Curl theme={null}
  curl 'https://ai.liaobots.work/v1/messages' \
    -H 'x-api-key: <authcode>' \
    -H 'anthropic-version: 2023-06-01' \
    -H 'Content-Type: application/json' \
    -d '{
      "model": "claude-sonnet-4-6",
      "max_tokens": 1024,
      "stream": true,
      "messages": [
          {
              "role": "user",
              "content": "Hi"
          }
      ]
  }'
  ```

  ```python Python theme={null}
  import anthropic

  client = anthropic.Anthropic(
      api_key='authcode',
      base_url='https://ai.liaobots.work'
  )

  with client.messages.stream(
      model="claude-sonnet-4-6",
      max_tokens=1024,
      messages=[
          {
              "role": "user",
              "content": "Hi"
          }
      ],
  ) as stream:
      for text in stream.text_stream:
          print(text, end="")
  ```
</CodeGroup>

### 非流式响应

<CodeGroup>
  ```shell Curl theme={null}
  curl 'https://ai.liaobots.work/v1/messages' \
    -H 'x-api-key: <authcode>' \
    -H 'anthropic-version: 2023-06-01' \
    -H 'Content-Type: application/json' \
    -d '{
      "model": "claude-sonnet-4-6",
      "max_tokens": 1024,
      "messages": [
          {
              "role": "user",
              "content": "Hi"
          }
      ]
  }'
  ```

  ```python Python theme={null}
  import anthropic

  client = anthropic.Anthropic(
      api_key='authcode',
      base_url='https://ai.liaobots.work'
  )

  message = client.messages.create(
      model="claude-sonnet-4-6",
      max_tokens=1024,
      messages=[
          {
              "role": "user",
              "content": "Hi"
          }
      ],
  )

  print(message.content[0].text)
  ```
</CodeGroup>

## OpenAI Responses API

### 流式响应

<CodeGroup>
  ```shell Curl theme={null}
  curl 'https://ai.liaobots.work/v1/responses' \
    -H 'Authorization: Bearer <authcode>' \
    -H 'Content-Type: application/json' \
    -d '{
      "model": "gpt-5.4",
      "stream": true,
      "input": "Hi"
  }'
  ```

  ```python Python theme={null}
  from openai import OpenAI

  client = OpenAI(
      api_key='authcode',
      base_url='https://ai.liaobots.work/v1'
  )

  stream = client.responses.create(
      model="gpt-5.4",
      input="Hi",
      stream=True,
  )

  for event in stream:
      if hasattr(event, 'delta') and event.delta:
          print(event.delta, end="")
  ```
</CodeGroup>

### 非流式响应

<CodeGroup>
  ```shell Curl theme={null}
  curl 'https://ai.liaobots.work/v1/responses' \
    -H 'Authorization: Bearer <authcode>' \
    -H 'Content-Type: application/json' \
    -d '{
      "model": "gpt-5.4",
      "input": "Hi"
  }'
  ```

  ```python Python theme={null}
  from openai import OpenAI

  client = OpenAI(
      api_key='authcode',
      base_url='https://ai.liaobots.work/v1'
  )

  response = client.responses.create(
      model="gpt-5.4",
      input="Hi",
  )

  print(response.output_text)
  ```
</CodeGroup>

## Gemini 格式（Gemini 原生）

### 非流式响应

```shell Curl theme={null}
curl "https://ai.liaobots.work/v1beta/models/gemini-3-pro-preview(或其他模型):generateContent" \
  -H "x-goog-api-key: <authcode>" \
  -H 'Content-Type: application/json' \
  -X POST \
  -d '{
    "contents": [
      {
        "role": "system",
        "parts": [
          {
            "text": "You are a helpful assistant. Reply in Chinese."
          }
        ]
      },
      {
        "role": "user",
        "parts": [
          {
            "text": "Explain how AI works in a few words"
          }
        ]
      }
    ]
  }'
```

### 流式响应

```shell Curl theme={null}
curl "https://ai.liaobots.work/v1beta/models/gemini-3-pro-preview(或其他模型):streamGenerateContent?alt=sse" \
  -H "x-goog-api-key: <authcode>" \
  -H 'Content-Type: application/json' \
  -X POST \
  -d '{
    "contents": [
      {
        "role": "system",
        "parts": [
          {
            "text": "You are a helpful assistant. Reply in Chinese."
          }
        ]
      },
      {
        "role": "user",
        "parts": [
          {
            "text": "Explain how AI works in a few words"
          }
        ]
      }
    ]
  }'
```

## Gemini Nano Banana 2/Pro

### 非流式响应

```shell Curl theme={null}
curl "https://ai.liaobots.work/v1beta/models/gemini-3-pro-image-preview:generateContent" \
  -H "x-goog-api-key: <authcode>" \
  -H 'Content-Type: application/json' \
  -X POST \
  -d '{
      "contents": [
          {
              "parts": [
                  {
                      "text": "Generate a visualization of the current weather in Tokyo."
                  }
              ]
          }
      ],
      "tools": [
          {
              "googleSearch": {}
          }
      ],
      "generationConfig": {
          "imageConfig": {
              "aspectRatio": "1:1",
              "imageSize": "2K"
          },
          "responseModalities":
          [
              "TEXT",
              "IMAGE"
          ]
      }
  }
```

### 流式响应

```shell Curl theme={null}
curl "https://ai.liaobots.work/v1beta/models/gemini-3-pro-image-preview:streamGenerateContent?alt=sse" \
  -H "x-goog-api-key: <authcode>" \
  -H 'Content-Type: application/json' \
  -X POST \
  -d '{
      "contents": [
          {
              "parts": [
                  {
                      "text": "Generate two dogs in manga style."
                  }
              ]
          }
      ],
      "generationConfig": {
          "imageConfig": {
              "aspectRatio": "16:9",
              "imageSize": "1K"
          },
          "responseModalities":
          [
              "TEXT",
              "IMAGE"
          ]
      }
  }
```
