0

I am using Python and I am reading the documentation and using the Get request to search for Flights under a certain price for a project in my online course. I am able to get my requests to go through successfully and return data that I need, but I am trying to search for flights within a 6 month window. Right now I am entering a static date. I am putting in all the required parameters, but I cannot seem to find anywhere I can specify a window for my departure date. A "from" and "to" date specification would be nice. The documentation is at the URL below.

https://developers.amadeus.com/self-service/category/flights/api-doc/flight-offers-search/api-reference

The only thing I can find under "Models" is the dateTimeRange, which only allows you to search within 3 days of the date you input. Even this, no matter how I put it in the parameters, tells me it is an "Invalid query parameter". I need to search within 180 days. I thought if I could at least get this 3 day window to work I could just loop it 60 times.

Wondering if anyone knows a better way to search within this 6 month window, or if you know how to get this dateTimeRange to work.

I have tried entering another parameter as "dateTimeRange": "I3D".

I've tried "dateWindow": "I3D".

I've tried "dateTimeRange": { "date": "2024-07-04", "dateWindow": "I3D" }.

All give me to same "Invalid query parameter".

Below is the code that I am using that is correctly returning the flight data, but I am trying to add the parameter described above without success.

import requests

class FlightSearch:
   
    def __init__(self):
        self.iata_departure_code = "SAC"
        self.bearer_token = ""
        self.flight_response = {}

    def authenticate(self):
        auth_token_endpoint = "https://test.api.amadeus.com/v1/security/oauth2/token"
        api_key = "xxxxxx"
        api_secret = "xxxxxxx"
        header = {
            "Content-Type": "application/x-www-form-urlencoded",
        }
        parameters = {
            "grant_type": "client_credentials",
            "client_id": api_key,
            "client_secret": api_secret,
        }

        response = requests.post(url=auth_token_endpoint, headers=header, data=parameters)
        data = response.json()
        self.bearer_token = data["access_token"]
        return self.bearer_token

    def search_flights(self):
        flight_offers_endpoint = "https://test.api.amadeus.com/v2/shopping/flight-offers"
        header = {
            "Authorization": f"Bearer {self.authenticate()}"
        }
        parameters = {
            "originLocationCode": self.iata_departure_code,
            "destinationLocationCode": "DUB",
            "departureDate": "2024-07-04",
            "adults": 1,
            "currencyCode": "USD",
            "maxPrice": 2000,
            "max": 20,
        }

        flight_response = requests.get(url=flight_offers_endpoint, params=parameters, headers=header)
        return flight_response.json()
1
  • Please provide enough code so others can better understand or reproduce the problem.
    – Community Bot
    Commented Jul 4 at 10:07

1 Answer 1

0

After much digging and reading other students comments, I discovered the task the instructor set for us was not possible with this endpoint. Even in her solution code, there was no way to get a range of departure dates, you could only use 1 departure date. There is another API endpoint I am going to try at https://developers.amadeus.com/self-service/category/flights/api-doc/flight-cheapest-date-search/api-reference which does allow for a range of dates. I am going to change the project to better fit the desired end results.

Not the answer you're looking for? Browse other questions tagged or ask your own question.