claude2api

repository·main·Indexed 20 days ago

https://github.com/yushangxiao/claude2api

A Go-based service that transforms Claude's web service into an OpenAI-compatible API. It supports the /v1/chat/completions endpoint, image recognition via base64 encoded image_url, file uploads for long context, streaming responses, and automatic conversation management. Deployment options include Docker, Docker Compose, Hugging Face Spaces, or building from source with Go 1.23+.

Tokens
3K
Snippets
11
Records
15
Agent score
69%

What's inside claude2api

  1. Deploy Claude2Api via Docker Compose

    main

    Use a docker-compose.yml file to manage the deployment. This is useful for managing multiple environment variables and ensuring the service restarts automatically.

    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
  2. Build and run Claude2Api from source

    main

    To deploy directly on your host machine, you need Go 1.23+ installed.

    1. Clone the repository.
    2. Copy the .env.example to .env and configure it.
    3. Build the binary using go build.
    4. Execute the binary.
    # Clone the repository
    git clone https://github.com/yushangxiao/claude2api.git
    cd claude2api
    cp .env.example .env  
    # Edit .env with your configuration
    # Build the binary
    go build -o claude2api .
    
    ./claude2api
  3. Deploy Claude2Api via Docker

    main

    You can run Claude2Api as a containerized service using Docker. Ensure you provide the required SESSIONS (Claude session keys) and APIKEY (your custom authentication key) environment variables.

    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
  4. Configure Claude2Api using YAML

    main

    If a config.yaml file exists in the application's root directory, it will be used instead of environment variables. This file allows for structured configuration of sessions, server settings, and API keys.

    # 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: ""
  5. Configure Claude2Api via config.yaml

    main

    If a config.yaml file exists in the application root, it will be used instead of environment variables. This file allows you to define sessions, server addresses, and other operational settings.

    # 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: ""
  6. Run the Claude2Api server

    main

    The application is a Go-based service that initializes a Gin web server, loads configuration, and sets up API routes. By default, the server runs on the address specified in the configuration (typically 0.0.0.0:8080).

    go run main.go
  7. Perform Image Analysis

    main

    Claude2Api supports image recognition. Send an array of content objects to the /v1/chat/completions endpoint, including a type: "image_url" object with a base64 encoded image.

    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": "这张图片里有什么?"
              },
              {
                "type": "image_url",
                "image_url": {
                  "url": "data:image/jpeg;base64,..."
                }
              }
            ]
          }
        ]
      }'
  8. Use the Chat Completions API

    main

    Claude2Api supports the OpenAI-compatible /v1/chat/completions endpoint. You must include your configured APIKEY in the Authorization header as a Bearer token.

    # Standard 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
      }'
  9. Create a Chat Completion

    main

    Send a POST request to /v1/chat/completions following the OpenAI API format to interact with Claude.

    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": "你好,Claude!"
          }
        ],
        "stream": true
      }'