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.
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()