Transform Claude's web service into an API service, supporting image recognition, file upload, streaming transmission, thing output... The API supports access in the OpenAI format.
|中文
- 🖼️ Image Recognition - Send images to Claude for analysis
- 📝 Automatic Conversation Management - Conversation can be automatically deleted after use
- 🌊 Streaming Responses - Get real-time streaming outputs from Claude
- 📁 File Upload Support - Upload long context
- 🧠 Thinking Process - Access Claude's step-by-step reasoning, support
- 🔄 Chat History Management - Control the length of conversation context , exceeding will upload file
- 🌐 Proxy Support - Route requests through your preferred proxy
- 🔐 API Key Authentication - Secure your API endpoints
- 🔁 Automatic Retry - Feature to automatically retry requests when request fail
- 🌐 Direct Proxy -let sk-ant-sid01* as key to use
- Go 1.23+ (for building from source)
- Docker (for containerized deployment)
docker run -d \
-p 8080:8080 \
-e SESSIONS=sk-ant-sid01-xxxx,sk-ant-sid01-yyyy \
-e APIKEY=123 \
-e CHAT_DELETE=true \
-e MAX_CHAT_HISTORY_LENGTH=10000 \
-e NO_ROLE_PREFIX=false \
-e PROMPT_DISABLE_ARTIFACTS=false \
-e ENABLE_MIRROR_API=false \
-e MIRROR_API_PREFIX=/mirror \
--name claude2api \
ghcr.io/yushangxiao/claude2api:latestCreate a docker-compose.yml file:
version: '3'
services:
claude2api:
image: ghcr.io/yushangxiao/claude2api:latest
container_name: claude2api
ports:
- "8080:8080"
environment:
- SESSIONS=sk-ant-sid01-xxxx,sk-ant-sid01-yyyy
- ADDRESS=0.0.0.0:8080
- APIKEY=123
- PROXY=http://proxy:2080 # Optional
- CHAT_DELETE=true
- MAX_CHAT_HISTORY_LENGTH=10000
- NO_ROLE_PREFIX=false
- PROMPT_DISABLE_ARTIFACTS=true
- ENABLE_MIRROR_API=false
- MIRROR_API_PREFIX=/mirror
restart: unless-stopped
Then run:
docker-compose up -dYou can deploy this project to Hugging Face Spaces with Docker:
- Fork the Hugging Face Space at https://huggingface.co/spaces/rclon/claude2api
- Configure your environment variables in the Settings tab
- The Space will automatically deploy the Docker image
notice: In Hugging Face, /v1 might be blocked, you can use /hf/v1 instead.
# Clone the repository
git clone https://github.com/yushangxiao/claude2api.git
cd claude2api
cp .env.example .env
vim .env
# Build the binary
go build -o claude2api .
./claude2apiYou can configure Claude2API using a config.yaml file in the application's root directory. If this file exists, it will be used instead of environment variables.
Example config.yaml:
# Sessions configuration
sessions:
- sessionKey: "sk-ant-sid01-xxxx"
orgID: ""
- sessionKey: "sk-ant-sid01-yyyy"
orgID: ""
# Server address
address: "0.0.0.0:8080"
# API authentication key
apiKey: "123"
# Other configuration options...
chatDelete: true
maxChatHistoryLength: 10000
noRolePrefix: false
promptDisableArtifacts: false
enableMirrorApi: false
mirrorApiPrefix: ""A sample configuration file is provided as config.yaml.example in the repository.
If config.yaml doesn't exist, the application will use environment variables for configuration:
| Environment Variable | Description | Default |
|---|---|---|
SESSIONS |
Comma-separated list of Claude API session keys | Required |
ADDRESS |
Server address and port | 0.0.0.0:8080 |
APIKEY |
API key for authentication | Required |
PROXY |
HTTP proxy URL | Optional |
CHAT_DELETE |
Whether to delete chat sessions after use | true |
MAX_CHAT_HISTORY_LENGTH |
Exceeding will text to file | 10000 |
NO_ROLE_PREFIX |
Do not add role in every message | false |
PROMPT_DISABLE_ARTIFACTS |
Add Prompt try to disable Artifacts | false |
ENABLE_MIRROR_API |
Enable direct use sk-ant-* as key | false |
MIRROR_API_PREFIX |
Add Prefix to protect Mirror,required when ENABLE_MIRROR_API is true | `` |
Include your API key in the request header:
Authorization: Bearer YOUR_API_KEY
curl -X POST http://localhost:8080/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "claude-3-7-sonnet-20250219",
"messages": [
{
"role": "user",
"content": "Hello, Claude!"
}
],
"stream": true
}'curl -X POST http://localhost:8080/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "claude-3-7-sonnet-20250219",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What\'s in this image?"
},
{
"type": "image_url",
"image_url": {
"url": "data:image/jpeg;base64,..."
}
}
]
}
]
}'Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create a 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
This project is licensed under the MIT License - see the LICENSE file for details.
- Anthropic for creating Claude
- The Go community for the amazing ecosystem
Made with ❤️ by yushangxiao