大模型 API 接入文档

YTokenHub 网关完全兼容 OpenAI 接口协议:把 BaseURL 换成本站的 /v1、API Key 换成控制台创建的密钥,现有代码零改造即可切换任意模型。

鉴权方式

所有 /v1 接口通过请求头携带 API Key:

Authorization: Bearer <sk-...>
为安全起见,密钥仅在创建时展示一次(服务端只保存 SHA-256 哈希)。 密钥可设置额度上限与有效期,随时可禁用或删除。
可用接口
方法路径说明
POST /v1/chat/completions 对话补全,支持 stream=true(SSE 流式)与 stream=false
GET /v1/models 当前可调用模型列表(同样需要携带 API Key)
完整可用模型与实时单价见 模型价格表; 传入未上架或不存在的模型会返回 404 model_not_found。
调用示例

cURL

curl http://www.yziss.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-xxxxxxxx" \
  -d '{
    "model": "deepseek-chat",
    "messages": [{"role": "user", "content": "你好"}],
    "stream": false
  }'

Python(官方 OpenAI SDK)

from openai import OpenAI

client = OpenAI(
    api_key="sk-xxxxxxxx",
    base_url="http://www.yziss.com/v1",
)
resp = client.chat.completions.create(
    model="deepseek-chat",
    messages=[{"role": "user", "content": "你好"}],
)
print(resp.choices[0].message.content)

Node.js

const resp = await fetch(
  "http://www.yziss.com/v1/chat/completions",
  {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "Authorization": "Bearer sk-xxxxxxxx",
    },
    body: JSON.stringify({
      model: "deepseek-chat",
      messages: [{ role: "user", content: "你好" }],
    }),
  }
);
const data = await resp.json();
console.log(data.choices[0].message.content);
流式输出(SSE)

传 stream=true 时网关逐块透传 SSE 分片,以 data: [DONE] 结束; 上游兼容 OpenAI / Anthropic / Gemini 三种协议,网关会统一转换为 OpenAI 分片格式,客户端无需区分厂商。

stream = client.chat.completions.create(
    model="deepseek-chat",
    messages=[{"role": "user", "content": "写一首短诗"}],
    stream=True,
)
for chunk in stream:
    print(chunk.choices[0].delta.content or "", end="")
错误码
HTTPcode含义与处理
400invalid_request请求体不合法(如缺少 model / messages)
401invalid_api_key密钥不存在、已禁用或已过期
402insufficient_balance余额不足,请前往「钱包」充值
404model_not_found模型未上架,见价格表
429rate_limit_exceeded超过单密钥每分钟调用上限
502upstream_error上游渠道全部失败(不扣费,可重试)
503no_available_channel该模型当前无可用渠道

要点速览

  • BaseURL:http://www.yziss.com/v1
  • 鉴权:Bearer sk-...
  • 计费:按 Token 用量,输入/输出分开计价
  • 流式:支持 SSE
  • 失败不扣费,自动切换渠道重试