Question:
Keep getting an error saying: “Failed to retrieve property prices”
Have checked my API key on postman and its all working
Repl link:
https://replit.com/@wincollad2/IckyFelineGnuassembler
import requests
API_KEY = "X"
BASE_URL = "https://api.propertydata.co.uk"
def get_property_prices(postcode):
endpoint = "/prices"
url = BASE_URL + endpoint
params = {"postcode": postcode, "api_key": API_KEY}
response = requests.get(url, params=params)
if response.status_code == 200:
return response.json()
else:
return None
# Example usage
postcode = "SW1A 1AA"
property_prices = get_property_prices(postcode)
if property_prices:
print(f"Property prices for {postcode}:")
print(property_prices)
else:
print("Failed to retrieve property prices.")
Welcome to the forums @wincollad2 !
I checked their documentation and your API call should include key
as a parameter, not api_key
.
So, you should modify the params dictionary to change api_key
to key
.
And, if you are using the endpoint /prices you are missing the bedrooms parameter.
import requests
API_KEY = "AROXZONHL1"
BASE_URL = "https://api.propertydata.co.uk"
def get_property_prices(postcode, bedrooms):
endpoint = "/prices"
url = BASE_URL + endpoint
params = {"postcode": postcode, "key": API_KEY, "bedrooms": bedrooms} #made the change here
response = requests.get(url, params=params)
if response.status_code == 200:
return response.json()
else:
print(f"Error: {response.status_code}")
print(f"Response: {response.content}")
return None
# Example usage
postcode = "SW1A 1AA"
bedrooms = 2 # This is the one you are missing, remember to change for what you desire.
property_prices = get_property_prices(postcode, bedrooms)
if property_prices:
print(f"Property prices for {postcode}:")
print(property_prices)
else:
print("Failed to retrieve property prices.")
thanks so much Windlother! this is magical to see this working. Replit is a great community
2 Likes