| Crates.io | claude-code-api |
| lib.rs | claude-code-api |
| version | 0.1.3 |
| created_at | 2025-07-21 06:04:32.638977+00 |
| updated_at | 2025-07-21 07:11:42.773781+00 |
| description | OpenAI-compatible API gateway for Claude Code CLI |
| homepage | |
| repository | https://github.com/ZhangHanDong/claude-code-api-rs |
| max_upload_size | |
| id | 1761773 |
| size | 231,950 |
中文文档 | 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.
git clone https://github.com/yourusername/claude-code-api.git
cd claude-code-api/rust-claude-code-api
cargo build --release
./target/release/claude-code-api
The API server will start on http://localhost:8080 by default.
curl -X POST http://localhost:8080/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "claude-opus-4-20250514",
"messages": [
{"role": "user", "content": "Hello, Claude!"}
]
}'
import openai
# Configure the client to use Claude Code API
client = openai.OpenAI(
base_url="http://localhost:8080/v1",
api_key="not-needed" # API key is not required
)
response = client.chat.completions.create(
model="claude-opus-4-20250514",
messages=[
{"role": "user", "content": "Write a hello world in Python"}
]
)
print(response.choices[0].message.content)
Maintain context across multiple requests:
# First request - creates a new conversation
response = client.chat.completions.create(
model="claude-opus-4-20250514",
messages=[
{"role": "user", "content": "My name is Alice"}
]
)
conversation_id = response.conversation_id
# Subsequent request - continues the conversation
response = client.chat.completions.create(
model="claude-opus-4-20250514",
conversation_id=conversation_id,
messages=[
{"role": "user", "content": "What's my name?"}
]
)
# Claude will remember: "Your name is Alice"
Process images with text:
response = client.chat.completions.create(
model="claude-opus-4-20250514",
messages=[{
"role": "user",
"content": [
{"type": "text", "text": "What's in this image?"},
{"type": "image_url", "image_url": {"url": "/path/to/image.png"}}
]
}]
)
Supported image formats:
stream = client.chat.completions.create(
model="claude-opus-4-20250514",
messages=[{"role": "user", "content": "Write a long story"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
Enable Claude to access external tools and services:
# Create MCP configuration
cat > mcp_config.json << EOF
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/allowed/path"]
},
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {
"GITHUB_PERSONAL_ACCESS_TOKEN": "your-token"
}
}
}
}
EOF
# Start with MCP support
export CLAUDE_CODE__MCP__ENABLED=true
export CLAUDE_CODE__MCP__CONFIG_FILE="./mcp_config.json"
./target/release/claude-code-api
# 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
Create config/local.toml:
[server]
host = "0.0.0.0"
port = 8080
[claude]
command = "claude"
timeout_seconds = 300
max_concurrent_sessions = 10
[file_access]
skip_permissions = false
additional_dirs = ["/Users/me/projects", "/tmp"]
[mcp]
enabled = true
config_file = "./mcp_config.json"
strict = false
debug = false
POST /v1/chat/completions - Create a chat completionGET /v1/models - List available modelsPOST /v1/conversations - Create a new conversationGET /v1/conversations - List active conversationsGET /v1/conversations/:id - Get conversation detailsGET /stats - Get API usage statisticsGET /health - Check service healthfrom langchain.chat_models import ChatOpenAI
llm = ChatOpenAI(
base_url="http://localhost:8080/v1",
api_key="not-needed",
model="claude-opus-4-20250514"
)
response = llm.invoke("Explain quantum computing")
print(response.content)
const OpenAI = require('openai');
const client = new OpenAI({
baseURL: 'http://localhost:8080/v1',
apiKey: 'not-needed'
});
async function chat() {
const response = await client.chat.completions.create({
model: 'claude-opus-4-20250514',
messages: [{ role: 'user', content: 'Hello!' }]
});
console.log(response.choices[0].message.content);
}
# Basic request
curl -X POST http://localhost:8080/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "claude-opus-4-20250514",
"messages": [{"role": "user", "content": "Hello"}]
}'
# With conversation ID
curl -X POST http://localhost:8080/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "claude-opus-4-20250514",
"conversation_id": "uuid-here",
"messages": [{"role": "user", "content": "Continue our chat"}]
}'
# With image
curl -X POST http://localhost:8080/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "claude-opus-4-20250514",
"messages": [{
"role": "user",
"content": [
{"type": "text", "text": "What is this?"},
{"type": "image_url", "image_url": {"url": "/path/to/image.png"}}
]
}]
}'
"Permission denied" errors
# Enable file permissions
export CLAUDE_CODE__FILE_ACCESS__SKIP_PERMISSIONS=true
# Or use the startup script
./start_with_permissions.sh
MCP servers not working
# Enable debug mode
export CLAUDE_CODE__MCP__DEBUG=true
# Check MCP server installation
npx -y @modelcontextprotocol/server-filesystem --version
High latency on first request
Contributions are welcome! Please feel free to submit a Pull Request.
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)This project is licensed under the MIT License - see the LICENSE file for details.
Made with ❤️ by the Claude Code API team