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

# 通过 API 直接调用

> 使用 Bash、PowerShell 或 Python 发起一次 OpenAI 兼容聊天请求。

# 通过 API 直接调用

以下示例调用聊天补全接口。你需要有效的 [API 密钥](https://crypto-serve.mintlify.site/zhang-hao-shi-yong-jiao-cheng-mao-li-she-qu/cryptoserveai/api-keys.md)、足够额度，以及平台当前提供且支持该接口的模型 ID。示例中的占位符必须替换，教程不指定固定模型。

| 项目 | 值 |
| - | - |
| Base URL | `https://api.cryptoserve.io/v1` |
| 聊天端点 | `https://api.cryptoserve.io/v1/chat/completions` |
| 请求方式 | `POST` |
| 认证 | `Authorization: Bearer <API密钥>` |

任选下面一种方式即可。测试可能产生模型调用费用；先发简短问题，不要直接提交长文件。

## macOS / Linux：Bash 与 curl

在 Bash 中执行。密钥使用隐藏输入，避免把真实值写进命令历史。输入模型 ID 后，命令会发送一次请求。

```bash theme={null}
read -r -s -p 'CryptoServeAI API Key: ' CRYPTOSERVE_API_KEY
printf '\n'
read -r -p 'Model ID: ' CRYPTOSERVE_MODEL
export CRYPTOSERVE_API_KEY CRYPTOSERVE_MODEL

curl --silent --show-error --max-time 120 \
  https://api.cryptoserve.io/v1/chat/completions \
  -H 'Content-Type: application/json' \
  -H "Authorization: Bearer $CRYPTOSERVE_API_KEY" \
  --data "{\"model\":\"$CRYPTOSERVE_MODEL\",\"messages\":[{\"role\":\"user\",\"content\":\"Hello!\"}]}"
```

正常的文本回复位于返回 JSON 的 `choices[0].message.content`。模型 ID 请直接从平台复制，不填其他文本。

## Windows：PowerShell 7

以下密钥输入方式需要 PowerShell 7。在 PowerShell 中执行，不要粘贴到命令提示符窗口。

```powershell theme={null}
$env:CRYPTOSERVE_API_KEY = Read-Host 'CryptoServeAI API Key' -MaskInput
$env:CRYPTOSERVE_MODEL = Read-Host 'Model ID'
$body = @{
    model = $env:CRYPTOSERVE_MODEL
    messages = @(@{ role = 'user'; content = 'Hello!' })
} | ConvertTo-Json -Depth 4

$result = Invoke-RestMethod `
    -Uri 'https://api.cryptoserve.io/v1/chat/completions' `
    -Method Post `
    -Headers @{ Authorization = "Bearer $env:CRYPTOSERVE_API_KEY" } `
    -ContentType 'application/json; charset=utf-8' `
    -Body ([System.Text.Encoding]::UTF8.GetBytes($body)) `
    -TimeoutSec 120
$result.choices[0].message.content
```

## Python：无需安装额外依赖

将下面内容保存为 `test_api.py`，运行 `python test_api.py`。程序会提示输入密钥和模型 ID，不会将密钥写入文件。

```python theme={null}
import getpass
import json
import urllib.error
import urllib.request

api_key = getpass.getpass('CryptoServeAI API Key: ')
model = input('Model ID: ').strip()
payload = {
    'model': model,
    'messages': [{'role': 'user', 'content': 'Hello!'}],
}
request = urllib.request.Request(
    'https://api.cryptoserve.io/v1/chat/completions',
    data=json.dumps(payload).encode('utf-8'),
    headers={
        'Authorization': f'Bearer {api_key}',
        'Content-Type': 'application/json',
    },
    method='POST',
)
try:
    with urllib.request.urlopen(request, timeout=120) as response:
        result = json.load(response)
    print(result['choices'][0]['message']['content'])
except urllib.error.HTTPError as error:
    print('HTTP', error.code)
    print(error.read().decode('utf-8', errors='replace'))
```

## 怎样判断成功？

收到模型回复后，在[用量与调用记录](https://crypto-serve.mintlify.site/zhang-hao-shi-yong-jiao-cheng-mao-li-she-qu/cryptoserveai/usage-logs.md)中核对时间、模型及消耗。仅能打开 API 域名首页，不代表模型调用成功。

如返回错误，按[常见问题](https://crypto-serve.mintlify.site/zhang-hao-shi-yong-jiao-cheng-mao-li-she-qu/cryptoserveai/faq.md)检查；请求超时后先核对平台记录，再决定是否重试，避免重复调用。分享终端输出前检查是否包含敏感信息。

这些示例用于聊天补全，不适用于图像生成或其他协议。接口格式参考 [New API 调用说明](https://docs.newapi.pro/zh/docs/guide/feature-guide/user/api)，当前可用能力以 CryptoServeAI 模型说明为准。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.