unofficial-claude-api: brotli.error and 403

I was using the API a couple of days ago and it was working fine.

Errors:

When using create_chat() I got Message limit hit, cannot create chat… (which I know is incorrect because I have messages left) so I changed the code and printed the response.status_code and got 403

When using send_message() I get brotli.error: BrotliDecompress failed

When using get_all_chat_ids() I get an empty array (which I know is incorrect because I have conversations) so I changed the code and printed the response.status_code and got 403

What I have tried:

Changing the __BASE_URL from “https://claude.ai” to “https://api.claude.ai” Host in headers from “claude.ai” to “api.claude.ai”. Using a US VPN connection.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (9 by maintainers)

Most upvoted comments

Nice!

Auto closing this issue once the new branch is published.

@HokkaidoInu Waiting for feedback before merging the new branch into main, I’ll have to create a new release so I’d like to know if everything works now.

All works fine for me on the new branch. Thank you 🎉

I found out the reason behind get_session_data() not working for me. It’s because at https://claude.ai/api/organizations I get 2 objects in the array and the data is different in each as you can see here (changed json to markdown because idk how to pretty print json here): uuid name settings.claude_console_privacy settings.allowed_invite_domains capabilities.0 rate_limit_tier billing_type free_credits_status data_retention api_disabled_reason api_disabled_until billable_usage_paused_until raven_type created_at updated_at active_flags 7685jh8-4nbvb7-b46nb-34nb6-hbhb63-notMyActualId NaN default_private api auto_api_evaluation default out_of_credits 2024-06-28T17:07:50.208422Z 2024-06-28T17:07:50.208422Z 43vy74yv-v3hj5-4366h-4b6-nb46b636-notMyActualId example@gmail.com’s Organization default_private chat default_claude_ai default 2024-06-28T21:37:11.003687Z 2024-06-28T21:37:11.003687Z

So I had to change the 0 in this line to a 1

https://github.com/st1vms/unofficial-claude-api/blob/422581d5766163e3b2477f7cf3acd3c4bcc0d0c7/claude_api/session.py#L85

It means your account has multiple organizations…

At this point what I can do is to retrieve by default the last element in this array by indexing with -1 since the latest organization will always be the last. And also leave an integer parameter in get_session_data called organization_index to override it.

I’ve also made a section in the README, all changes are already in the attached branch of this issue, they will be merged after feedback.

I found out the reason behind get_session_data() not working for me. It’s because at https://claude.ai/api/organizations I get 2 objects in the array and the data is different in each as you can see here (changed json to markdown because idk how to pretty print json here):

uuid name settings.claude_console_privacy settings.allowed_invite_domains capabilities.0 rate_limit_tier billing_type free_credits_status data_retention api_disabled_reason api_disabled_until billable_usage_paused_until raven_type created_at updated_at active_flags
7685jh8-4nbvb7-b46nb-34nb6-hbhb63-notMyActualId NaN default_private api auto_api_evaluation default out_of_credits 2024-06-28T17:07:50.208422Z 2024-06-28T17:07:50.208422Z
43vy74yv-v3hj5-4366h-4b6-nb46b636-notMyActualId example@gmail.com's Organization default_private chat default_claude_ai default 2024-06-28T21:37:11.003687Z 2024-06-28T21:37:11.003687Z

So I had to change the 0 in this line to a 1 https://github.com/st1vms/unofficial-claude-api/blob/422581d5766163e3b2477f7cf3acd3c4bcc0d0c7/claude_api/session.py#L85

The error is caused by this line: https://github.com/st1vms/unofficial-claude-api/blob/422581d5766163e3b2477f7cf3acd3c4bcc0d0c7/claude_api/client.py#L476

I figured out the reason is because even though it says that the content-encoding is br, when I printed out the response.content it returned a byte response like this b'[{"geonameId:"703448"}..........'. mentioned in this stack overflow question and so because in here

https://github.com/st1vms/unofficial-claude-api/blob/422581d5766163e3b2477f7cf3acd3c4bcc0d0c7/claude_api/client.py#L481

is run data_bytes.decode so this

https://github.com/st1vms/unofficial-claude-api/blob/422581d5766163e3b2477f7cf3acd3c4bcc0d0c7/claude_api/client.py#L476

just needs to be changed from br_decompress(buffer) to buffer

Since I already received complaints about this error, I decided to drop the Brotli decoding. Changes are already live on the new branch, but I don’t know if I’ll merge into main any time soon without some feedback…