For some reason, I can call the API without an API key...?

Hi, can someone explain to me how/why I’m able to call the OpenAI API through a python script, and return results, using my tokens, despite not having included my API key in my python script? At some point I included my API key in the settings of one of the virtual environments I was using to code python and test the OpenAI API, but I am not using that virtual environment now. I am just executing a script from python through the command line, and it’s generating good results, and using up my tokens, but my API key is nowhere in the python script. How is OpenAI authenticating my call?

Welcome to the forum!

Can you post your code and a coy of the prompt and output produced?

(SHH, that’s our secret!)

Actually, the openai libraries will look for an environment variable containing the key, and that’s likely the method being used.

Setting an environment variable once in the shell can make it persistent for the session or subshells, and it can also be infused in different ways.

text-davinci-003

Also, you can just do a code search for “sk-” to see where you’ve included it.

2 Likes

Oh that’s brilliant, thanks _j! Indeed, I did put my API key into a User Environment Variable. But 1) how does OpenAI know what my User Environment Variables are? Is my local python instance pulling that data from my environment and passing it to OpenAI in the background somehow? And 2) How can I validate the API KEY that OpenAI used to authenticate my call?

FYI this was my simple code:

import openai
prompt = “What’s the capitol of North Dakota?”
response = openai.Completion.create(engine=“text-davinci-001”, prompt=prompt, max_tokens=6)
print(response)

There is currently no mechanism to see which of many keys you may have created made a particular billing or usage.

There is only a workaround that requires more accounts (and more cel phones): the strange “organization” system.

The python environment or interpreter has the environment scope of the parent, such as a bash shell, given the environment stored in your profile. The openai library uses its environment variable if no other key is then specified.

If @_j provided the solution, please mark it as such so it is easy for others to find.