Skip to content

istarwyh/claude-code-router

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Claude-Code-Router

Universal API proxy for using Claude Code with multiple AI providers. Seamlessly translates between Anthropic's Claude API and OpenAI-compatible APIs.

✨ Features

  • πŸ”„ API Translation: Anthropic ↔ OpenAI format conversion
  • 🌍 Multi-Provider: OpenRouter, OpenAI, DeepSeek, Kimi, SiliconFlow
  • ⚑ Edge Computing: Cloudflare Workers for global performance
  • πŸ“‘ Streaming Support: Real-time response streaming
  • πŸ›‘οΈ Type Safety: Full TypeScript implementation

πŸš€ Quick Start

1. Install Claude Code

npm install -g @anthropic-ai/claude-code

2. Configure API Access

# Option A: Use shared instance (testing only)
export ANTHROPIC_BASE_URL="https://cc.xiaohui.cool"
export ANTHROPIC_API_KEY="your-provider-api-key"

# Option B: Deploy your own instance (recommended)
git clone https://github.com/your-username/claude-code-router
cd claude-code-router && wrangler deploy
export ANTHROPIC_BASE_URL="https://your-domain.workers.dev"

3. Start Using Claude Code

source ~/.bashrc && claude

Provider Setup

Provider API Key Source Base URL
OpenRouter openrouter.ai https://cc.xiaohui.cool
DeepSeek platform.deepseek.com Deploy with DEEPSEEK_BASE_URL
OpenAI platform.openai.com Deploy with OPENAI_BASE_URL

πŸ—οΈ Architecture

graph TB
    A[Claude Code Client] -->|Anthropic API Format| B[Claude-Code-Router]
    B -->|Provider Selection| C{Environment Config}
    C -->|OpenRouter| D[OpenRouter API]
    C -->|DeepSeek| E[DeepSeek API]
    C -->|OpenAI| F[OpenAI API]
    C -->|Others| G[Other OpenAI-Compatible APIs]
    
    B -->|Format Conversion| H[Request Translator]
    B -->|Response Processing| I[Response Translator]
    B -->|Streaming| J[SSE Handler]
    
    subgraph "Cloudflare Workers"
        B
        H
        I
        J
    end
    
    style A fill:#e1f5fe
    style B fill:#fff3e0
    style D fill:#f3e5f5
    style E fill:#e8f5e8
    style F fill:#fff8e1
    style G fill:#fce4ec
Loading

⚑ Technical Stack

Core Runtime

  • Platform: Cloudflare Workers (V8 Isolates)
  • Language: TypeScript with strict typing
  • Runtime: Edge-optimized JavaScript (no Node.js dependencies)
  • Deployment: Wrangler CLI + GitHub Actions

Architecture Patterns

Pattern Implementation Benefit
Edge Computing 300+ global locations <1ms cold start, 0ms warm start
Serverless Auto-scaling isolates 0 maintenance, infinite scale
Type Safety Full TypeScript coverage Runtime error prevention
Streaming Web Streams API Real-time response delivery
Modular Design Functional composition Easy testing & maintenance

Build System

  • Bundler: esbuild (10x faster than webpack)
  • Module System: ES6 with tree-shaking
  • Asset Pipeline: TypeScript β†’ JavaScript + type checking
  • Hot Reload: Wrangler dev server with instant updates

πŸ”§ Deployment

Development

git clone https://github.com/your-username/claude-code-router
cd claude-code-router
npm install && npm install -g wrangler
npm run dev    # Start development server

Production Deployment

# Configure environment variables
wrangler secret put OPENROUTER_BASE_URL       # OpenRouter backend
wrangler secret put DEEPSEEK_BASE_URL         # DeepSeek backend  
wrangler secret put OPENAI_BASE_URL           # OpenAI backend

# Deploy to Cloudflare Workers
npm run deploy

Environment Configuration

flowchart LR
    A[Environment Variables] --> B{Provider Selection}
    B -->|Priority 1| C[DEEPSEEK_BASE_URL]
    B -->|Priority 2| D[OPENAI_BASE_URL]
    B -->|Priority 3| E[KIMI_BASE_URL]
    B -->|Priority 4| F[SILICONFLOW_BASE_URL]
    B -->|Default| G[OPENROUTER_BASE_URL]
    
    C --> H[DeepSeek API]
    D --> I[OpenAI API]
    E --> J[Kimi API]
    F --> K[SiliconFlow API]
    G --> L[OpenRouter API]
Loading

πŸ”Œ API Reference

Request Format (Anthropic)

curl -X POST https://cc.xiaohui.cool/v1/messages \
  -H "Content-Type: application/json" \
  -H "x-api-key: your-api-key" \
  -d '{
    "model": "claude-3-5-sonnet-20241022",
    "messages": [{"role": "user", "content": "Hello"}],
    "max_tokens": 100,
    "stream": true
  }'

Model Mapping

Claude Model OpenRouter DeepSeek OpenAI
claude-3-5-haiku-20241022 anthropic/claude-3.5-haiku deepseek-chat gpt-4o-mini
claude-3-5-sonnet-20241022 anthropic/claude-3.5-sonnet deepseek-chat gpt-4o
claude-3-opus-20240229 anthropic/claude-3-opus deepseek-reasoner gpt-4o

