This guide will walk you through setting up and running the unofficial Claude API client. This client allows you to interact with Claude AI programmatically, similar to how you might use OpenAI's API.
This script provides a way to:
- Interact with Claude AI through a Python API
- Run a local server that mimics OpenAI's API structure but connects to Claude
- Perform various tasks like chatting, generating embeddings, and managing conversations
- Python 3.7 or higher installed
- A Claude AI account
- Basic familiarity with command line operations
First, clone the repository containing the Claude API scripts to your local machine.
git clone [repository_url]
cd [repository_name]
Create a virtual environment and activate it:
python -m venv venv
source venv/bin/activate # On Windows, use: venv\Scripts\activate
Install the required packages:
pip install -r requirements.txt
Create a file named .env
in the root directory of the project. You'll need to fill this with several important values:
- Log in to Claude AI in your web browser
- In the same browser session, open a new tab and go to: https://api.claude.ai/api/organizations
- You'll see a JSON response. Look for the
uuid
field of your desired organization - Copy this UUID
- While still on the Claude AI website, open your browser's developer tools (F12 or right-click and select "Inspect")
- Go to the "Application" or "Storage" tab
- Find the Cookies section and look for the
claude.ai
cookie - Copy the entire cookie string
Open the .env
file and add the following lines, replacing the placeholders with your actual values:
ORGANIZATION_ID=your_organization_uuid_here
COOKIE=your_full_cookie_string_here
API_KEY=your_chosen_api_key_here # You can make this up; it's for your local API
To start a console-based chat with Claude:
python console_chat.py
This will allow you to interact with Claude directly in your terminal.
design-sans-titre_D7P4lBhK.mp4
To run the local API server that mimics the OpenAI API structure:
python server.py
This will start a server on http://localhost:8008
. You can now make API calls to this address as if it were the OpenAI API, but it will use Claude instead.
With the server running, you can make requests to it using tools like curl
or any programming language. Here's an example using Python's requests
library:
import requests
api_url = "http://localhost:8008/v1/chat/completions"
headers = {
"Authorization": f"Bearer {your_api_key_here}",
"Content-Type": "application/json"
}
data = {
"model": "claude-3-5-sonnet-20240620",
"messages": [{"role": "user", "content": "Hello, Claude!"}]
}
response = requests.post(api_url, json=data, headers=headers)
print(response.json())
- Keep your
.env
file secure and never share it publicly. - The cookie and organization ID are sensitive. Only use them on trusted devices.
- This is an unofficial client. Be aware of Claude AI's terms of service when using it.
- The API mimics OpenAI's structure but may not be 100% compatible with all OpenAI API features.
- If you encounter errors, double-check your cookie and organization ID.
- Ensure you're using the correct API key in your requests to the local server.
- Check the console output for any error messages when running the scripts.
This project is not affiliated with or endorsed by Claude AI. Use this client at your own risk, and make sure to comply with Claude AI's terms of service.
If you encounter issues or have suggestions for improvements, please open an issue on the project's GitHub repository.