I can access Alpha Vantage data even with invalid API keys [python]

122 views Asked by At

I'm using Alpha Vantage for a project of mine and I noticed that as long as an API key is given as a placeholder, even invalid ones will return data for any endpoint. This is new to me as of today, and interferes with my project. Does anyone know why this is happening?

Entering any string into the parameter of api_key in the following function, for example, will return data:

import requests

def validate_alpha_vantage_key(api_key):
    test_url = f"https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=SPY&apikey={api_key}"
    response = requests.get(test_url)

    if response.status_code == 200:
        json_response = response.json()
        print(json_response)
        if json_response.get("message") == "forbidden.":
            print("Invalid Alpha Vantage API key.")
            return False
        elif "Global Quote" in json_response:  # Assuming valid response contains "Global Quote" key
            print("Valid Alpha Vantage API key.")
            return True
        else:
            print("Received unexpected JSON response from Alpha Vantage API.")
            return False
    else:
        print(f"Received unexpected status code {response.status_code} from Alpha Vantage API")
        return False
0

There are 0 answers