401 Error on HTTP request

Hi,

I have a personal AeroAPI subscription. I have been trying to use the Flights/search endpoint to return results. I keep getting a 401 error. I am using my FA username and an API key that I generated. Super basic. Code below.

import requests
from requests.auth import HTTPBasicAuth

Define your FlightAware credentials

username = ‘XXXX’
api_key = ‘XXXXXXXXXXXXXXX’

Define the latitude and longitude range

lat_min = 25.0
lat_max = 26.0
lon_min = 55.0
lon_max = 56.0

Construct the query

query = f"-latlong {lat_min} {lon_min} {lat_max} {lon_max} -type H" # -type H is used to filter for helicopters

Make the request to the AeroAPI

url = “https://aeroapi.flightaware.com/aeroapi/flights/search
params = {
‘query’: query
}

response = requests.get(url, params=params, auth=HTTPBasicAuth(username, api_key))

Check the response

if response.status_code == 200:
data = response.json()
print(data)
else:
print(f"Error: {response.status_code}")

Thanks in advance.

You should follow the instructions at AeroAPI Developer Portal - FlightAware for authenticating with AeroAPI. Specifically, the API key goes in the x-apikey header and no username should be specified.

-Chris

Thank you for the help. Changed the code and now I am authenticating.