claude-code-provider-switch
v1.4.0
Published
Cross-platform Claude Code provider switcher (OpenRouter, Ollama, Anthropic)
Downloads
2,717
Maintainers
Readme
Claude Code Provider Switcher
A powerful CLI tool that lets you seamlessly switch between different AI providers for Claude Code, including OpenRouter, Anthropic, MiniMax, Ollama, and the original Claude Code configuration.
🚀 Features
- Multi-Provider Support: Switch between OpenRouter, Anthropic, MiniMax, Ollama, and original Claude Code
- Enhanced Interactive Menu: Beautiful, intuitive interface with visual hierarchy and icons
- Centralized Configuration: Secure, centralized base URL management for all providers
- Security-First: Explicit base URL configuration prevents URL hijacking attacks
- Default Configuration: Set and save your preferred provider and model
- Model Selection: Choose from hundreds of models across different providers
- Auto-Restart: Automatically restarts with new configuration after changes
- Cross-Platform: Works on Windows, macOS, and Linux
- Global Installation: Install once and use anywhere
📦 Installation
Prerequisites
First, ensure you have Claude Code installed.
npm install -g @anthropic-ai/claude-codeGlobal Installation (Method 1)
# Install globally
npm install -g claude-code-provider-switchGlobal Installation (Method 2 - Alternative)
# Clone the repository
git clone <repository-url>
cd claude-code-provider-switch
# Install globally
npm install -g .
# Or use npm link for development (changes are immediate)
npm run link🎯 Quick Start
Basic Usage
# Show interactive menu with enhanced visual interface
claude-switch ui
# Use a specific provider
claude-switch openrouter
claude-switch anthropic
claude-switch minimax
claude-switch ollama
claude-switch original
# Use specific provider with model selection
claude-switch openrouter --model
claude-switch minimax --modelEnhanced Menu Interface
When you run claude-switch ui, you'll see a beautifully formatted interactive menu:
Claude Code Provider Switcher
Configuration: Global (~/.claude/.claude-switch-env)
Current default: OpenRouter ⭐ [DEFAULT] (openrouter/free)
Available providers:
❯ 1) OpenRouter ⭐ [DEFAULT] ── (openrouter, or, open)
2) Ollama ── (ollama, oll)
3) Minimax ── (minimax, min, mm)
4) Anthropic ── (anthropic, ant)
5) Original Claude Code ── (original, orig, def, d)
──────────────────────────────────────────────────────────
6) ⚙️ Set as Default
7) 🔑 Manage API Keys
8) ❓ Help
🎯 Navigation:
• Use ↑/↓ arrows to navigate options
• Press Enter to select highlighted option
• Press 1-8 for quick select
• Press ESC to exit
⭐ Tips:
• ⭐ indicates your current default provider
• 🌐 Providers: Launch AI models with different services
• 🔸 Management: Configure settings and preferencesConfiguration Management
# Set up default provider and model interactively
claude-switch set-default
# View current defaults
claude-switch show-defaults
# Clear all defaults
claude-switch clear-defaults
# Manage API keys interactively
claude-switch api-keys⚙️ Configuration
Security & Centralization
The CLI now features centralized base URL management for enhanced security:
- 🔒 Secure: All providers use explicitly configured base URLs from
constants.js - 🛡️ Anti-Hijacking: Prevents URL hijacking through malicious environment variables
- 📍 Centralized: Single source of truth for all provider configurations
- ✅ Consistent: Uniform behavior across all providers
Environment Variables
The CLI is designed to create a ~/.claude/.claude-switch-env (in your home directory) with this syntax:
# API Keys for different providers
OPENROUTER_AUTH_TOKEN=your_openrouter_token_here
ANTHROPIC_API_KEY=your_anthropic_key_here
MINIMAX_AUTH_TOKEN=your_minimax_token_here
OLLAMA_AUTH_TOKEN=your_ollama_token_here
# Optional: Default models for each provider
OPENROUTER_MODEL=openrouter/free
OLLAMA_MODEL=minimax-m2.5:cloud
ANTHROPIC_MODEL=claude-3-5-sonnet-latest
MINIMAX_MODEL=minimax-m2.7
# Default provider and model settings
DEFAULT_PROVIDER=default # Use 'default' to show menu on startup, or set to 'openrouter', 'anthropic', 'minimax', 'ollama', or 'original'
DEFAULT_MODEL=When you run the CLI for the first time, based on your selection, it will ask for the API keys or Auth Tokens and after this will be saved in the .env file for future use.
Global vs Local Configuration
The CLI supports two configuration modes:
Global Configuration (Default)
- Location:
~/.claude/.claude-switch-env(in your home directory) - Priority: Used when no local
.envfile exists - Benefit: Share configuration across all projects
- Creation: Automatically created on first run when no local
.envexists
Local Configuration
- Location:
.envfile in your project directory - Priority: Overrides global configuration when present
- Benefit: Project-specific settings
- Creation: Create manually or use "Save Configuration Locally" menu option
Configuration Display
The main menu shows which configuration source is active:
Configuration: Global (~/.claude/.claude-switch-en)
Configuration: Local (current_folder/.env)Menu Behavior
- No defaults set: Shows interactive menu for provider selection
- Defaults set: Auto-launches with configured provider
- Fresh install: Always shows menu until you set defaults
Switching Between Modes
- To Local: Use "Save Configuration Locally" option from main menu
- To Global: Delete local
.envfile to fall back to global config - Priority: Local always takes precedence over global when both exist
API Key Management
For easier API key management, use the interactive menu:
# Launch interactive API key management
claude-switch api-keysOr access it through the main menu (option 6) when you run claude-switch without arguments.
Features:
- 🔐 Secure: API keys are masked for display (shows only first 4 and last 4 characters)
- 🎯 Visual: Clear status indicators (✅/❌) show which providers have keys configured
- ⚡ Interactive: Arrow key navigation with visual selection indicators and icons
- 🔄 Flexible: Update, remove, or set new API keys interactively
- 🎨 Enhanced UI: Beautiful menu with visual hierarchy and intuitive navigation
Provider Setup
OpenRouter
- Get your API key from OpenRouter
- Run
claude-switch uito set the API key interactively - Select OpenRouter from the menu to browse and choose available models
Anthropic
- Get your API key from Anthropic Console
- Run
claude-switch uito set the API key interactively - Select Anthropic from the menu to browse and choose available models
MiniMax
- Get your API key from MiniMax Console
- Run
claude-switch uito set the API key interactively - Select MiniMax from the menu to browse and choose available minimal models
Ollama
- Install and run Ollama
- Run
claude-switch uito set the API key interactively (optional, for remote instances) - Select Ollama from the menu to see locally available models
📋 Commands Reference
Provider Commands
| Command | Description | Example |
| ------------ | ------------------------ | -------------------------- |
| openrouter | Use OpenRouter provider | claude-switch openrouter |
| anthropic | Use Anthropic provider | claude-switch anthropic |
| minimax | Use MiniMax provider | claude-switch minimax |
| ollama | Use Ollama provider | claude-switch ollama |
| original | Use original Claude Code | claude-switch original |
Model Selection
| Command | Description | Example |
| -------------------- | ------------------------- | ---------------------------------- |
| <provider> --model | Select model for provider | claude-switch openrouter --model |
| minimax --model | Select MiniMax model | claude-switch minimax --model |
Configuration Commands
| Command | Description | Example |
| ---------------- | ----------------------------- | ------------------------------ |
| set-default | Interactive default setup | claude-switch set-default |
| show-defaults | View current defaults | claude-switch show-defaults |
| clear-defaults | Reset all defaults | claude-switch clear-defaults |
| api-keys | Manage API keys interactively | claude-switch api-keys |
| help | Show help information | claude-switch --help |
| version | Show Version information | claude-switch --version |
Aliases
| Command | Aliases |
| ------------ | ------------------------------ |
| openrouter | or, open |
| anthropic | ant |
| minimax | min, mm |
| ollama | oll |
| original | original, orig, def, d |
⚠️ IMPORTANT: Extra Arguments with Default Provider
When a default provider is set, extra arguments are automatically passed to Claude Code:
claude-switch --continue # Continue previous conversation
claude-switch --resume # Resume last session
claude-switch -p "Explain what this project does" # Quick prompt without interactive mode
claude-switch -p "List all providers supported" --output-format json # Get structured JSON output
This means you can use Claude Code with your default provider seamlessly after setting a default provider, also passing extra arguments like --continue, --resume, etc.
⚠️ Important: Clearing Defaults
If the interactive menu doesn't show and Claude Code opens directly with a predefined provider, you need to clear the default settings:
claude-switch clear-defaultsWhy This Happens
When you use the "Set as Default" option, the application saves your provider and model choices to provide a faster experience. However, if you want to change providers or access the full menu again, you must clear these defaults first with claude-switch clear-defaults
🎨 Enhanced Menu Features
The interactive menu includes several visual enhancements:
Visual Elements
- Icons: Management options have intuitive icons (⚙️ Settings, 🔑 Keys, ❓ Help, etc.)
- Color Coding: Different colors for different option types
- Star Indicator: ⭐ shows your current default provider
- Separator Line: Clean visual separation between providers and management options
- Inline Aliases: Provider aliases displayed on the same line for cleaner layout
Navigation Improvements
- Arrow Keys: Smooth up/down navigation with visual feedback
- Quick Select: Press number keys for instant selection
- ESC Support: Exit the menu cleanly
- Visual Selection: Clear indicator (❯) shows currently selected option
🔧 Advanced Usage
Programmatic API
You can also use this package as a module in your Node.js applications:
const {
setDefaultProvider,
getDefaultProvider,
launchOpenRouter,
launchAnthropic,
showApiKeyMenu,
updateApiKey,
maskApiKey,
} = require("claude-code-provider-switch");
// Set default provider programmatically
await setDefaultProvider("openrouter");
// Get current provider
const current = getDefaultProvider();
console.log(`Current provider: ${current}`);
// Launch specific provider
await launchOpenRouter(false, [], "gpt-4");
// Manage API keys programmatically
await showApiKeyMenu(); // Show interactive API key menu
await updateApiKey({
id: "openrouter",
name: "OpenRouter",
envVar: "OPENROUTER_AUTH_TOKEN",
}); // Update specific API key
console.log(maskApiKey("sk-1234567890abcdef")); // "sk-1234...cdef"Development
# Install for development with symlink (changes are immediate)
npm run link
# Run directly from source
npm run dev -- --help
# Run tests
npm run test:all
# Unlink development version
npm run unlink
# Install production version
npm install -g .🐛 Troubleshooting
Common Issues
"API key required" error
- Check your
.envfile contains the correct API key - Ensure the
.envfile is in your current working directory
Menu selection not showing options, opens Claude Code directly
- Try clearing defaults:
claude-switch clear-defaults
File permission errors
- Ensure you have write permissions to create/modify the
.envfile - Try running with elevated permissions if necessary
Debug Mode
For debugging, you can run the tool directly from source:
node bin/claude-switch.js --help🤝 Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
Development Scripts
npm run dev- Run directly from sourcenpm run test:validation- Run validation testsnpm run test:provider- Run provider integration testsnpm run test:comprehensive- Run comprehensive testsnpm run test:all- Run all test suites
📄 License
MIT License - see LICENSE file for details
🔗 Related Projects
- Claude Code - Official Claude Code repository
- OpenRouter - Unified API for AI models
- Ollama - Local AI model deployment
📞 Support
If you encounter any issues or have questions:
- Check the troubleshooting section above
- Search existing GitHub issues
- Create a new issue with detailed information
- Include your OS, Node.js version, and error messages
Made with ❤️ by Adrian R
