Wrap Claude Code CLI as an HTTP API service, supporting calls through URL and API Key.
npm install -g @anthropic-ai/claude-codeclaude --version
which claudeEdit claude_server.py and update the Claude Code path:
def __init__(self, claude_path: str = "/path/to/your/claude"):
# Update this path to match your Claude Code installation
# Common paths:
# - "/usr/local/bin/claude" (global npm install)
# - "/home/codespace/nvm/current/bin/claude" (nvm)
# - "/home/username/.nvm/versions/node/v18.x.x/bin/claude" (nvm with specific version)To find your Claude Code path:
which claude
# or
whereis claudeexport ANTHROPIC_API_KEY="your-anthropic-api-key"
export CLAUDE_CODE_API_KEYS="your-custom-api-key-1,your-custom-api-key-2"pip install fastapi uvicorn requests pydanticclaude_server.py- HTTP server that wraps Claude Code CLIclaude_cli_client.py- Client for HTTP calls to servertest_server.py- Test scriptclaude_examples.py- Complete feature demonstrationsimple_claude_test.py- Simple test example
cd claude
python3 claude_server.pyThe server will start on http://localhost:8000 by default.
Output should show:
INFO: Started server process [12345]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
In another terminal:
cd claude
python3 test_server.pycd claude
python3 claude_examples.pyPOST /v1/messages
Content-Type: application/json
Authorization: Bearer your-api-key
{
"messages": [
{
"role": "user",
"content": "Hello, how are you?"
}
],
"max_tokens": 4000,
"model": "claude-3-sonnet-20240229"
}POST /v1/chat
Content-Type: application/json
Authorization: Bearer your-api-key
{
"message": "Write a Python function to calculate factorial",
"session_id": "optional-session-id",
"max_tokens": 1000
}POST /v1/code
Content-Type: application/json
Authorization: Bearer your-api-key
{
"prompt": "Create a REST API with FastAPI for user management",
"language": "python",
"complexity": "intermediate"
}POST /v1/file/analyze
Content-Type: application/json
Authorization: Bearer your-api-key
{
"file_path": "/path/to/your/file.py",
"task": "analyze and suggest improvements"
}GET /healthGET /infofrom claude_cli_client import ClaudeCodeClient
# Create client
client = ClaudeCodeClient(
api_key="your-api-key",
base_url="http://localhost:8000"
)
# Send message
response = client.send_message("Hello")
print(response)
# Programming task
code = client.code_task("Write a Python function to calculate Fibonacci sequence")
print(code)# Chat with session
response = client.chat(
message="Explain machine learning",
session_id="my-session",
max_tokens=2000
)
# Code generation
code = client.generate_code(
prompt="Create a web scraper using requests and BeautifulSoup",
language="python"
)
# File analysis
analysis = client.analyze_file(
file_path="./my_script.py",
task="find bugs and optimization opportunities"
)# Health check
curl http://localhost:8000/health
# Send message
curl -X POST http://localhost:8000/v1/chat \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-api-key" \
-d '{
"message": "Hello Claude!",
"max_tokens": 1000
}'
# Code generation
curl -X POST http://localhost:8000/v1/code \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-api-key" \
-d '{
"prompt": "Create a calculator class in Python",
"language": "python"
}'Edit claude_server.py to customize:
class ClaudeCodeWrapper:
def __init__(self,
claude_path: str = "/usr/local/bin/claude",
timeout: int = 300,
max_concurrent_requests: int = 10):
# Your configuration here# Required for Claude Code CLI
export ANTHROPIC_API_KEY="your-anthropic-api-key"
# Optional: Custom API keys for your wrapper service
export CLAUDE_CODE_API_KEYS="key1,key2,key3"
# Optional: Server configuration
export CLAUDE_WRAPPER_HOST="0.0.0.0"
export CLAUDE_WRAPPER_PORT="8000"
export CLAUDE_WRAPPER_LOG_LEVEL="INFO"-
Claude Code not found
Error: Claude Code CLI not found at path: /path/to/claudeSolution: Update the
claude_pathinclaude_server.pywith the correct path fromwhich claude. -
Permission denied
Error: Permission denied when executing Claude CodeSolution: Make sure the Claude Code binary is executable:
chmod +x /path/to/claude
-
API Key issues
Error: Invalid API keySolution: Set the
ANTHROPIC_API_KEYenvironment variable or check your API key configuration. -
Port already in use
Error: [Errno 48] Address already in useSolution: Change the port in
claude_server.pyor kill the process using port 8000:lsof -ti:8000 | xargs kill -9
-
Module not found
ModuleNotFoundError: No module named 'fastapi'Solution: Install the required dependencies:
pip install fastapi uvicorn requests pydantic
For different environments:
Linux/Ubuntu:
claude_path: str = "/usr/local/bin/claude"macOS with Homebrew:
claude_path: str = "/opt/homebrew/bin/claude"GitHub Codespaces:
claude_path: str = "/home/codespace/nvm/current/bin/claude"WSL/Windows:
claude_path: str = "/mnt/c/Users/username/AppData/Roaming/npm/claude"Docker:
claude_path: str = "/usr/local/bin/claude"MIT License - see LICENSE file for details
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
For issues and questions:
- Check the troubleshooting section
- Review Claude Code CLI documentation
- Open an issue on GitHub