# 401 Error on HTTP request

**URL:** <https://discussions.flightaware.com/t/401-error-on-http-request/93585>\
**Category:** AeroAPI\
**Created:** [July 10, 2024, 10:28pm UTC](https://discussions.flightaware.com/t/401-error-on-http-request/93585 "2024-07-10T22:28:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jastrout](https://sea1.discourse-cdn.com/flex019/user_avatar/discussions.flightaware.com/jastrout/32/117752_2.png) [@jastrout](https://discussions.flightaware.com/u/jastrout)\
**Post date:** [July 10, 2024, 10:28pm UTC](https://discussions.flightaware.com/t/401-error-on-http-request/93585/1 "2024-07-10T22:28:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nasageek](https://sea1.discourse-cdn.com/flex019/user_avatar/discussions.flightaware.com/nasageek/32/122208_2.png) [@nasageek](https://discussions.flightaware.com/u/nasageek)\
**Post date:** [July 10, 2024, 10:43pm UTC](https://discussions.flightaware.com/t/401-error-on-http-request/93585/2 "2024-07-10T22:43:52Z")

</div>

You should follow the instructions at [AeroAPI Developer Portal - FlightAware](https://www.flightaware.com/aeroapi/portal/documentation#auth) for authenticating with AeroAPI. Specifically, the API key goes in the `x-apikey` header and no username should be specified.

-Chris

---

<div class="post-metadata">

**Author:** ![jastrout](https://sea1.discourse-cdn.com/flex019/user_avatar/discussions.flightaware.com/jastrout/32/117752_2.png) [@jastrout](https://discussions.flightaware.com/u/jastrout)\
**Post date:** [July 10, 2024, 10:55pm UTC](https://discussions.flightaware.com/t/401-error-on-http-request/93585/3 "2024-07-10T22:55:25Z")

</div>

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