Cant get my API key to work in python

Made the API key as instructed but get a key error and crashed my program when i try to use

genai.configure(api_key=os.environ["MY_API_KEY"])

giving me a KeyError in traceback.

What am I doing wrong?

How are you setting the API Key in your environment?

How am i meant to? I followed the quickstart guide and i got as far as getting the API key and it advises me to set it as an environment variable using

export API_KEY=<YOUR_API_KEY>

except i havent figured out where or how to do that yet

For development and testing, you’d do that in the terminal / shell where you will run your python program.

export API_KEY=whatever sets the environment variable so it is available in programs that are run in that same shell. (What are called “children programs”.)

In your program, os.environ["API_KEY"] reads the value that “API_KEY” was set to in the shell.

Make sure, btw, that the name you set it with in your export, and the name you read it in with os.environ are the same.

What constitutes an environment if working only in the cloud like with Co laboratory or AI Studio?

Welcome to the forums!
Good question.

AI Studio doesn’t require an API Key.

Colab has a “secure settings” area to the left of the notebook where you can save the key and then access it from within the notebook.

Other cloud environments have other ways to read in a key from the environment, including .env files and secure configuration servers.