Skip to main content

Anthropic Claude API 配置管理工具,支持多环境切换

Project description

Claude Switch

A Python CLI tool for managing multiple Anthropic Claude API configurations with seamless environment switching.

Features

  • Multi-configuration management: Handle multiple Anthropic API configurations
  • One-click switching: Switch between development, testing, and production environments
  • Dual authentication: Support for both auth_token and api_key authentication methods
  • Automatic environment activation: Environment variables automatically生效 after configuration switch
  • Secure storage: Configuration file permission protection with encrypted credential storage
  • Interactive navigation: Arrow key navigation with color highlighting
  • Backup mechanism: Automatic configuration file backup to prevent data loss

Installation

pip Installation (Recommended)

pip install claude-switch

# Or using pipx (recommended to avoid dependency conflicts)
pipx install claude-switch

From Source

git clone https://github.com/your-username/claude-switch.git
cd claude-switch

# Using uv (recommended)
uv pip install -e .

# Or using pip
pip install -e .

Getting Started

Initialization

After installation, initialize shell integration:

cs init

Initialization will:

  • Automatically detect your shell environment (bash/zsh/fish)
  • Generate dynamic shell configuration files
  • Add configuration to your shell profile
  • Optionally add the first configuration

Restart your terminal or run:

source ~/.claude-switch/cs-wrapper.sh

Verification

# Check version
cs --version

# View help
cs --help

# Add first configuration
cs add

Commands

Interactive Operations

  • cs - Interactive configuration selection (arrow key navigation)
  • cs add - Interactive new configuration addition

Management Commands

  • cs list - List all configurations
  • cs use <name> - Switch to specified configuration
  • cs current - Display current configuration
  • cs edit <name> - Edit configuration information
  • cs remove <name> - Remove configuration
  • cs add-credential <config> - Add credentials to specified configuration

Environment Export

  • cs current --export - Output executable export commands

Usage Examples

Adding a Configuration

# Interactive configuration addition
cs add
# Enter configuration name, Base URL, and description

# Add credentials to configuration
cs add-credential work

Viewing and Managing Configurations

# View all configurations
cs list
# Output:
# → work - 工作账号 [https://api.anthropic.com] (0tokens, 1keys) [key1]
#   personal - 个人账号 [https://api.anthropic.com] (1tokens, 0keys) [token1]

# Interactive selection
cs
# Use arrow keys to select, press Enter to confirm

# Quick switch
cs use work

Configuration Management

# Edit configuration information
cs edit work

# View current configuration
cs current
# Output: 当前配置: work - 工作账号 [https://api.anthropic.com] (0tokens, 1keys) [key1]

# Export environment variables
cs current --export
# Output: export ANTHROPIC_BASE_URL="..."; export ANTHROPIC_API_KEY="..."

Configuration Structure

Configuration is stored in ~/.claude-switch/config.json:

{
  "version": "2.0",
  "active": "work",
  "configs": {
    "work": {
      "base_url": "https://api.anthropic.com",
      "api_keys": ["sk-xxx"],
      "auth_tokens": [],
      "note": "工作账号",
      "active_key": 0,
      "active_auth": -1
    },
    "personal": {
      "base_url": "https://api.anthropic.com",
      "api_keys": [],
      "auth_tokens": ["sk-yyy"],
      "note": "个人账号",
      "active_key": -1,
      "active_auth": 0
    }
  }
}

Interactive Interface Features

  • Cursor highlighting: Current selection line highlighted in green
  • Status indicators: Green arrow (→) indicates active configuration
  • Rich information: Shows configuration name, description, URL, credential count and type
  • Arrow navigation: Supports up/down arrow key selection with Enter confirmation

Security Features

  • File permissions: Configuration file automatically set to 0o600 (user-readable only)
  • Sensitive information masking: Automatic credential masking during display
  • Automatic backup: Automatic backup creation when configuration file is corrupted
  • Version compatibility: Automatic configuration version upgrade handling

Advanced Usage

Manual Configuration Editing

You can directly edit the configuration file ~/.claude-switch/config.json. The system automatically handles format errors and creates backups.

Environment Variable Settings

# Automatic activation (requires initialization)
cs use production

# Manual activation
eval $(cs current --export)

Shell Integration

After initialization, the following aliases are automatically available:

  • cs - Main command
  • csu - Quick configuration switching
  • csc - View current configuration
  • css - Manual environment variable reload

Troubleshooting

Installation Issues

Problem: cs command not found

# Check if installed correctly
pip list | grep claude-switch

# Check PATH settings
echo $PATH

# Reinstall
pip install --force-reinstall claude-switch

Problem: Permission errors

# Use user installation
pip install --user claude-switch

# Or use pipx
pipx install claude-switch

Initialization Issues

Problem: Shell integration not working

# Manually source configuration file
source ~/.claude-switch/cs-wrapper.sh

# Check if configuration files exist
ls -la ~/.claude-switch/

# Reinitialize
cs init

Problem: Environment variables not taking effect

# Manually load environment variables
source ~/.claude-switch/env.sh

# Or use export command
eval $(cs current --export)

Compatibility

  • Supported Python versions: 3.8+
  • Supported operating systems: macOS, Linux, Windows
  • Supported shells: bash, zsh, fish
  • Supported installation methods: pip, pipx, conda, uv

Getting Help

If you encounter issues:

  1. View cs --help for command assistance
  2. Check configuration files in ~/.claude-switch/ directory
  3. Submit an Issue on GitHub: https://github.com/your-username/claude-switch/issues

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

claude_switch-1.0.0.tar.gz (24.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

claude_switch-1.0.0-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

File details

Details for the file claude_switch-1.0.0.tar.gz.

File metadata

  • Download URL: claude_switch-1.0.0.tar.gz
  • Upload date:
  • Size: 24.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for claude_switch-1.0.0.tar.gz
Algorithm Hash digest
SHA256 cc1c7320971a9d7a37a50bdee11cdb6662cd13aa21423a5fa773c17f85b73379
MD5 cbcda7897f16f8f53538c9cc93bced29
BLAKE2b-256 b2dcf71790af955f7a0cd8ed5e72d78bbb2fcaf29a3b5671c932f1decc368ffe

See more details on using hashes here.

File details

Details for the file claude_switch-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: claude_switch-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 18.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for claude_switch-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cc8f16115cb7372d610bf25fb0a7e9edc07eec4beea2351638da94490252e393
MD5 62a39647f66ecb9243633fa414063847
BLAKE2b-256 4726aa6d382e3608779ed113d5c7fd2ac6380cb2d17c57f5578c1b3428c5d07f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page