快速开始
本指南将帮助您在几分钟内完成 TopRouter API 的首次调用。
前置条件
🌐 API 基础地址
所有 API 请求都使用以下基础地址:
https://toprouter.cc使用 OpenAI SDK(推荐)
TopRouter 完全兼容 OpenAI SDK,您只需修改 base_url 即可使用。
Python
安装 OpenAI Python SDK:
bash
pip install openai发起聊天补全请求:
python
from openai import OpenAI
client = OpenAI(
base_url="https://toprouter.cc",
api_key="your-api-key" # 替换为您的 API Key
)
# 普通请求
response = client.chat.completions.create(
model="anthropic/claude-4.6-sonnet",
messages=[
{"role": "system", "content": "你是一个有帮助的助手。"},
{"role": "user", "content": "请用一句话介绍人工智能。"}
]
)
print(response.choices[0].message.content)流式响应:
python
from openai import OpenAI
client = OpenAI(
base_url="https://toprouter.cc",
api_key="your-api-key"
)
# 流式请求
stream = client.chat.completions.create(
model="openai/gpt-5.5",
messages=[
{"role": "user", "content": "写一首关于春天的短诗。"}
],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)TypeScript / Node.js
安装 OpenAI Node.js SDK:
bash
npm install openai发起请求:
typescript
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'https://toprouter.cc',
apiKey: 'your-api-key', // 替换为您的 API Key
});
async function main() {
// 普通请求
const response = await client.chat.completions.create({
model: 'anthropic/claude-4.6-sonnet',
messages: [
{ role: 'system', content: '你是一个有帮助的助手。' },
{ role: 'user', content: '请用一句话介绍人工智能。' },
],
});
console.log(response.choices[0].message.content);
}
main();流式响应:
typescript
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'https://toprouter.cc',
apiKey: 'your-api-key',
});
async function streamExample() {
const stream = await client.chat.completions.create({
model: \'openai/gpt-5.5\',
messages: [{ role: 'user', content: '写一首关于春天的短诗。' }],
stream: true,
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content;
if (content) {
process.stdout.write(content);
}
}
}
streamExample();使用 cURL
聊天补全
bash
curl https://toprouter.cc/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-api-key" \
-d '{
"model": "anthropic/claude-4.6-sonnet",
"messages": [
{"role": "user", "content": "你好!"}
]
}'流式响应
bash
curl https://toprouter.cc/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-api-key" \
-d '{
"model": "openai/gpt-5.5",
"messages": [
{"role": "user", "content": "写一首短诗"}
],
"stream": true
}'查看可用模型
bash
curl https://toprouter.cc/models \
-H "Authorization: Bearer your-api-key"使用直接 HTTP 请求
如果您不使用 OpenAI SDK,也可以直接发起 HTTP 请求:
python
import requests
url = "https://toprouter.cc/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer your-api-key"
}
payload = {
"model": "anthropic/claude-4.6-sonnet",
"messages": [
{"role": "user", "content": "你好!"}
]
}
response = requests.post(url, json=payload, headers=headers)
data = response.json()
print(data["choices"][0]["message"]["content"])⚠️ 注意事项
- 请妥善保管您的 API Key,不要在客户端代码中暴露
- 建议使用环境变量存储 API Key
- 所有请求都需要通过 HTTPS 发送
