FlightAware Discussions

Python technical support

I have obtained the API key, and I want to obtain some flight data, including the actual takeoff and landing time and type of aircraft. I tried to verify with the example you provided, but failed. I hope to obtain python code support.

This is the output, can you help me to see what the problem is

Are you using the python example seen at https://flightaware.com/commercial/flightxml/? If you are using a Python 3 environment it will require a small change to make the print statements correct. Otherwise, can you please post the example, with your username and key removed, that isn’t working properly?

This is my Python code, can you help me see what went wrong

The API key associated with your account is for FlightXML v2, but the fxmlURL in the example used is for FlightXML v3. This will cause the authentication to fail and the response status code to be a 401. If your example only prints a result on a status code 200 occurs then this is why there is no output.

Please try changing the fxmlUrl the value to https://flightxml.flightaware.com/json/FlightXML2/ and update the request to match the format of one of the FlightXML v2 requests found here. You may also want to add a condition to print the error if a status code 200 is not returned.

I have fxmlUrl change to https://flightxml.flightaware.com/json/FlightXML2/, but the result is still the same, according to “the Process finished with exit code 0”, is there any solution?

You need to make your program print out the error message if the status_code != 200, otherwise it will be very difficult to debug. You are likely getting an authentication failure, or invalid arguments or function name. Be sure you are not still attempting to call FlightInfoStatus if you are using FlightXML2–that function is only available under FlightXML3 (the closest equivalent in FlightXML2 is FlightInfoEx).

My KPI key is invalid. What’s the matter,Can you give me a solution?

The following works fine for me:

import requests
import sys

result = []

username = "xxxxxxxxx"
apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"

fxmlUrl = "https://flightxml.flightaware.com/json/FlightXML2/"

payload = {'ident': 'N56821', 'howMany': 5}

response = requests.get(fxmlUrl + "FlightInfoEx", params=payload, auth=(username, apiKey))
if response.status_code == 200:
    decodedResponse = response.json()
    for flight in decodedResponse['FlightInfoExResult']['flights']:
        print(f"{flight['ident']} = {flight['faFlightID']} ({flight['origin']} to {flight['destination']})")
else:
    print(f"Got error {response.status_code}")
    print(response.text)

My code problem has been solved. I tried it with my colleague’s KPI key and found that the code is valid, so I found that there is a problem with my KPI key. What is the reason? The expiration date on my KPI key says “never” (as shown in the picture above). Is there a solution?