-2

I am experimenting with the IEX API. I have created a website to query stock info via a symbol (e.g. NFLX or AAPL) in an IDE provided via the edX Harvard CS50x course and am now trying to set up VS Code desktop to be independent of their provided IDE.

I am struggling to set my API_KEY in VS Code, desktop version. I downloaded VS Code (desktop version +python, nodejs, flask, etc. etc.), cloned my repository from Github and then tried to run the website to query stock info (which works perfectly fine in the IDE after setting my api-key).

The code for the key looks as follows:

# Contact API
try:
    api_key = os.environ.get("API_KEY")
    url = f"https://cloud.iexapis.com/stable/stock/{urllib.parse.quote_plus(symbol)}/quote?token={api_key}"
    response = requests.get(url)
    response.raise_for_status()

except requests.RequestException:
    return None

I get the following error: API_KEY not set in VS Code Desktop

I am working on windows and have managed to set the flask app with this syntax: set FLASK_APP=app.py

I set the API_KEY accordingly (set API_KEY=xxx), but when running flask, I get the error above. I use this command to run flask:python3 -m flask run

I tried to set the API_KEY within the app.py file, just to see if it would work (despite security issues), but no success: os.environ["API_KEY"] = "xxx"

Any experiences with this in windows? I found this stackoverflow post, but that guy didn't get an answer either: How do I set an API_KEY in Windows terminal?

1 Answers1

0

I managed to make it work: My FLASK_APP app.py file was checking if the API_KEY was set like this:

# Make sure API key is set

if not os.environ.get("API_KEY"):

        raise RuntimeError("API_KEY not set")

I do not know how to set the api key with the correct syntax, so I commented this part out and inserted the api key as a string in my code instead: api_key = 'xxx'

Apparently, this is now a security issue, since I hard-coded my api key, but at least it runs now on windows in the VS code desktop version. I also tried to hide the key away in a .env file which works (How to set and get environment variables in python) but still no solution for setting it from the command line.

not a robot
  • 2,436
  • 1
  • 10
  • 24