logo
0
0
WeChat Login

Claude2Api

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.

Go Report Card License |中文

NOTICE: ONLY PRO USER CAN USE ALL MODELS , FREE USER ONLY CAN USE claude-sonnet-4-20250514

✨ Features

  • 🖼️ 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

📋 Prerequisites

  • Go 1.23+ (for building from source)
  • Docker (for containerized deployment)

🚀 Deployment Options

Docker

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

Docker Compose

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

Hugging Face Spaces

You can deploy this project to Hugging Face Spaces with Docker:

  1. Fork the Hugging Face Space at https://huggingface.co/spaces/rclon/claude2api
  2. Configure your environment variables in the Settings tab
  3. The Space will automatically deploy the Docker image

notice: In Hugging Face, /v1 might be blocked, you can use /hf/v1 instead.

Direct Deployment

# 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

⚙️ Configuration

YAML Configuration

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.

Environment Variables

If config.yaml doesn't exist, the application will use environment variables for configuration:

Environment VariableDescriptionDefault
SESSIONSComma-separated list of Claude API session keysRequired
ADDRESSServer address and port0.0.0.0:8080
APIKEYAPI key for authenticationRequired
PROXYHTTP proxy URLOptional
CHAT_DELETEWhether to delete chat sessions after usetrue
MAX_CHAT_HISTORY_LENGTHExceeding will text to file10000
NO_ROLE_PREFIXDo not add role in every messagefalse
PROMPT_DISABLE_ARTIFACTSAdd Prompt try to disable Artifactsfalse
ENABLE_MIRROR_APIEnable direct use sk-ant-* as keyfalse
MIRROR_API_PREFIXAdd Prefix to protect Mirror,required when ENABLE_MIRROR_API is true``

📝 API Usage

Authentication

Include your API key in the request header:

Authorization: Bearer YOUR_API_KEY

Chat Completion

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 }'

Image Analysis

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,..." } } ] } ] }'

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

  • Anthropic for creating Claude
  • The Go community for the amazing ecosystem

🎁 Support

If you find this project helpful, consider supporting me on Afdian 😘

Star History

Star History Chart

Made with ❤️ by yushangxiao

About

将Claude 的网页服务转为 Api 服务,支持识图,文件上传,思考输出…… https://github.com/yushangxiao/claude2api.git

122.00 KiB
0 forks0 stars1 branches17 TagREADMEMIT license
Language
Go98.7%
Dockerfile1.3%