调用 API
New API 提供 OpenAI 兼容 接口。任何支持 OpenAI 的 SDK 或工具,只要把基础地址(Base URL)指向本站、把密钥换成你的令牌即可使用。
基础信息
| 项 | 值 |
|---|---|
| Base URL | https://api.laobai.dev/v1 |
| 认证方式 | 请求头 Authorization: Bearer YOUR_TOKEN |
| 接口风格 | OpenAI 兼容(/v1/chat/completions 等) |
将
YOUR_TOKEN替换为你的令牌。
cURL
bash
curl https://api.laobai.dev/v1/chat/completions \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o-mini",
"messages": [{"role": "user", "content": "用一句话介绍你自己"}]
}'Python(openai SDK)
python
from openai import OpenAI
client = OpenAI(
api_key="YOUR_TOKEN",
base_url="https://api.laobai.dev/v1",
)
resp = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "你好!"}],
)
print(resp.choices[0].message.content)Node.js(openai SDK)
js
import OpenAI from 'openai'
const client = new OpenAI({
apiKey: 'YOUR_TOKEN',
baseURL: 'https://api.laobai.dev/v1',
})
const resp = await client.chat.completions.create({
model: 'gpt-4o-mini',
messages: [{ role: 'user', content: '你好!' }],
})
console.log(resp.choices[0].message.content)流式响应
设置 stream: true 即可获得增量输出(SSE),用法与 OpenAI 一致。
常见错误
| 状态码 | 含义 | 处理 |
|---|---|---|
| 401 | 令牌无效 | 检查 Authorization 头与令牌是否正确 |
| 403 | 无权访问该模型 | 确认令牌 / 分组的可用模型范围 |
| 429 | 触发限流 | 降低请求频率后重试 |
| insufficient quota | 额度不足 | 前往 充值 |