claude-code-api-0.1.3 is not a library.
Claude Code API
中文文档 | English
A high-performance Rust implementation of an OpenAI-compatible API gateway for Claude Code CLI. This project provides a RESTful API interface that allows you to interact with Claude Code using the familiar OpenAI API format.
✨ Features
- 🔌 OpenAI API Compatibility - Drop-in replacement for OpenAI API, works with existing OpenAI client libraries
- 🚀 High Performance - Built with Rust, Axum, and Tokio for exceptional performance
- 💬 Conversation Management - Built-in session support for multi-turn conversations
- 🖼️ Multimodal Support - Process images alongside text in your requests
- ⚡ Response Caching - Intelligent caching system to reduce latency and costs
- 🔧 MCP Support - Model Context Protocol integration for accessing external tools and services
- 📁 File Access Control - Configurable file system permissions for secure operations
- 🌊 Streaming Responses - Real-time streaming support for long-form content
- 🛡️ Robust Error Handling - Comprehensive error handling with automatic retries
- 📊 Statistics API - Monitor usage and performance metrics
🚀 Quick Start
Prerequisites
- Rust 1.75 or higher
- Claude CLI installed and configured
- (Optional) MCP servers for extended functionality
Installation
- Clone the repository:
- Build the project:
- Start the server:
The API server will start on http://localhost:8080
by default.
Quick Test
📖 Core Features
1. OpenAI-Compatible Chat API
# Configure the client to use Claude Code API
=
=
2. Conversation Management
Maintain context across multiple requests:
# First request - creates a new conversation
=
=
# Subsequent request - continues the conversation
=
# Claude will remember: "Your name is Alice"
3. Multimodal Support
Process images with text:
=
Supported image formats:
- Local file paths
- HTTP/HTTPS URLs
- Base64 encoded data URLs
4. Streaming Responses
=
5. MCP (Model Context Protocol)
Enable Claude to access external tools and services:
# Create MCP configuration
# Start with MCP support
🔧 Configuration
Environment Variables
# Server configuration
CLAUDE_CODE__SERVER__HOST=0.0.0.0
CLAUDE_CODE__SERVER__PORT=8080
# Claude CLI configuration
CLAUDE_CODE__CLAUDE__COMMAND=claude
CLAUDE_CODE__CLAUDE__TIMEOUT_SECONDS=300
CLAUDE_CODE__CLAUDE__MAX_CONCURRENT_SESSIONS=10
# File access permissions
CLAUDE_CODE__FILE_ACCESS__SKIP_PERMISSIONS=false
CLAUDE_CODE__FILE_ACCESS__ADDITIONAL_DIRS='["/path1", "/path2"]'
# MCP configuration
CLAUDE_CODE__MCP__ENABLED=true
CLAUDE_CODE__MCP__CONFIG_FILE="./mcp_config.json"
CLAUDE_CODE__MCP__STRICT=false
CLAUDE_CODE__MCP__DEBUG=false
# Cache configuration
CLAUDE_CODE__CACHE__ENABLED=true
CLAUDE_CODE__CACHE__MAX_ENTRIES=1000
CLAUDE_CODE__CACHE__TTL_SECONDS=3600
# Conversation management
CLAUDE_CODE__CONVERSATION__MAX_HISTORY_MESSAGES=20
CLAUDE_CODE__CONVERSATION__SESSION_TIMEOUT_MINUTES=30
Configuration File
Create config/local.toml
:
[]
= "0.0.0.0"
= 8080
[]
= "claude"
= 300
= 10
[]
= false
= ["/Users/me/projects", "/tmp"]
[]
= true
= "./mcp_config.json"
= false
= false
📚 API Endpoints
Chat Completions
POST /v1/chat/completions
- Create a chat completion
Models
GET /v1/models
- List available models
Conversations
POST /v1/conversations
- Create a new conversationGET /v1/conversations
- List active conversationsGET /v1/conversations/:id
- Get conversation details
Statistics
GET /stats
- Get API usage statistics
Health Check
GET /health
- Check service health
🛠️ Advanced Usage
Using with LangChain
=
=
Using with Node.js
const OpenAI = require;
const client = ;
Using with curl
# Basic request
# With conversation ID
# With image
🔒 Security
- File access is controlled through configurable permissions
- MCP servers run in isolated processes
- No API key required (relies on Claude CLI authentication)
- Supports CORS for web applications
- Request ID tracking for audit trails
🐛 Troubleshooting
Common Issues
-
"Permission denied" errors
# Enable file permissions # Or use the startup script
-
MCP servers not working
# Enable debug mode # Check MCP server installation
-
High latency on first request
- This is normal as Claude CLI needs to start up
- Subsequent requests will be faster due to process reuse
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
- Built on top of Claude Code CLI
- Inspired by OpenAI's API design
- Powered by Axum web framework
📞 Support
Made with ❤️ by the Claude Code API team