### YAML Configuration Example Source: https://github.com/yushangxiao/claude2api/blob/main/README.md Example configuration file (config.yaml) for Claude2API. This file can be used instead of environment variables for configuration. ```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: "" ``` -------------------------------- ### Docker Compose Up Command Source: https://github.com/yushangxiao/claude2api/blob/main/README.md Command to start the services defined in your docker-compose.yml file. ```bash docker-compose up -d ``` -------------------------------- ### Environment Variable Configuration Source: https://context7.com/yushangxiao/claude2api/llms.txt Configure the service runtime parameters using environment variables before starting the binary. ```bash export SESSIONS=sk-ant-sid01-your-first-key,sk-ant-sid01-your-second-key export APIKEY=your-api-authentication-key export ADDRESS=0.0.0.0:8080 # Server bind address export PROXY=http://127.0.0.1:2080 # HTTP proxy for Claude requests export CHAT_DELETE=true # Auto-delete conversations after use export MAX_CHAT_HISTORY_LENGTH=10000 # Max chars before using file attachment export NO_ROLE_PREFIX=false # Disable role prefixes in prompts export PROMPT_DISABLE_ARTIFACTS=false # Disable Claude's artifact wrapping export ENABLE_MIRROR_API=false # Enable direct session key usage export MIRROR_API_PREFIX=/mirror # Prefix for mirror API routes ./claude2api ``` -------------------------------- ### Manage Docker Compose Services Source: https://context7.com/yushangxiao/claude2api/llms.txt Commands to start, monitor, and stop the service using Docker Compose. ```bash # Start with Docker Compose docker-compose up -d # View logs docker-compose logs -f # Stop service docker-compose down ``` -------------------------------- ### GET /v1/models Source: https://context7.com/yushangxiao/claude2api/llms.txt Retrieves a list of all available Claude models. ```APIDOC ## GET /v1/models ### Description Retrieve the list of available Claude models supported by the API, including both standard and thinking-enabled variants. ### Method GET ### Endpoint http://localhost:8080/v1/models ### Response #### Success Response (200) - **data** (array) - List of model objects containing model IDs ``` -------------------------------- ### GET /health Source: https://context7.com/yushangxiao/claude2api/llms.txt Verifies the API service is running. ```APIDOC ## GET /health ### Description Simple health check endpoint to verify the API service is running and responsive. ### Method GET ### Endpoint http://localhost:8080/health ### Response #### Success Response (200) - **status** (string) - Should return 'ok' ``` -------------------------------- ### Direct Deployment (Build and Run) Source: https://github.com/yushangxiao/claude2api/blob/main/README.md Deploy Claude2Api by building from source. This involves cloning the repository, setting up environment variables, building the binary, and running the executable. ```bash # 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 ``` -------------------------------- ### HuggingFace-Compatible Routes Source: https://context7.com/yushangxiao/claude2api/llms.txt Use these routes when deploying on platforms like HuggingFace Spaces where standard /v1 paths might be restricted. ```bash curl -X POST http://localhost:8080/hf/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_API_KEY" \ -d '{ "model": "claude-sonnet-4-20250514", "messages": [ { "role": "user", "content": "Hello, Claude!" } ], "stream": false }' curl -X GET http://localhost:8080/hf/v1/models \ -H "Authorization: Bearer YOUR_API_KEY" ``` -------------------------------- ### Deploy with Docker Compose Source: https://context7.com/yushangxiao/claude2api/llms.txt Use a YAML configuration file to define the service, environment variables, and health checks. ```yaml # docker-compose.yml version: '3' services: claude2api: image: ghcr.io/yushangxiao/claude2api:latest container_name: claude2api ports: - "8080:8080" environment: - SESSIONS=sk-ant-sid01-your-key-1,sk-ant-sid01-your-key-2 - ADDRESS=0.0.0.0:8080 - APIKEY=your-secure-api-key - PROXY=http://proxy:2080 - 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 healthcheck: test: ["CMD", "curl", "-f", "http://localhost:8080/health"] interval: 30s timeout: 10s retries: 3 ``` -------------------------------- ### YAML Configuration File Source: https://context7.com/yushangxiao/claude2api/llms.txt Use a config.yaml file for structured configuration, particularly useful when managing multiple session keys. ```yaml sessions: - sessionKey: "sk-ant-sid01-your-first-session-key" orgID: "" - sessionKey: "sk-ant-sid01-your-second-session-key" orgID: "org-uuid-if-known" address: "0.0.0.0:8080" apiKey: "your-secure-api-key" proxy: "http://127.0.0.1:2080" ``` -------------------------------- ### List Available Models Source: https://context7.com/yushangxiao/claude2api/llms.txt Retrieve a list of all supported Claude models, including standard and thinking-enabled versions. ```bash curl -X GET http://localhost:8080/v1/models \ -H "Authorization: Bearer YOUR_API_KEY" ``` -------------------------------- ### Mirror API (Direct Session Key) Source: https://context7.com/yushangxiao/claude2api/llms.txt Authenticate directly using Claude session keys. Requires ENABLE_MIRROR_API to be set to true. ```bash curl -X POST http://localhost:8080/mirror/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer sk-ant-sid01-your-session-key" \ -d '{ "model": "claude-3-7-sonnet-20250219", "messages": [ { "role": "user", "content": "Hello from mirror API!" } ], "stream": false }' curl -X POST http://localhost:8080/mirror/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer sk-ant-sid01-your-session-key:org-uuid-here" \ -d '{ "model": "claude-3-7-sonnet-20250219", "messages": [ { "role": "user", "content": "Hello!" } ] }' ``` -------------------------------- ### Deploy Claude2Api with Docker Source: https://context7.com/yushangxiao/claude2api/llms.txt Run the service using Docker with environment variables for session keys and API configuration. ```bash # Basic Docker deployment docker run -d \ --name claude2api \ -p 8080:8080 \ -e SESSIONS=sk-ant-sid01-your-session-key \ -e APIKEY=your-secure-api-key \ -e CHAT_DELETE=true \ -e MAX_CHAT_HISTORY_LENGTH=10000 \ ghcr.io/yushangxiao/claude2api:latest ``` ```bash # Full configuration Docker deployment docker run -d \ --name claude2api \ -p 8080:8080 \ -e SESSIONS=sk-ant-sid01-key1,sk-ant-sid01-key2 \ -e APIKEY=your-secure-api-key \ -e PROXY=http://host.docker.internal:2080 \ -e CHAT_DELETE=true \ -e MAX_CHAT_HISTORY_LENGTH=10000 \ -e NO_ROLE_PREFIX=false \ -e PROMPT_DISABLE_ARTIFACTS=true \ -e ENABLE_MIRROR_API=true \ -e MIRROR_API_PREFIX=/mirror \ --restart unless-stopped \ ghcr.io/yushangxiao/claude2api:latest ``` -------------------------------- ### Docker Compose Deployment Source: https://github.com/yushangxiao/claude2api/blob/main/README.md Set up Claude2Api with Docker Compose. This method allows for a more structured configuration of services and environment variables. ```yaml 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 ``` -------------------------------- ### Docker Deployment Source: https://github.com/yushangxiao/claude2api/blob/main/README.md Deploy Claude2Api using Docker. Ensure to set necessary environment variables like SESSIONS and APIKEY. ```bash 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 ``` -------------------------------- ### POST /mirror/v1/chat/completions Source: https://context7.com/yushangxiao/claude2api/llms.txt Mirror API endpoint for using direct Claude session keys. ```APIDOC ## POST /mirror/v1/chat/completions ### Description Allows using Claude session keys directly as the API key for authentication. Supports optional organization ID in the Authorization header. ### Method POST ### Endpoint http://localhost:8080/mirror/v1/chat/completions ### Request Body - **model** (string) - Required - The model ID - **messages** (array) - Required - List of message objects - **stream** (boolean) - Optional - Whether to stream the response ``` -------------------------------- ### Execute Multi-turn Conversation Request Source: https://context7.com/yushangxiao/claude2api/llms.txt Send a POST request to the chat completions endpoint including a message history array. ```bash # Multi-turn conversation example 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": "system", "content": "You are a helpful coding assistant specialized in Python." }, { "role": "user", "content": "How do I read a JSON file in Python?" }, { "role": "assistant", "content": "You can use the json module:\n\nimport json\n\nwith open(\"data.json\", \"r\") as f:\n data = json.load(f)" }, { "role": "user", "content": "How do I handle the case where the file might not exist?" } ], "stream": false }' ``` -------------------------------- ### Manage Docker Containers Source: https://context7.com/yushangxiao/claude2api/llms.txt Commands for checking logs of a running Claude2Api container. ```bash # Check container logs docker logs -f claude2api ``` -------------------------------- ### Health Check Endpoint Source: https://context7.com/yushangxiao/claude2api/llms.txt Verify that the API service is running and responsive. ```bash curl -X GET http://localhost:8080/health ``` -------------------------------- ### Image Analysis with Base64 Encoding Source: https://context7.com/yushangxiao/claude2api/llms.txt Perform image analysis by sending a base64-encoded image along with a text prompt. The 'content' field in the messages array should include both text and the image_url object. ```bash 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 objects do you see in this image? Describe them in detail." }, { "type": "image_url", "image_url": { "url": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD..." } } ] } ], "stream": false }' ``` -------------------------------- ### Image Analysis with Multiple Images Source: https://context7.com/yushangxiao/claude2api/llms.txt Analyze multiple images in a single request by including multiple 'image_url' objects within the 'content' array. This is useful for comparing images or providing more context. ```bash 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": "Compare these two images and describe their differences." }, { "type": "image_url", "image_url": { "url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUg..." } }, { "type": "image_url", "image_url": { "url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUg..." } } ] } ] }' ``` -------------------------------- ### Enable thinking mode for complex problem solving Source: https://context7.com/yushangxiao/claude2api/llms.txt Use the thinking-enabled model variant to receive a step-by-step reasoning process in the response. ```bash 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-think", "messages": [ { "role": "user", "content": "Solve this step by step: If a train travels 120 km in 2 hours, and then 180 km in 3 hours, what is the average speed for the entire journey?" } ], "stream": true }' ``` -------------------------------- ### POST /v1/chat/completions Source: https://context7.com/yushangxiao/claude2api/llms.txt Sends a chat message to the Claude model, supporting thinking mode and streaming. ```APIDOC ## POST /v1/chat/completions ### Description Sends a chat message to the Claude model. Supports thinking mode by selecting specific models and streaming responses. ### Method POST ### Endpoint http://localhost:8080/v1/chat/completions ### Request Body - **model** (string) - Required - The model ID (e.g., claude-3-7-sonnet-20250219-think) - **messages** (array) - Required - List of message objects with role and content - **stream** (boolean) - Optional - Whether to stream the response ### Request Example { "model": "claude-3-7-sonnet-20250219-think", "messages": [ {"role": "user", "content": "Solve this step by step..."} ], "stream": true } ``` -------------------------------- ### Streaming Chat Completion Request Source: https://context7.com/yushangxiao/claude2api/llms.txt Initiate a streaming chat completion request by setting the 'stream' parameter to true. Responses will be received as Server-Sent Events (SSE). ```bash curl -X POST http://localhost:8080/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_API_KEY" \ -d '{ "model": "claude-sonnet-4-20250514", "messages": [ { "role": "user", "content": "Write a haiku about programming" } ], "stream": true }' ``` -------------------------------- ### Basic Chat Completion Request Source: https://context7.com/yushangxiao/claude2api/llms.txt Use this command to send a basic chat completion request to the Claude2Api. Ensure your API key and model name are correctly specified. Set 'stream' to false for a non-streaming response. ```bash 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": "system", "content": "You are a helpful assistant." }, { "role": "user", "content": "What is the capital of France?" } ], "stream": false }' ``` -------------------------------- ### Thinking Mode Source: https://context7.com/yushangxiao/claude2api/llms.txt Enable Claude's extended thinking process for complex reasoning tasks by appending '-think' to the model name. Responses include `` tags containing Claude's step-by-step reasoning before the final answer. ```APIDOC ## Using Thinking Mode ### Description Activates an extended reasoning process for Claude models, providing step-by-step thought processes within the response. This is useful for complex problem-solving and debugging. ### How to Enable Append `-think` to the model name when making a request to the Chat Completions endpoint. ### Example Model Name `claude-3-opus-20240229-think` ### Response Format Responses will include `` tags containing the model's reasoning process before the final answer. ### Example Response Snippet ```xml The user is asking for the capital of France. I know that Paris is the capital of France. Therefore, the answer is Paris. The capital of France is Paris. ``` ``` -------------------------------- ### Send Image Analysis Request Source: https://github.com/yushangxiao/claude2api/blob/main/README.md This cURL command demonstrates how to perform image analysis using the Claude API. It includes a text prompt and an image URL within the messages payload. Replace YOUR_API_KEY and the base64 encoded image data as needed. ```bash 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,மையில்" } } ] } ] }' ``` -------------------------------- ### Send Chat Completion Request Source: https://github.com/yushangxiao/claude2api/blob/main/README.md Use this cURL command to send a text-based chat completion request to the Claude API. Ensure you replace YOUR_API_KEY with your actual API key and specify the desired model. ```bash 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 Source: https://context7.com/yushangxiao/claude2api/llms.txt Send images to Claude for analysis using base64-encoded data or URLs. The API processes images through Claude's vision capabilities and returns detailed descriptions or answers to questions about the image content. ```APIDOC ## POST /v1/chat/completions (with image analysis) ### Description Allows sending images along with text prompts to Claude for analysis. Supports base64-encoded images and image URLs. ### Method POST ### Endpoint /v1/chat/completions ### Parameters #### Request Body - **model** (string) - Required - The Claude model to use (e.g., "claude-3-7-sonnet-20250219"). - **messages** (array) - Required - An array of message objects. For image analysis, the content can be a mix of text and image objects. - **role** (string) - Required - Role of the message sender (e.g., "user"). - **content** (array) - Required - An array containing message parts. - **type** (string) - Required - Type of content ("text" or "image_url"). - **text** (string) - Required if type is "text" - The text prompt. - **image_url** (object) - Required if type is "image_url" - Object containing the image URL. - **url** (string) - Required - The URL of the image, can be a data URI (e.g., "data:image/jpeg;base64,..."). - **stream** (boolean) - Optional - Whether to stream the response. Defaults to false. ### Request Example (Base64 Image) ```json { "model": "claude-3-7-sonnet-20250219", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "What objects do you see in this image? Describe them in detail." }, { "type": "image_url", "image_url": { "url": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD..." } } ] } ], "stream": false } ``` ### Request Example (Multiple Images) ```json { "model": "claude-3-7-sonnet-20250219", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Compare these two images and describe their differences." }, { "type": "image_url", "image_url": { "url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUg..." } }, { "type": "image_url", "image_url": { "url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUg..." } } ] } ] } ``` ### Response #### Success Response (200) Similar to the Chat Completions response, with the assistant's response detailing the image analysis. - **id** (string) - Unique identifier for the completion. - **object** (string) - Type of object returned (e.g., "chat.completion"). - **created** (integer) - Timestamp of creation. - **model** (string) - The model used for the completion. - **choices** (array) - An array of completion choices. - **index** (integer) - Index of the choice. - **message** (object) - The message content. - **role** (string) - Role of the message sender (e.g., "assistant"). - **content** (string) - The response text describing the image analysis. - **finish_reason** (string) - The reason the model stopped generating tokens. ### Response Example (Non-streaming) ```json { "id": "uuid-here", "object": "chat.completion", "created": 1234567890, "model": "claude-3-7-sonnet-20250219", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "The image contains a red car parked on a street. There are trees in the background." }, "finish_reason": "stop" } ] } ``` ``` -------------------------------- ### Chat Completions Endpoint Source: https://context7.com/yushangxiao/claude2api/llms.txt The primary endpoint for sending messages to Claude models using OpenAI-compatible format. Supports both streaming and non-streaming responses, multiple message types, and various Claude models including thinking-enabled variants. ```APIDOC ## POST /v1/chat/completions ### Description Sends messages to Claude models using an OpenAI-compatible format. Supports streaming and non-streaming responses. ### Method POST ### Endpoint /v1/chat/completions ### Parameters #### Request Body - **model** (string) - Required - The Claude model to use (e.g., "claude-3-7-sonnet-20250219"). - **messages** (array) - Required - An array of message objects, each with a "role" (system, user, assistant) and "content". - **stream** (boolean) - Optional - Whether to stream the response. Defaults to false. ### Request Example (Non-streaming) ```json { "model": "claude-3-7-sonnet-20250219", "messages": [ { "role": "system", "content": "You are a helpful assistant." }, { "role": "user", "content": "What is the capital of France?" } ], "stream": false } ``` ### Response #### Success Response (200) - **id** (string) - Unique identifier for the completion. - **object** (string) - Type of object returned (e.g., "chat.completion"). - **created** (integer) - Timestamp of creation. - **model** (string) - The model used for the completion. - **choices** (array) - An array of completion choices. - **index** (integer) - Index of the choice. - **message** (object) - The message content. - **role** (string) - Role of the message sender (e.g., "assistant"). - **content** (string) - The response text. - **finish_reason** (string) - The reason the model stopped generating tokens. ### Response Example (Non-streaming) ```json { "id": "uuid-here", "object": "chat.completion", "created": 1234567890, "model": "claude-3-7-sonnet-20250219", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "The capital of France is Paris." }, "finish_reason": "stop" } ] } ``` ### Request Example (Streaming) ```json { "model": "claude-sonnet-4-20250514", "messages": [ { "role": "user", "content": "Write a haiku about programming" } ], "stream": true } ``` ### Response Example (Streaming SSE) ``` data: {"id":"uuid","object":"chat.completion.chunk","created":1234567890,"model":"claude-3-7-sonnet-20250219","choices":[{"index":0,"delta":{"content":"Code"},"logprobs":null,"finish_reason":null}]} data: {"id":"uuid","object":"chat.completion.chunk","created":1234567890,"model":"claude-3-7-sonnet-20250219","choices":[{"index":0,"delta":{"content":" flows"},"logprobs":null,"finish_reason":null}]} ... data: [DONE] ``` ``` -------------------------------- ### API Authentication Header Source: https://github.com/yushangxiao/claude2api/blob/main/README.md Include your API key in the request header for authentication when interacting with the Claude2Api. ```http Authorization: Bearer YOUR_API_KEY ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.