πŸ“ Architecture & Structure

Worker Runtime Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        Cloudflare Workers                        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ πŸ”„ Request Router    β”‚ πŸ”„ Format Converter β”‚ πŸ“‘ Stream Handler  β”‚
β”‚ β€’ Path matching       β”‚ β€’ Anthropic β†’ OpenAI  β”‚ β€’ SSE processing    β”‚
β”‚ β€’ Method validation   β”‚ β€’ OpenAI β†’ Anthropic  β”‚ β€’ Chunk buffering   β”‚
β”‚ β€’ Auth handling       β”‚ β€’ Model mapping      β”‚ β€’ Error recovery    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

File Structure & Frontend Architecture

claude-code-router/
β”œβ”€β”€ πŸ“ src/
β”‚   β”œβ”€β”€ πŸ“ client/              # Frontend & documentation system (TypeScript source)
β”‚   β”‚   β”œβ”€β”€ πŸ“ bestPractices/    # Best practices module (development source)
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“ core/          # Manager & business logic
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“ data/          # Card data & configuration
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“ renderers/     # UI rendering components
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“ services/      # Content & markdown services
β”‚   β”‚   β”‚   └── πŸ“ index.ts       # Module entry point
β”‚   β”‚   └── πŸ“ howToImplement/   # Implementation guide module (development source)
β”‚   β”‚       β”œβ”€β”€ πŸ“ core/          # HowToImplementManager
β”‚   β”‚       β”œβ”€β”€ πŸ“ data/          # Card data & configuration
β”‚   β”‚       β”œβ”€β”€ πŸ“ handlers/      # Event handling
β”‚   β”‚       β”œβ”€β”€ πŸ“ renderers/     # UI rendering components
β”‚   β”‚       β”œβ”€β”€ πŸ“ services/      # Content services
β”‚   β”‚       └── πŸ“ index.ts       # Module entry point
β”‚   └── πŸ“ server/              # Worker runtime logic
β”œβ”€β”€ πŸ“ modules/                 # Static HTML templates + compiled JavaScript
β”‚   β”œβ”€β”€ πŸ“ best-practices/      # HTML template + bundled client code
β”‚   β”œβ”€β”€ πŸ“ how-to-implement/    # HTML template + bundled client code
β”‚   └── πŸ“ get-started/         # Static module components
β”œβ”€β”€ πŸ“ shared/                  # Cross-platform utilities
β”‚   β”œβ”€β”€ πŸ“ scripts/             # Generated bundles for runtime
β”‚   β”‚   └── πŸ“ generated/        # Auto-generated from src/client/*
β”‚   └── πŸ“ utils/               # Helper functions
β”œβ”€β”€ πŸ“ scripts/                 # Build automation & bundling
β”œβ”€β”€ πŸ”§ index.ts                 # Worker entry point (fetch handler)
β”œβ”€β”€ πŸ”§ formatRequest.ts         # Anthropic β†’ OpenAI transformer
β”œβ”€β”€ πŸ”§ formatResponse.ts        # OpenAI β†’ Anthropic transformer
β”œβ”€β”€ πŸ”§ streamResponse.ts        # Server-Sent Events handler
β”œβ”€β”€ πŸ”§ types.ts                 # TypeScript definitions
└── βš™οΈ wrangler.toml            # Worker configuration & bindings

Frontend Build Architecture

The project uses a dual-layer frontend architecture:

Development Layer (src/client/)

  • Purpose: Modern TypeScript development with full module structure
  • Architecture: Modular design (core, data, handlers, renderers, services)
  • Benefits: Type safety, code organization, maintainability
  • Build Target: Gets compiled and bundled by scripts/build-client.js

Runtime Layer (modules/)

  • Purpose: Production-ready HTML templates + compiled JavaScript
  • Architecture: Static HTML containers + bundled client code
  • Benefits: Single-file deployment, optimized for Cloudflare Workers
  • Source: Generated from development layer through build process

Build Process Flow

graph LR
    A[src/client/*/index.ts] -->|esbuild| B[Bundled JavaScript]
    B -->|Inject| C[modules/*/index.ts]
    C -->|Runtime| D[HTML + JS Module]
    E[scripts/build-client.js] -->|Orchestrates| A
    
    style A fill:#e3f2fd
    style B fill:#fff3e0  
    style C fill:#e8f5e8
    style D fill:#fce4ec
Loading

This approach ensures clean separation between development complexity and runtime efficiency.

Key Design Principles

  • 🌐 Edge-First: Optimized for Cloudflare's global network
  • πŸ”’ Zero Dependencies: No external runtime dependencies
  • ⚑ Performance: Sub-millisecond response times
  • πŸ”„ Streaming: Native Web Streams API support
  • πŸ›‘οΈ Type Safety: Full TypeScript coverage with strict mode

πŸ™ Acknowledgments

Built with inspiration from:

βš–οΈ License & Disclaimer

MIT License - Use at your own risk and discretion.

⚠️ Important: This is an independent tool, not affiliated with Anthropic, OpenAI, or OpenRouter. Users are responsible for compliance with all relevant Terms of Service and API usage policies.

About

A Simple Proxy enabling Claude Code to work with OpenRouter or Deepseek

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 98.7%
  • JavaScript 1.3%