CLIProxyAPI中转站搭建
CLIProxyAPI是一个模型聚合中转站,支持OAuth和API两种方式接入大模型。
github地址:https://github.com/router-for-me/CLIProxyAPI/tree/main
为什么要使用中转站?
众所周知,国外的主流大模型:Claude、GPT、Gemini等都不支持直接在国内使用。不仅注册账号需要科学上网,如果使用的国外IP不稳定,即使购买了他们的套餐,也有很大概率面临封号的风险。而使用美国的稳定IP搭建一个中转站,客户端通过中转站使用大模型不用科学上网,也能降低封号的风险。
为什么选择CLIProxyAPI
我对比了Sub2API、NewAPI、CliProxyAPI这几个比较流行的开源中转站。Sub2API最近封号比较严重,可能是某些请求头透传导致大模型厂商风控,NewAPI不支持OAuth登录,最后选择了CLIProxyAPI。
这里采用docker compose搭建,比较方便。
1.创建docker-compose.yml
services:
cli-proxy-api:
image: 'eceasy/cli-proxy-api:latest'
volumes:
- './auth-dir:/root/.cli-proxy-api'
- './config.yaml:/CLIProxyAPI/config.yaml'
- './logs:/CLIProxyAPI/logs'
ports:
- '8317:8317'
2.克隆项目源码(可选)
只需要项目中的config.example.yaml文件,可以不克隆整个项目,直接去github获取文件内容即可
git clone git@github.com:router-for-me/CLIProxyAPI.git
3.创建config.yaml
在docker-compose.yml同级目录创建config.yaml。
方式一:复制源码中的config.example.yaml文件,重命名为config.yaml
cp CLIProxyAPI/config.example.yaml config.yaml
方式二:创建config.yaml,从github中找到config.example.yaml,将文件内的内容粘贴到config.yaml中。
4.修改config.yaml中的配置
开启管理功能远程访问,并配置访问密码。
主要修改两项:
- allow-remote改为
true,开启web远程管理功能。 - 配置secret-key,该选项为web管理页面的登录密码。如果不配置,无法登录web。
# Management API settings
remote-management:
# Whether to allow remote (non-localhost) management access.
# When false, only localhost can access management endpoints (a key is still required).
allow-remote: true
# Management key. If a plaintext value is provided here, it will be hashed on startup.
# All management requests (even from localhost) require this key.
# Leave empty to disable the Management API entirely (404 for all /v0/management routes).
secret-key: "xxxxx"
# Disable the bundled management control panel asset download and HTTP route when true.
disable-control-panel: false
# Disable automatic periodic background updates of the management panel from GitHub (default: false).
# When enabled, the panel is only downloaded on first access if missing, and never auto-updated afterward.
# disable-auto-update-panel: false
# GitHub repository for the management control panel. Accepts a repository URL or releases API URL.
panel-github-repository: "https://github.com/router-for-me/Cli-Proxy-API-Management-Center"
5.启动docker容器
docker compose up -d
6.访问web管理页面
访问地址:http://IP:8317/management.html
管理密钥就是上面配置的secret-key。
7.删除默认的API key
配置面板-认证配置中,将原先的3个默认API key删除,再生成一个新的API key。
注意:每次修改配置,都要点底部的√来保存配置。
8.开启使用统计(可选)
配置面板-系统配置,开启使用统计。
9.配置模型(重要!)
有两种方式来配置大模型。一般推荐使用OAuth登录,像OpenAI和Claude,注册免费账号后,都会有免费额度可以使用,但只支持OAuth,不支持API调用。如果要薅免费羊毛,只能使用OAuth。
9.1 方式一:OAuth登录(推荐)
选择对应的厂商,登录已有的账号即可。(需要先自行注册大模型厂商的账号)
通过OAuth登录各家的账号之后,CLIProxyAPI会将登录认证文件保存,就可以直接使用CLIProxyAPI的API key在codex、claude code中调用大模型了。
9.2 方式二:配置API秘钥
如果购买了各平台的套餐,可以配置API秘钥来使用。
10.客户端使用
10.1 codex
10.1.1 安装codex
npm install -g @openai/codex
安装完成后,启动一次codex,到提示是否要登录的界面就可以ctrl + c退出。这一步是为了生成codex目录。
codex
10.1.2 配置文件
创建codex配置文件~/.codex/config.toml。
model_provider = "cliproxyapi"
model = "gpt-5.4" #模型ID
model_reasoning_effort = "high"
[model_providers.cliproxyapi]
name = "cliproxyapi"
base_url = "http://IP:8317/v1"
wire_api = "responses"
- base_url填写CLIProxyAPI的endpoint
创建秘钥配置文件~/.codex/auth.json。
{
"OPENAI_API_KEY": "sk-xxx"
}
- OPENAI_API_KEY是固定名,不要修改。秘钥的值填写CLIProxyAPI web管理页面中,配置面板-认证配置里的API秘钥。
配置完成后,进入codex就可以使用大模型了。
10.2 claude code
10.2.1 安装claude code
npm install -g @anthropic-ai/claude-code
安装完成后,启动一次claude code,到提示是否要登录的界面就可以ctrl + c退出。这一步是为了生成codex目录。
claude
10.2.2 配置文件
创建claude code配置文件~/.claude/settings.json
{
"env": {
"ANTHROPIC_AUTH_TOKEN": "YOUR_API_KEY",
"ANTHROPIC_BASE_URL": "http://IP:8317",
"ANTHROPIC_DEFAULT_HAIKU_MODEL": "claude-haiku-4-5-20251001",
"ANTHROPIC_DEFAULT_OPUS_MODEL": "claude-opus-4-6",
"ANTHROPIC_DEFAULT_SONNET_MODEL": "claude-sonnet-4-6",
"ANTHROPIC_MODEL": "claude-sonnet-4-6"
}
}
- ANTHROPIC_AUTH_TOKEN配置CLIProxyAPI web管理页面中,配置面板-认证配置里的API秘钥。
- ANTHROPIC_BASE_URL填写CLIProxyAPI的endpoint。
- 模型ID:claude-sonnet-4-6、claude-opus-4-6、claude-haiku-4-5-20251001
创建~/.claude.json
{
"hasCompletedOnboarding": true
}
该步骤可避免启动Claude Code时报错:Unable to connect to Anthropic services。
配置完成后,就可以进入claude code使用大模型了。