API 调用
对应官方文档「接口调用示例」。开发者可以直接通过 HTTP 请求调用 OpenAI 兼容接口,支持 cURL、Python 和 Node.js。
关键配置
接入步骤
- 1选择 OpenAI 兼容格式,将客户端或 SDK 的 Base URL 设置为中转站的Base URL。
- 2在请求头中加入 Authorization Bearer 令牌,令牌本身已包含 sk- 前缀,不要重复添加。
- 3按需开启 stream: true 获取流式输出。
- 4Python 使用 openai 包;Node.js 使用 openai 包。
配置示例
cURL
bash
curl "中转站的Base URL/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-xxxxxxxxxxxxxxxx" \
-d '{
"model": "claude-sonnet-4-20250514",
"messages": [
{ "role": "user", "content": "介绍一下你自己" }
],
"stream": true
}'Node.js
js
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env.API_KEY,
baseURL: '中转站的Base URL',
});
const stream = await client.chat.completions.create({
model: 'claude-sonnet-4-20250514',
messages: [{ role: 'user', content: '介绍一下你自己' }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || '');
}Python
python
from openai import OpenAI
import os
client = OpenAI(
api_key=os.environ["API_KEY"],
base_url="中转站的Base URL",
)
response = client.chat.completions.create(
model="claude-sonnet-4-20250514",
messages=[{"role": "user", "content": "介绍一下你自己"}],
)
print(response.choices[0].message.content)注意事项
- Python 依赖安装: pip install openai。
- Node.js 依赖安装: npm install openai。
- 示例令牌需要替换成实际 API 令牌。