Claude-API icon indicating copy to clipboard operation
Claude-API copied to clipboard

Can't instantiate Claude API Client

Open gullpavon opened this issue 1 year ago • 4 comments

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 avatar Oct 01 '23 16:10 gullpavon

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)

gullpavon avatar Oct 01 '23 18:10 gullpavon

@gullpavon It seems not work for me

Sylar-W avatar Oct 07 '23 09:10 Sylar-W

Just log in again and get the cookies

Lxb921006 avatar Oct 08 '23 08:10 Lxb921006

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

ToanG1 avatar Nov 20 '23 02:11 ToanG1