Skip to main content

Authentication Overview

CLI Proxy API supports multiple authentication methods to access AI providers:
  1. OAuth 2.0 - Use your existing subscriptions (Gemini, Claude, Codex, etc.)
  2. API Keys - Traditional API key authentication (Gemini, Claude, OpenAI-compatible)
  3. Service Accounts - Google Cloud service accounts (Vertex AI)

Client Authentication

Clients authenticate to CLI Proxy API using API keys configured in config.yaml:
config.yaml
Clients include the API key in the Authorization header:

OAuth Authentication

OAuth providers require interactive authentication through a web browser.

OAuth Flow

PKCE (Proof Key for Code Exchange)

CLI Proxy API uses PKCE for enhanced OAuth security:
PKCE prevents authorization code interception attacks by:
  1. Generating a random code_verifier
  2. Creating a SHA256 hash as code_challenge
  3. Sending challenge in auth request
  4. Sending verifier in token exchange

Provider-Specific OAuth

Gemini CLI OAuth

Gemini uses standard Google OAuth with:
  • Standard OAuth 2.0 flow
  • Refresh tokens for automatic renewal
  • Multi-account support via separate auth files

Claude Code OAuth

Claude OAuth includes:
  • PKCE for security
  • Organization UUID and account email in token response
  • Custom TLS fingerprinting to bypass Cloudflare

OpenAI Codex OAuth

Codex OAuth features:
  • JWT-based session tokens
  • Organization membership tracking
  • Refresh token rotation

Other OAuth Providers

Qwen Code:
iFlow (Z.ai GLM):
Antigravity:
Kimi:

Token Storage

Authentication tokens are stored in the auth directory (~/.cli-proxy-api by default):

Token File Format

Each provider has a specific token structure:

File Watching

CLI Proxy API watches the auth directory for changes using fsnotify:
  • Add new file → Credential becomes available immediately
  • Modify file → Token updates applied automatically
  • Delete file → Credential removed from rotation
No server restart required.

Token Refresh

The auth.Manager automatically refreshes tokens before they expire:
sdk/cliproxy/auth/conductor.go
Refresh behavior:
  1. Background checks every 5 seconds
  2. Proactive refresh before expiry (varies by provider)
  3. Concurrent refresh up to 16 tokens at once
  4. Failure backoff 5 minutes on error
  5. Pending backoff 1 minute if already refreshing

Multi-Account Authentication

CLI Proxy API supports multiple accounts per provider:

Adding Multiple Accounts

Each account is a separate file with unique credentials.

Account Selection

The routing strategy determines which account is used: Round-robin (default):
config.yaml
Rotates through accounts evenly. Fill-first:
config.yaml
Uses first account until quota exceeded. See Routing for details.

Account Prefixes

You can target specific accounts using prefixes:
config.yaml

API Key Authentication

Providers that support API keys can be configured directly:

Gemini API Keys

config.yaml

Claude API Keys

config.yaml

OpenAI-Compatible Providers

config.yaml

Authentication Attributes

You can add metadata to credentials:
~/.cli-proxy-api/gemini_oauth_email@gmail.com.json
Attributes:
  • priority: Higher priority credentials selected first (default: 0)
  • websockets: Enable WebSocket API access (default: false)
  • description: Human-readable label (not used by system)

Quota & Cooldown

When a credential hits quota limits:
  1. Error detected (HTTP 429 or quota exceeded message)
  2. Credential enters cooldown state
  3. Request automatically retries with next credential
  4. After cooldown period, credential returns to rotation
Cooldown uses exponential backoff:
sdk/cliproxy/auth/conductor.go
Configure behavior:
config.yaml

Security Considerations

Token Storage

  • Tokens stored in plaintext files
  • Use file permissions to restrict access: chmod 600 ~/.cli-proxy-api/*
  • Consider encrypting the auth directory at rest

API Keys

  • Use environment variables: CLI_PROXY_API_KEY=sk-your-key
  • Rotate keys regularly
  • Use different keys for different clients/teams

Management API

config.yaml
See Management API for securing the management interface.

Next Steps

OAuth Setup

Set up OAuth for each provider

API Keys

Configure API key providers

Multi-Account

Manage multiple accounts

Routing

Learn about credential selection