现象

用 POST /api/channel/ 新增渠道(new-api v1.0.0-rc.37 实测):

请求体返回
直接传渠道对象 {"name":...,"type":1,...}{"message":"channel cannot be empty"}
传数组 [{...}]cannot unmarshal array into Go value of type controller.AddChannelRequest
{"mode":"single","channel":{...}}{"message":"","success":true}

报错信息误导性很强——它说的是"渠道为空",实际是外层包装不对,解析成了空结构体。

鉴权

new-api 新版管理接口不用 cookie,用 JWT:

  1. POST /api/user/login,body {"username":"...","password":"..."}
    → 返回 data.access_token(JWT,带过期时间)
  2. 之后所有管理请求带两个头:
    Authorization: Bearer <access_token> 和 New-Api-User: <用户id>(管理员一般是 1)

只带 cookie 会返回 AUTH_UNAUTHORIZED: Unauthorized, invalid access token。

可用示例

TOK=$(curl -s -H "Content-Type: application/json" \
  -d '{"username":"admin","password":"<pw>"}' \
  http://127.0.0.1:3000/api/user/login \
  | python3 -c "import sys,json;print(json.load(sys.stdin)['data']['access_token'])")

cat > ch.json <<'JSON'
{"mode":"single","channel":{
  "name":"GeminiWeb-LA","type":1,
  "key":"sk-xxxx","base_url":"http://127.0.0.1:8083",
  "models":"gemini-3.5-flash-lite,gemini-3.6-flash",
  "group":"default","weight":0,"priority":0,"status":1,"auto_ban":1}}
JSON

curl -X POST -H "Authorization: Bearer $TOK" -H "New-Api-User: 1" \
  -H "Content-Type: application/json" --data-binary @ch.json \
  http://127.0.0.1:3000/api/channel/
  • models 是逗号分隔字符串,不是数组
  • 单渠道测试:GET /api/channel/test/<id>?model=<model>,返回 {"success":true,"time":3.48}
  • 结尾少斜杠会 307 重定向

渠道 model 重叠的隐式分流

多个渠道声明同一个模型名(例如两个渠道都写 gemini-3.5-flash-lite)时,
new-api 会按权重/优先级自行分流,请求不保证落在你以为的那个渠道。

判断实际命中的渠道:看响应 id 的形态(不同上游生成风格不同,例如
chatcmpl-xxxx vs 上游原生 id)。要严格区分就在渠道上配 model_mapping 映射成独有名字。

关联


本文归档于个人知识库 · 领域:CS & AI · 模块:04 Project_CS_AI/AI apps