Additional ops docs:
auth2api is a lightweight OAuth-to-API proxy that turns your own upstream logins into local API endpoints.
It currently supports:
anthropic: Claude OAuthcodex: OpenAI "Sign in with ChatGPT" OAuthcursor: experimental Cursor account integration
The project is intentionally small: one proxy, a few upstream providers, multi-account routing, and admin tooling for local/self-hosted use.
- Exposes OpenAI-compatible endpoints:
POST /v1/chat/completions,POST /v1/responses,GET /v1/models - Exposes Anthropic-compatible endpoints:
POST /v1/messages,POST /v1/messages/count_tokens - Supports multiple accounts per provider with automatic account selection, failover, cooldown, refresh, and sticky routing
- Supports per-account routing hints such as
bias,level, andproxy, persisted alongside tokens - Routes requests by model family instead of forcing one upstream for everything
- Supports API key tiers, per-key concurrency limits, and per-key 5-hour request quotas
- Supports scheduled/manual usage refresh, request tracing, daily HTML reports, and email delivery
- Supports per-account outbound proxy routing plus VPS/nginx deployment helpers
- Includes admin endpoints for account snapshots, routing inspection, API key management, stats, reload, and daily reports
- Includes request tracing and observability helpers for debugging slow or failing traffic
- Node.js 20+
- At least one upstream account:
- Claude account for
anthropic - ChatGPT Plus/Pro for
codex - Cursor desktop/browser login for
cursor(experimental)
- Claude account for
git clone https://github.com/wangguohao/auth2api.git
cd auth2api
npm install
npm run buildPick the provider with --provider=. Default is anthropic.
# Claude OAuth
node dist/index.js --login
# ChatGPT / Codex OAuth
node dist/index.js --login --provider=codex
# ChatGPT / Codex OAuth on a remote machine
node dist/index.js --login --provider=codex --manual
# Cursor browser login (experimental)
node dist/index.js --login --provider=cursor
# Import existing local Cursor desktop login
node dist/index.js --login --provider=cursor --cursor-import-local
node dist/index.js --login --provider=cursor --cursor-storage=/path/to/state.vscdbNotes:
- Anthropic and Codex support browser callback login.
--manualis useful on remote servers. - Cursor uses a different browser deep-link flow or local token import.
- Running
--loginmultiple times adds more accounts to the same provider pool. - Successful login triggers
POST /admin/reloadautomatically if the server is already running.
You can persist per-account routing hints during login:
node dist/index.js --login --provider=codex --routingExtra='{"bias":1,"level":"pro"}'
node dist/index.js --login --provider=codex --routingExtra='{"bias":1,"level":"pro","proxy":"http://127.0.0.1:7890"}'Supported fields:
bias: adjusts account-selection prioritylevel:liteorproproxy: per-account HTTP(S) proxy URL, for examplehttp://127.0.0.1:7890
These values are stored with the token file and are reloaded by POST /admin/reload.
Notes:
proxycurrently uses a standard HTTP(S) proxy endpoint, which maps cleanly to Clash'shttp-portormixed-port- Selecting a specific Clash node in the UI does not give you a node-specific URL to paste into auth2api; in practice you usually point auth2api at Clash's local proxy endpoint such as
http://127.0.0.1:7890 - If you need one auth2api account to stay on one egress route, that isolation has to be enforced on the Clash side; if multiple accounts share the same Clash port and you only flip the global Selector, they will all move together
- Cursor's main chat path still uses a custom direct HTTP/2 transport;
routingExtra.proxyalready covers most fetch-based flows (Anthropic/Codex upstream, refresh, models/usage), but not yet the primary Cursor chat stream
node dist/index.jsDefault address:
http://127.0.0.1:8317
On first start, a bootstrap admin key is generated automatically and written to config.yaml if bootstrap-admin-key is empty.
If you run behind nginx, the server trusts one reverse-proxy hop and uses X-Forwarded-For for the client IP. This keeps the built-in IP rate limit meaningful instead of collapsing everything to 127.0.0.1.
Provider routing is model-driven:
cursor-*andcr/*always go tocursorgpt-5*,o*, andcodex-*go tocodexclaude-*and Anthropic aliases go toanthropic- unknown models fall back to
anthropic
If cursor is the only provider with logged-in accounts, all model names are routed to Cursor automatically, including plain claude-* or gpt-* names. This makes Cursor-only deployments usable with off-the-shelf Anthropic/OpenAI clients without adding a cursor- prefix.
GET /v1/models only lists models for providers that currently have accounts loaded.
Do not hardcode the README model table as the source of truth:
- Codex model availability is account-dependent and fetched dynamically
- Cursor model availability may come from its internal model list, with fallbacks when unavailable
- Anthropic/Cursor aliases and translation behavior evolve over time
POST /v1/chat/completionsPOST /v1/responsesGET /v1/models
POST /v1/messagesPOST /v1/messages/count_tokens
| Endpoint | anthropic | codex | cursor |
|---|---|---|---|
POST /v1/chat/completions |
Yes | Yes | Yes |
POST /v1/responses |
Yes | Yes | Yes |
POST /v1/messages |
Yes | Yes | Yes |
POST /v1/messages/count_tokens |
Yes | 501 |
501 |
Implementation notes:
codexis normalized through the internal Responses translator so Chat, Responses, and Anthropic-style requests can all share the upstream Codex backend.cursoronly supports streaming upstream; non-stream clients are aggregated locally where needed.- Cursor-backed
POST /v1/messagesis re-encoded as Anthropic Messages SSE for Claude Code compatibility.
Copy config.example.yaml to config.yaml and adjust it:
host: ""
port: 8317
auth-dir: "~/.auth2api"
bootstrap-admin-key: "sk-bootstrap-admin"
api-key-tier-limits:
lite:
concurrency: 5
max-requests-5h: 300
pro:
concurrencyMultiplier: 2
max-requests-multiplier: 2
admin:
concurrencyMultiplier: 2
max-requests-multiplier: 2
api-key-rate-limit:
window-ms: 18000000
max-requests: 300
overrides:
sk-special-key:
window-ms: 18000000
max-requests: 600
body-limit: "200mb"
timeouts:
messages-ms: 120000
stream-messages-ms: 600000
count-tokens-ms: 30000
stats:
enabled: true
observability:
enabled: false
trace:
enabled: true
retentionDays: 14
report:
enabled: false
scheduleHour: 2
timezone: "Asia/Shanghai"
retentionDays: 14
recipients:
- "[email protected]"
mail:
provider: "resend"
resend:
apiKey: "re_xxx"
from: "auth2api Report <[email protected]>"
debug: "off"
cloaking:
cli-version: "2.1.88"
entrypoint: "cli"
codex:
originator: "codex_cli_rs"
cli-version: "0.125.0"
cursor:
client-version: "2.3.41"
client-type: "ide"
agent-base-url: "https://api2.cursor.sh"
api-base-url: "https://api2.cursor.sh"Important config areas:
auth-dir: token, API key, stats, and observability storageapi-key-tier-limits: per-tier concurrency and quota limitsapi-key-rate-limit: per-client key request window; supports per-key overrides by exact API keystats.enabled: enables request statistics persisted instats.jsonlobservability.*: enables trace collection and daily HTML reportsmail.*: used by scheduled or manual daily report deliverycloaking.*: upstream request fingerprint/version settingsdebug:off,errors, orverbose
All /v1/* endpoints require an API key.
Supported auth headers:
Authorization: Bearer <key>x-api-key: <key>
Admin endpoints require an enabled admin tier key.
API keys are stored in:
<auth-dir>/api-keys.json
All /admin/* routes require an admin API key.
| Endpoint | Description |
|---|---|
GET /admin/accounts |
account snapshot by provider |
POST /admin/accounts/usage/refresh |
refresh usage for all or selected accounts |
GET /admin/accounts/decision |
inspect provider/account routing decisions |
GET /admin/stats |
aggregated request stats |
GET /admin/api-keys |
list API keys |
POST /admin/api-keys |
create API key |
POST /admin/api-keys/:id/enable |
enable API key |
POST /admin/api-keys/:id/disable |
disable API key |
POST /admin/reload |
reload tokens and API keys from disk |
POST /admin/reports/daily |
generate daily HTML report and optionally send email |
GET /admin/stats supports:
date=YYYY-MM-DD- or
start_date=YYYY-MM-DD&end_date=YYYY-MM-DD - optional
locale=zh-CN
Limits:
- date range must stay within the last 7 days
- maximum span is 7 days
When observability is enabled:
- traces are written to
<auth-dir>/observability/traces/trace-YYYY-MM-DD.jsonl - daily reports are written to
<auth-dir>/observability/reports/daily-YYYY-MM-DD.html
Helper scripts:
npm run obs:slow -- --date YYYY-MM-DD --limit 20
npm run obs:trace -- --date YYYY-MM-DD --trace-id <trace-id>
npm run obs:explain -- --date YYYY-MM-DD --trace-id <trace-id>
npm run obs:report -- --date YYYY-MM-DD [--send-email]
npm run auth:error-summary -- --date YYYY-MM-DD
npm run auth:error-trace -- --date YYYY-MM-DDThese scripts help inspect:
- slow requests
- a single trace event
- heuristic latency explanations
- manual daily report generation
- repeated auth/refresh failures
For a fresh Ubuntu 22.04 x64 VPS, the repo includes helper scripts:
npm run deploy:node20
npm run deploy:clone-install
npm run deploy:nginx -- your-domain.example
npm run deploy:mihomo
npm run deploy:mihomo-instance -- \
--source-yaml /path/to/source.yaml \
--proxy-name "Japan-TY-4" \
--instance-name jp-ty-4 \
--port 7890Notes:
deploy:nginxconfigures nginx for HTTP/1.1 proxying so SSE/streaming works correctlydeploy:mihomoanddeploy:mihomo-instanceare optional helpers for per-account proxy egress setupsdeploy:mihomo-instancerequires four mandatory arguments: source YAML path, exact proxy node name, instance name, and local port- the fuller proxy migration and operations notes live in docs/vps-proxy-runbook.md
curl http://127.0.0.1:8317/v1/chat/completions \
-H "Authorization: Bearer <your-api-key>" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4-5",
"messages": [
{ "role": "user", "content": "Hello" }
]
}'Build and run:
docker compose up --build -dThe included image starts with:
node dist/index.js --config=/config/config.yamlIf you use Docker, make sure config.yaml points auth-dir to a writable container path such as /data, because the compose file mounts:
./config.yaml:/config/config.yaml- volume
auth-data:/data
npm run dev
npm run build
npm testcodexrelays your ChatGPT account session and quota; use it only for your own account and at your own risk.cursorsupport is reverse-engineered and experimental; upstream protocol or version-gating changes may break it at any time.