skip to Main Content

I am trying to read the property response from airbnb via the API but I somehow can’t get a proper result and I don’t know what I do wrong.

import json
from urllib.request import urlopen

def fetch_airbnb_data():
    api_url = "https://www.airbnb.co.uk/api/v3/StaysPdpSections/3bcbe9ee20da9daa6722934f0b54ef68db41699d179e698c3aeab4ec30a964cb?operationName=StaysPdpSections&locale=en-GB&currency=GBP&variables=%7B%22id%22%3A%22U3RheUxpc3Rpbmc6MTAyNzQ1NzkyNDI2OTA2NDk4OQ%3D%3D%22%2C%22pdpSectionsRequest%22%3A%7B%22adults%22%3A%221%22%2C%22amenityFilters%22%3Anull%2C%22bypassTargetings%22%3Afalse%2C%22categoryTag%22%3Anull%2C%22children%22%3Anull%2C%22disasterId%22%3Anull%2C%22discountedGuestFeeVersion%22%3Anull%2C%22displayExtensions%22%3Anull%2C%22federatedSearchId%22%3Anull%2C%22forceBoostPriorityMessageType%22%3Anull%2C%22hostPreview%22%3Afalse%2C%22infants%22%3Anull%2C%22interactionType%22%3Anull%2C%22layouts%22%3A%5B%22SIDEBAR%22%2C%22SINGLE_COLUMN%22%5D%2C%22pets%22%3A0%2C%22pdpTypeOverride%22%3Anull%2C%22photoId%22%3Anull%2C%22preview%22%3Afalse%2C%22previousStateCheckIn%22%3Anull%2C%22previousStateCheckOut%22%3Anull%2C%22priceDropSource%22%3Anull%2C%22privateBooking%22%3Afalse%2C%22promotionUuid%22%3Anull%2C%22relaxedAmenityIds%22%3Anull%2C%22searchId%22%3Anull%2C%22selectedCancellationPolicyId%22%3Anull%2C%22selectedRatePlanId%22%3Anull%2C%22splitStays%22%3Anull%2C%22staysBookingMigrationEnabled%22%3Afalse%2C%22translateUgc%22%3Anull%2C%22useNewSectionWrapperApi%22%3Afalse%2C%22sectionIds%22%3Anull%2C%22checkIn%22%3Anull%2C%22checkOut%22%3Anull%2C%22p3ImpressionId%22%3A%22p3_1729488926_P3eUXvpmVbFyX_Cq%22%7D%7D&extensions=%7B%22persistedQuery%22%3A%7B%22version%22%3A1%2C%22sha256Hash%22%3A%223bcbe9ee20da9daa6722934f0b54ef68db41699d179e698c3aeab4ec30a964cb%22%7D%7D"
    
    try:
        jsonurl = urlopen(api_url)
        data = json.loads(jsonurl.read())
        print(json.dumps(data, indent=4))
    except Exception as e:
        print(f"Error fetching data: {e}")

fetch_airbnb_data()

I get the following error:

    "errors": [
        {
            "message": "Response not successful: Received status code 400",
            "locations": [
                {
                    "line": 1,
                    "column": 1
                }
            ],
            "path": [],
            "extensions": {
                "response": {
                    "statusCode": 400,
                    "body": {
                        "error_code": 400,
                        "error": "invalid_key",
                        "error_message": "Invalid API key."
                    }
                },
                "code": "Bad Request"
            }
        }
    ],
    "data": null
}```

2

Answers


  1. As suggested in the error response, you need to add in an API key.

    I’d suggest reading the documentation properly and using the requests library for API calls, the method you’re using right now is performing a simple GET request without any parameters.

    Could you also share the API documentation? Because I could not find any API documentation for the v3 API.

    Login or Signup to reply.
  2. I think it is much more convenient to use the requests library for these purposes

    It is better to pass the request parameters in params, and not in the address itself via "?"

    You can use https://curlconverter.com to translate the curl request into some programming language

    import requests
    
    response = requests.get(
        url='https://www.airbnb.co.uk/api/v3/StaysPdpSections/3bcbe9ee20da9daa6722934f0b54ef68db41699d179e698c3aeab4ec30a964cb',
        params={
        'operationName': 'StaysPdpSections',
        'locale': 'en-GB',
        'currency': 'GBP',
        'variables': '{"id":"U3RheUxpc3Rpbmc6MTAyNzQ1NzkyNDI2OTA2NDk4OQ==","pdpSectionsRequest":{"adults":"1","amenityFilters":null,"bypassTargetings":false,"categoryTag":null,"children":null,"disasterId":null,"discountedGuestFeeVersion":null,"displayExtensions":null,"federatedSearchId":null,"forceBoostPriorityMessageType":null,"hostPreview":false,"infants":null,"interactionType":null,"layouts":["SIDEBAR","SINGLE_COLUMN"],"pets":0,"pdpTypeOverride":null,"photoId":null,"preview":false,"previousStateCheckIn":null,"previousStateCheckOut":null,"priceDropSource":null,"privateBooking":false,"promotionUuid":null,"relaxedAmenityIds":null,"searchId":null,"selectedCancellationPolicyId":null,"selectedRatePlanId":null,"splitStays":null,"staysBookingMigrationEnabled":false,"translateUgc":null,"useNewSectionWrapperApi":false,"sectionIds":null,"checkIn":null,"checkOut":null,"p3ImpressionId":"p3_1729488926_P3eUXvpmVbFyX_Cq"}}',
        'extensions': '{"persistedQuery":{"version":1,"sha256Hash":"3bcbe9ee20da9daa6722934f0b54ef68db41699d179e698c3aeab4ec30a964cb"}}',
    }
    )
    
    print(response.status_code)
    print(response.json())
    

    It’s strange that it has a response code of 200, but in the body of the response itself

    {'errors': [{'message': 'Response not successful: Received status code 400', 'locations': [{'line': 1, 'column': 1}], 'path': [], 'extensions': {'response': {'statusCode': 400, 'body': {'error_code': 400, 'error': 'invalid_key', 'error_message': 'Invalid API key.'}}, 'code': 'Bad Request'}}], 'data': None}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search