This unofficial Python API provides access to the conversational capabilities of Anthropic's Claude AI through a simple chat messaging interface.
Project description
unofficial-claude-api
Table of Contents
What is this?
This unofficial Python API provides access to the conversational capabilities of Anthropic's Claude AI through a simple chat messaging interface.
While not officially supported by Anthropic, this library can enable interesting conversational applications.
It allows for:
- Creating chat sessions with Claude and getting chat IDs.
- Sending messages to Claude containing up to 5 attachment files (txt, pdf, csv, png, jpeg, etc...) 10 MB each, images are also supported!
- Retrieving chat message history, accessing specific chat conversations.
- Deleting old chats when they are no longer needed.
- Sending requests through proxies.
Some of the key things you can do with Claude through this API
-
Ask questions about a wide variety of topics. Claude can chat about current events, pop culture, sports, and more.
-
Get helpful explanations on complex topics. Ask Claude to explain concepts and ideas in simple terms.
-
Generate summaries from long text or documents. Just give the filepath as an attachment to Claude and get back a concise summary.
-
Receive thoughtful responses to open-ended prompts and ideas. Claude can brainstorm ideas, expand on concepts, and have philosophical discussions.
-
Send images and let Claude analyze them for you.
How to install
pip install unofficial-claude-api
Uninstallation
pip uninstall unofficial-claude-api
Requirements
These requirements are needed to auto retrieve a SessionData object using selenium
- Python >= 3.10
- Firefox installed, and with at least one profile logged into Claude.
- geckodriver installed inside a folder registered in PATH environment variable.
( scrolling through this README you'll find also a manual alternative )
Example Usage
from sys import exit as sys_exit
from claude_api.client import (
ClaudeAPIClient,
SendMessageResponse,
)
from claude_api.session import SessionData, get_session_data
from claude_api.errors import ClaudeAPIError, MessageRateLimitError, OverloadError
# Wildcard import will also work the same as above
# from claude_api import *
# List of attachments filepaths, up to 5, max 10 MB each
FILEPATH_LIST = [
"test1.txt",
"test2.txt",
]
# This function will automatically retrieve a SessionData instance using selenium
# It will auto gather cookie session, user agent and organization ID.
# Omitting profile argument will use default Firefox profile
session: SessionData = get_session_data()
# Initialize a client instance using a session
# Optionally change the requests timeout parameter to best fit your needs...default to 240 seconds.
client = ClaudeAPIClient(session, timeout=240)
# Create a new chat and cache the chat_id
chat_id = client.create_chat()
if not chat_id:
# This will not throw MessageRateLimitError
# But it still means that account has no more messages left.
print("\nMessage limit hit, cannot create chat...")
sys_exit(1)
try:
# Used for sending message with or without attachments
# Returns a SendMessageResponse instance
res: SendMessageResponse = client.send_message(
chat_id, "Hello!", attachment_paths=FILEPATH_LIST
)
# Inspect answer
if res.answer:
print(res.answer)
else:
# Inspect response status code and raw answer bytes
print(f"\nError code {res.status_code}, raw_answer: {res.raw_answer}")
except ClaudeAPIError as e:
# Identify the error
if isinstance(e, MessageRateLimitError):
# The exception will hold these informations about the rate limit:
print(f"\nMessage limit hit, resets at {e.reset_date}")
print(f"\n{e.sleep_sec} seconds left until -> {e.reset_timestamp}")
elif isinstance(e, OverloadError):
print(f"\nOverloaded error: {e}")
else:
print(f"\nGot unknown Claude error: {e}")
finally:
# Perform chat deletion for cleanup
client.delete_chat(chat_id)
# Get a list of all chats ids
all_chat_ids = client.get_all_chat_ids()
# Delete all chats
for chat in all_chat_ids:
client.delete_chat(chat)
# Or by using a shortcut utility
client.delete_all_chats()
sys_exit(0)
Tips
Retrieving chat history
# A convenience method to access a specific chat conversation is
chat_data = client.get_chat_data(chat_id)
chat_data
will be the same json dictionary returned by calling
/api/organizations/{organization_id}/chat_conversations/{chat_id}
Here's an example of this json:
{
"uuid": "<ConversationUUID>",
"name": "",
"summary": "",
"model": null,
"created_at": "1997-12-25T13:33:33.959409+00:00",
"updated_at": "1997-12-25T13:33:39.487561+00:00",
"chat_messages": [
{
"uuid": "<MessageUUID>",
"text": "Who is Bugs Bunny?",
"sender": "human",
"index": 0,
"created_at": "1997-12-25T13:33:39.487561+00:00",
"updated_at": "1997-12-25T13:33:40.959409+00:00",
"edited_at": null,
"chat_feedback": null,
"attachments": []
},
{
"uuid": "<MessageUUID>",
"text": "<Claude response's text>",
"sender": "assistant",
"index": 1,
"created_at": "1997-12-25T13:33:40.959409+00:00",
"updated_at": "1997-12-25T13:33:42.487561+00:00",
"edited_at": null,
"chat_feedback": null,
"attachments": []
}
]
}
Faster loading, avoiding selenium
If for whatever reason you'd like to avoid auto session gathering using selenium,
you just need to manually create a SessionData
class for ClaudeAPIClient
constructor, like so...
from claude_api.session import SessionData
cookie_header_value = "The entire Cookie header value string when you visit https://claude.ai/chats"
user_agent = "User agent to use, required"
# You can retrieve this string from /api/organizations endpoint
# If omitted or None it will be auto retrieved when instantiating ClaudeAPIClient
organization_id = "<org_uuid>"
session = SessionData(cookie_header_value, user_agent, organization_id)
Proxies
NOTE ( Only proxies with no user/passwd authentication are supported )
How to set HTTP/S proxies
If you'd like to set an HTTP proxy for all requests, follow this example:
from claude_api.client import HTTPProxy, ClaudeAPIClient
from claude_api.session import SessionData
# Create HTTPProxy instance
http_proxy = HTTPProxy(
"the.proxy.ip.addr", # Proxy IP
8080, # Proxy port
use_ssl=False # Set to True if proxy uses HTTPS schema
)
session = SessionData(...)
# Give the proxy instance to ClaudeAPIClient constructor, along with session data.
client = ClaudeAPIClient(session, proxy=http_proxy)
How to set SOCKS proxies
If you want to opt for SOCKS proxies instead, the procedure is the same, but you need to import the SOCKSProxy
class instead, configuring it with the version number.
from claude_api.client import SOCKSProxy, ClaudeAPIClient
from claude_api.session import SessionData
# Create SOCKSProxy instance
socks_proxy = SOCKSProxy(
"the.proxy.ip.addr", # Proxy IP
8080, # Proxy port
version_num=5 # Either 4 or 5, defaults to 4
)
session = SessionData(...)
# Give the proxy instance to ClaudeAPIClient constructor as usual
client = ClaudeAPIClient(session, proxy=socks_proxy)
Changing Claude model
In case you'd like to change the model used, or you do have accounts that are unable to migrate to latest model, you can override the model_name
string parameter of ClaudeAPIClient
constructor like so:
from claude_api.client import ClaudeAPIClient
from claude_api.session import SessionData
session = SessionData(...)
# Defaults to None (latest Claude model)
client = ClaudeAPIClient(session, model_name="claude-2.0")
You can retrieve the model_name
strings from the official API docs
Changing Organization
As reported in issue #23
if you're encountering 403 errors when using Selenium to auto retrieve a SessionData
class and your account has multiple organizations,
you may want to override the default organization retrieved.
By default get_session_data
retrieves the last organization from the result array found here.
You can override the index to fetch by using parameter organization_index
:
from claude_api.session import get_session_data
# Defaults to -1 (last entry)
session = get_session_data(organization_index=-1)
TROUBLESHOOTING
Some common errors that may arise during the usage of this API:
-
Error [400] (Unable to prepare file attachment):
To fix this error, change the extension of the attachment file to something like .txt, since by default this api will fallback to octet-stream for unknown file extensions, Claude may reject the file data.
-
Error [403]:
*This bug should be already fixed after version 0.2.2*
This api will sometime return a 403 status_code when calling
send_message
, when this happens it is recommeded to look for these things:Check if your IP location is allowed, should be in US/UK, other locations may work sporadically.
Don't try to send the same prompt/file over and over again, instead wait for some time, and change input.
DISCLAIMER
This repository provides an unofficial API for automating free accounts on claude.ai. Please note that this API is not endorsed, supported, or maintained by Anthropic. Use it at your own discretion and risk. Anthropic may make changes to their official product or APIs at any time, which could affect the functionality of this unofficial API. We do not guarantee the accuracy, reliability, or security of the information and data retrieved using this API. By using this repository, you agree that the maintainers are not responsible for any damages, issues, or consequences that may arise from its usage. Always refer to Anthropic's official documentation and terms of use. This project is maintained independently by contributors who are not affiliated with Anthropic.
DONATING
A huge thank you in advance to anyone who wants to donate :)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file unofficial_claude_api-0.3.3.tar.gz
.
File metadata
- Download URL: unofficial_claude_api-0.3.3.tar.gz
- Upload date:
- Size: 16.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 921205a217b627dbe2a4a1f188d56272e6ebce9421f4d4cb215f9666ec80df62 |
|
MD5 | 317c2ed7526d4a389b96bf284baa1643 |
|
BLAKE2b-256 | ed4dad0c74fde0e258c168d7d9de00c9cb5d104614feb8eedfc017e84299b3b7 |
File details
Details for the file unofficial_claude_api-0.3.3-py3-none-any.whl
.
File metadata
- Download URL: unofficial_claude_api-0.3.3-py3-none-any.whl
- Upload date:
- Size: 14.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f2f7597e8461667dcc3505a88965dc8a7c86618e430f41bad1b47878a997a4df |
|
MD5 | c90e38a3c889032058c018a781733496 |
|
BLAKE2b-256 | 7ae124471a99f89928656220781584ca28ff4c18ccac36fbf35a22bcd1104cc8 |