Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't instantiate Claude API Client #78

Open
gullpavon opened this issue Oct 1, 2023 · 4 comments
Open

Can't instantiate Claude API Client #78

gullpavon opened this issue Oct 1, 2023 · 4 comments

Comments

@gullpavon
Copy link

Here is my code trying to instantiate this Claude API

from claude_api import Client
import os

claude_api = Client('sk-ant-my-key-here')

However, I'm getting the following error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[205], line 1
----> 1 claude_api = Client('sk-ant-my-key-here')

File ~/miniconda3/envs/truth/lib/python3.11/site-packages/claude_api.py:13, in Client.__init__(self, cookie)
     11 def __init__(self, cookie):
     12   self.cookie = cookie
---> 13   self.organization_id = self.get_organization_id()

File ~/miniconda3/envs/truth/lib/python3.11/site-packages/claude_api.py:33, in Client.get_organization_id(self)
     31 response = requests.get(url, headers=headers,impersonate="chrome110")
     32 res = json.loads(response.text)
---> 33 uuid = res[0]['uuid']
     35 return uuid

KeyError: 0

Any ideas on how to mitigate this?

@gullpavon
Copy link
Author

gullpavon commented Oct 1, 2023

This solves the issue for those who are also coming to this error:

class Client:
    
  def __init__(self, cookie):
    self.cookie = cookie
    self.organization_id = self.get_organization_id()

  def get_organization_id(self):

        
    BASE_URL = "https://claude.ai"
    API_ENDPOINT = "/api/organizations"
    API_KEY = cookie

    HEADERS = {
      "Content-Type": "application/json",
      "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0",
      "Authority": "claude.ai",  
      "Accept": "*/*",
      "Accept-Language": "en-US,en;q=0.9",
      "DNT": "1",
      "Sec-Fetch-Dest": "empty",
      "Sec-Fetch-Mode": "navigate",
      "Sec-Fetch-Site": "same-origin",
      "Upgrade-Insecure-Requests": "1",
      "Connection": "keep-alive"  
    }

    url = BASE_URL + API_ENDPOINT

    headers = HEADERS.copy()
    headers["Cookie"] = f"sessionKey={API_KEY}"

    response = requests.get(url, headers=headers)

    if response.status_code == 200:
          
          orgs = response.json()

          for org in orgs:
                return org["uuid"]
      
          else:
                print("Error:", response.text)

@sylar003
Copy link

sylar003 commented Oct 7, 2023

@gullpavon It seems not work for me

@Lxb921006
Copy link

Just log in again and get the cookies

@ToanG1
Copy link

ToanG1 commented Nov 20, 2023

This solves the issue for those who are also coming to this error:

class Client:
    
  def __init__(self, cookie):
    self.cookie = cookie
    self.organization_id = self.get_organization_id()

  def get_organization_id(self):

        
    BASE_URL = "https://claude.ai"
    API_ENDPOINT = "/api/organizations"
    API_KEY = cookie

    HEADERS = {
      "Content-Type": "application/json",
      "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0",
      "Authority": "claude.ai",  
      "Accept": "*/*",
      "Accept-Language": "en-US,en;q=0.9",
      "DNT": "1",
      "Sec-Fetch-Dest": "empty",
      "Sec-Fetch-Mode": "navigate",
      "Sec-Fetch-Site": "same-origin",
      "Upgrade-Insecure-Requests": "1",
      "Connection": "keep-alive"  
    }

    url = BASE_URL + API_ENDPOINT

    headers = HEADERS.copy()
    headers["Cookie"] = f"sessionKey={API_KEY}"

    response = requests.get(url, headers=headers)

    if response.status_code == 200:
          
          orgs = response.json()

          for org in orgs:
                return org["uuid"]
      
          else:
                print("Error:", response.text)

thank, this is helpful to me but at first you guy need a new claude cookie

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants