sub2api_simple is a lightweight standalone gateway that exposes a unified API for multiple upstream AI accounts (Anthropic and OpenAI), with account pooling, failover, sticky sessions, and streaming support.
- Unified HTTP API for:
POST /v1/messages(Anthropic-compatible)POST /v1/responsesandPOST /responses(OpenAI-compatible)POST /v1/chat/completions(OpenAI Chat Completions-compatible)GET /v1/modelsGET /health
- Multi-account pool with:
- Priority-based scheduling
- Concurrency limits per account
- Sticky session routing
- Automatic failover on transient upstream errors
- OAuth and API key support:
- OpenAI OAuth browser login flow (
/auth/login) - OpenAI refresh token auto-refresh and persistence
- Anthropic/OpenAI API key mode
- OpenAI OAuth browser login flow (
- Streaming and non-streaming handling:
- SSE passthrough for streaming requests
- SSE aggregation for non-streaming clients when needed
- Go 1.21+
- Copy and edit config:
cp config.example.json config.json- Build:
- Windows (PowerShell):
./build.ps1- Linux/macOS:
bash ./build.shDefault output is output/.
- Run:
go run . -config config.jsonor run the built binary:
- Windows:
./output/sub2api_simple.exe -config config.json- Linux/macOS:
./output/sub2api_simple -config config.jsonClient requests must provide one of the configured auth_tokens:
Authorization: Bearer <token>- or
x-api-key: <token>
If an OpenAI OAuth account has no available token, the gateway logs a login URL on startup.
You can also open it manually:
http://localhost:8080/auth/login?account=<account-name>
The callback listener uses:
http://localhost:1455/auth/callback
See config.example.json for full options.
Main fields:
listen_addr: bind address (for example:8080)auth_tokens: client-facing API tokens for this gatewayaccounts: upstream account definitionsopenai_default_instructions: default instructions by model (supports*)model_extra_params: default extra params by model (supports*)enable_request_log: enable request/response error loggingenable_stream_debug_log: verbose stream line loggingenable_model_debug_log: log upstream model request payloads and downstream client responsesmax_account_switches: maximum failover switches per requeststicky_session_ttl: sticky routing TTL (duration string)stream_read_timeout: stream inactivity timeout (duration string)
Each account supports:
platform:anthropicoropenaitype:oauthorapi_key
Common auth patterns:
- Anthropic/OpenAI API key:
- set
type: "api_key"+api_key
- set
- OpenAI OAuth refresh:
- set
type: "oauth"+refresh_token
- set
- OpenAI OAuth browser login:
- set
type: "oauth"without token, then use/auth/login
- set
POST /v1/messages- Anthropic-compatible message API proxy
POST /v1/responses- OpenAI-compatible responses API proxy
POST /responses- Alias of
/v1/responses
- Alias of
POST /v1/chat/completions- OpenAI Chat Completions-compatible proxy (request/response format conversion)
GET /v1/models- Combined model list based on configured platforms
GET /health- Service and account runtime status
GET /auth/login- Start OpenAI OAuth browser flow
go test ./... -run ^$client_test.go expects a running gateway at http://localhost:8080:
go test ./....
├── main.go
├── internal/gateway
│ ├── oauth/ # token refresh, PKCE, OAuth session
│ ├── forwarder/ # failover policy and stream helpers
│ ├── router/ # endpoint dispatch
│ ├── claude/ # Claude request shaping
│ └── openai/ # OpenAI request shaping
├── cmd/chatclient # optional interactive client
├── build.ps1
├── build.sh
└── config.example.json
This project is licensed under the MIT License. See LICENSE.