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.
|中文
NOTICE: ONLY PRO USER CAN USE ALL MODELS , FREE USER ONLY CAN USE claude-sonnet-4-20250514
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:latest
Create 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 -d
You can deploy this project to Hugging Face Spaces with Docker:
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 .
./claude2api
You 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.
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.
If you find this project helpful, consider supporting me on Afdian 😘
Made with ❤️ by yushangxiao