快速开始
先在个人控制台创建 API Key,再将 Key 放入服务端环境变量。不要把 Key 放入浏览器代码。
设置环境变量
export MENGBI_ROUTER_API_KEY="sk-mengbi-xxxxxx"
export MENGBI_ROUTER_BASE_URL="https://router-api.mengbi-ai.net/v1"Note文档中的 sk-mengbi-xxxxxx 只是占位符,请替换为你自己的个人 Key。
第一条 OpenAI 兼容请求
POST /v1/chat/completionscurl https://router-api.mengbi-ai.net/v1/chat/completions \
-H "Authorization: Bearer sk-mengbi-xxxxxx" \
-H "content-type: application/json" \
-d '{
"model": "gpt-5.6-sol",
"messages": [{"role": "user", "content": "你好"}]
}'from openai import OpenAI
client = OpenAI(
api_key="sk-mengbi-xxxxxx",
base_url="https://router-api.mengbi-ai.net/v1",
)
response = client.chat.completions.create(
model="gpt-5.6-sol",
messages=[{"role": "user", "content": "你好"}],
)
print(response.choices[0].message.content)模型列表
使用模型 ID 调用模型。模型目录会展示当前可用且已发布价格的模型,接入前建议先读取接口返回的实时列表。
读取可用模型
GET /v1/modelscurl https://router-api.mengbi-ai.net/v1/models \
-H "Authorization: Bearer sk-mengbi-xxxxxx"{
"object": "list",
"data": [
{"id": "gpt-5.6-sol", "object": "model", "owned_by": "mengbi"},
{"id": "claude-sonnet-5", "object": "model", "owned_by": "anthropic"}
]
}Note模型是否可用、模型 ID 和价格可能变化。请不要在客户端永久缓存完整模型列表。
Anthropic 原生格式
Claude 模型建议使用 Anthropic 原生协议,以保留消息结构、流式事件和模型能力。
基础请求
POST /v1/messagescurl https://router-api.mengbi-ai.net/v1/messages \
-H "x-api-key: sk-mengbi-xxxxxx" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-sonnet-5",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "用一句话介绍你自己"}]
}'from anthropic import Anthropic
client = Anthropic(
api_key="sk-mengbi-xxxxxx",
base_url="https://router-api.mengbi-ai.net",
)
response = client.messages.create(
model="claude-sonnet-5",
max_tokens=1024,
messages=[{"role": "user", "content": "你好"}],
)
print(response.content[0].text)流式请求
在请求体中加入 stream: true,响应使用 text/event-stream。客户端应持续读取事件,不要用一次性 JSON 解析器处理流式响应。
{
"model": "claude-sonnet-5",
"max_tokens": 1024,
"stream": true,
"messages": [{"role": "user", "content": "写一首五言绝句"}]
}Note常见事件顺序为 message_start、content_block_delta、message_delta、message_stop。
OpenAI 兼容格式
适用于 OpenAI SDK、Codex 以及支持 Chat Completions 的大多数客户端。
Chat Completions
POST /v1/chat/completionscurl https://router-api.mengbi-ai.net/v1/chat/completions \
-H "Authorization: Bearer sk-mengbi-xxxxxx" \
-H "content-type: application/json" \
-d '{
"model": "gpt-5.6-sol",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "Hello"}]
}'import OpenAI from 'openai'
const client = new OpenAI({
apiKey: process.env.MENGBI_ROUTER_API_KEY,
baseURL: 'https://router-api.mengbi-ai.net/v1',
})
const response = await client.chat.completions.create({
model: 'gpt-5.6-sol',
messages: [{ role: 'user', content: '你好' }],
})
console.log(response.choices[0].message.content)流式请求
在 Chat Completions 请求中加入 stream: true,返回标准 OpenAI SSE 分片,并以 data: [DONE] 结束。
{
"model": "gpt-5.6-sol",
"stream": true,
"messages": [{"role": "user", "content": "写一段介绍"}]
}Responses API
Responses API 面向已采用 OpenAI Responses SDK 的应用,目前仅用于 GPT 系列模型。
基础请求
POST /v1/responsescurl https://router-api.mengbi-ai.net/v1/responses \
-H "Authorization: Bearer sk-mengbi-xxxxxx" \
-H "content-type: application/json" \
-d '{
"model": "gpt-5.6-sol",
"input": "用一句话介绍你自己"
}'from openai import OpenAI
client = OpenAI(
api_key="sk-mengbi-xxxxxx",
base_url="https://router-api.mengbi-ai.net/v1",
)
response = client.responses.create(
model="gpt-5.6-sol",
input="用一句话介绍你自己",
)
print(response.output_text)NoteClaude 请使用 /v1/messages,Gemini 请使用 Gemini 原生格式。将不兼容的模型发送到 /v1/responses 会返回错误。
快速模式
如果模型目录标记某个模型支持快速模式,可以在 Chat Completions 或 Responses API 中加入 service_tier: fast。具体可用性以模型目录和接口响应为准。
{
"model": "gpt-5.6-sol",
"input": "分析这个需求",
"service_tier": "fast"
}Gemini 原生格式
Gemini CLI 和 Gemini 原生 SDK 可以使用 Gemini 风格的路径与请求体。
生成内容
POST /v1beta/models/{model}:generateContentcurl https://router-api.mengbi-ai.net/v1beta/models/gemini-3.6-flash:generateContent \
-H "Authorization: Bearer sk-mengbi-xxxxxx" \
-H "content-type: application/json" \
-d '{
"contents": [{
"role": "user",
"parts": [{"text": "用一句话介绍你自己"}]
}]
}'{
"candidates": [{
"content": {"role": "model", "parts": [{"text": "你好!"}]},
"finishReason": "STOP"
}],
"usageMetadata": {
"promptTokenCount": 12,
"candidatesTokenCount": 22
}
}流式请求与环境变量
流式调用使用 streamGenerateContent,并在查询参数中加入 alt=sse。不同 Gemini 客户端对自定义 Base URL 的字段名可能不同。
curl "https://router-api.mengbi-ai.net/v1beta/models/gemini-3.6-flash:streamGenerateContent?alt=sse" \
-H "Authorization: Bearer sk-mengbi-xxxxxx" \
-H "content-type: application/json" \
-d '{"contents": [{"role": "user", "parts": [{"text": "你好"}]}]}'export GOOGLE_GEMINI_BASE_URL="https://router-api.mengbi-ai.net"
export GEMINI_API_KEY="sk-mengbi-xxxxxx"
export GEMINI_API_KEY_AUTH_MECHANISM="bearer"