Search code examples
hypixel-api

KeyError with hypixel api when profile json does exist


I'm using the Hypixel API while getting a response I try to find a certain key (last_save) in the json request.get(url) gets but while to print it, it keeps saying that the key doesn't exist this is my code

import requests
import json
api_key = 'API KEY HERE'
uuid = 'UUID HERE'
profileID = 'PROFILE ID HERE'

 #Set the player name to the desired username
player = 'PLAYER NAME HERE'

 #Make the request to the API
url = f'https://api.hypixel.net/skyblock/profile?key={api_key}&profile={profileID}'
response = requests.get(url)

 #Parse the JSON response
data = response.json()
last_save = data["last_save"]

 #Print the player's information
print(data['profile'])

this is the error it throws

'Traceback (most recent call last):
  File "c:\Users\...\main.py", line 16, in <module>
    last_save = data["last_save"]
KeyError: 'last_save''

how do i fix


Solution

  • You are trying to access key that doesn't exists. The structure, that /profile returns looks like this:

    {
    
        "success": true,
        "profile": {
            "profile_id": "bfcb6779-b1f9-41fc-92d7-88f8bc1d12e8",
            "members": {},
            "cute_name": "string",
            "selected": true,
            "community_upgrades": { },
            "banking": {},
            "game_mode": "ironman"
        }
    }
    

    Because of this you need to change your code to something like this:

    # Parse the JSON response
    data = response.json()
    
    # Print the player's information
    print(data['profile']['members']['your minecraft uuid'])
    

    and yeah, last_save removed completely. (check https://github.com/HypixelDev/PublicAPI/issues/562)