ccr(Claude Code Router)配置多apikey的插件

2026-04-29 09:463阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐
问题描述:

前言

ccr好用,但是一个提供商只能配置一个apikey。
于是想着写一个插件实现可以一次配置多个apikey。
最终效果:
在ccr ui页面api秘钥位置填写多个以英文逗号分割的apikey,就能每次使用不同的apikey。
image640×719 18.2 KB

详细教程

1. 将下面代码放到任意位置:

module.exports = class { name = "apikey"; constructor() { this.map = {}; } async transformRequestIn(request, provider) { const apiKeys = provider.apiKey; const arr = apiKeys.split(","); let apiKey = arr[0]; if (arr.length > 1) { // 存在多个api key时 const key = provider.name; const index = this.map[key] || 0; apiKey = arr[index].trim(); this.map[key] = (index + 1) % arr.length; } return { body: request, config: { headers: { Authorization: apiKey.startsWith("Bearer ") ? apiKey : `Bearer ${apiKey}`, }, }, }; } async transformResponseOut(response) { // Pass through response unchanged return response; } };

2. ccr ui右下角添加自定义转化器(或者直接在配置文件中配置)


路径填刚刚保存文件的路径,建议"/"分割
image513×287 5.25 KB

3. 给需要配置多apikey的提供商选择"apikey"转换器

image642×725 21.2 KB
确定有"apikey"转换器
image640×259 3.52 KB

4. api秘钥填写由英文逗号分割的apikey,如:

image642×497 9.14 KB

5. 效果:

postman发请求:
image614×332 14.3 KB
第一次用第一个apikey
image280×170 2.92 KB
第二次用第二个apikey
image274×172 2.89 KB

网友解答:
--【壹】--:

前言

ccr好用,但是一个提供商只能配置一个apikey。
于是想着写一个插件实现可以一次配置多个apikey。
最终效果:
在ccr ui页面api秘钥位置填写多个以英文逗号分割的apikey,就能每次使用不同的apikey。
image640×719 18.2 KB

详细教程

1. 将下面代码放到任意位置:

module.exports = class { name = "apikey"; constructor() { this.map = {}; } async transformRequestIn(request, provider) { const apiKeys = provider.apiKey; const arr = apiKeys.split(","); let apiKey = arr[0]; if (arr.length > 1) { // 存在多个api key时 const key = provider.name; const index = this.map[key] || 0; apiKey = arr[index].trim(); this.map[key] = (index + 1) % arr.length; } return { body: request, config: { headers: { Authorization: apiKey.startsWith("Bearer ") ? apiKey : `Bearer ${apiKey}`, }, }, }; } async transformResponseOut(response) { // Pass through response unchanged return response; } };

2. ccr ui右下角添加自定义转化器(或者直接在配置文件中配置)


路径填刚刚保存文件的路径,建议"/"分割
image513×287 5.25 KB

3. 给需要配置多apikey的提供商选择"apikey"转换器

image642×725 21.2 KB
确定有"apikey"转换器
image640×259 3.52 KB

4. api秘钥填写由英文逗号分割的apikey,如:

image642×497 9.14 KB

5. 效果:

postman发请求:
image614×332 14.3 KB
第一次用第一个apikey
image280×170 2.92 KB
第二次用第二个apikey
image274×172 2.89 KB

问题描述:

前言

ccr好用,但是一个提供商只能配置一个apikey。
于是想着写一个插件实现可以一次配置多个apikey。
最终效果:
在ccr ui页面api秘钥位置填写多个以英文逗号分割的apikey,就能每次使用不同的apikey。
image640×719 18.2 KB

详细教程

1. 将下面代码放到任意位置:

module.exports = class { name = "apikey"; constructor() { this.map = {}; } async transformRequestIn(request, provider) { const apiKeys = provider.apiKey; const arr = apiKeys.split(","); let apiKey = arr[0]; if (arr.length > 1) { // 存在多个api key时 const key = provider.name; const index = this.map[key] || 0; apiKey = arr[index].trim(); this.map[key] = (index + 1) % arr.length; } return { body: request, config: { headers: { Authorization: apiKey.startsWith("Bearer ") ? apiKey : `Bearer ${apiKey}`, }, }, }; } async transformResponseOut(response) { // Pass through response unchanged return response; } };

2. ccr ui右下角添加自定义转化器(或者直接在配置文件中配置)


路径填刚刚保存文件的路径,建议"/"分割
image513×287 5.25 KB

3. 给需要配置多apikey的提供商选择"apikey"转换器

image642×725 21.2 KB
确定有"apikey"转换器
image640×259 3.52 KB

4. api秘钥填写由英文逗号分割的apikey,如:

image642×497 9.14 KB

5. 效果:

postman发请求:
image614×332 14.3 KB
第一次用第一个apikey
image280×170 2.92 KB
第二次用第二个apikey
image274×172 2.89 KB

网友解答:
--【壹】--:

前言

ccr好用,但是一个提供商只能配置一个apikey。
于是想着写一个插件实现可以一次配置多个apikey。
最终效果:
在ccr ui页面api秘钥位置填写多个以英文逗号分割的apikey,就能每次使用不同的apikey。
image640×719 18.2 KB

详细教程

1. 将下面代码放到任意位置:

module.exports = class { name = "apikey"; constructor() { this.map = {}; } async transformRequestIn(request, provider) { const apiKeys = provider.apiKey; const arr = apiKeys.split(","); let apiKey = arr[0]; if (arr.length > 1) { // 存在多个api key时 const key = provider.name; const index = this.map[key] || 0; apiKey = arr[index].trim(); this.map[key] = (index + 1) % arr.length; } return { body: request, config: { headers: { Authorization: apiKey.startsWith("Bearer ") ? apiKey : `Bearer ${apiKey}`, }, }, }; } async transformResponseOut(response) { // Pass through response unchanged return response; } };

2. ccr ui右下角添加自定义转化器(或者直接在配置文件中配置)


路径填刚刚保存文件的路径,建议"/"分割
image513×287 5.25 KB

3. 给需要配置多apikey的提供商选择"apikey"转换器

image642×725 21.2 KB
确定有"apikey"转换器
image640×259 3.52 KB

4. api秘钥填写由英文逗号分割的apikey,如:

image642×497 9.14 KB

5. 效果:

postman发请求:
image614×332 14.3 KB
第一次用第一个apikey
image280×170 2.92 KB
第二次用第二个apikey
image274×172 2.89 KB