API Key Trouble

These are not official API’s and violate our terms of use.

Hello There, thank you very much!

I face a similar issue with Curl in windows 10 as I cannot use my environment variable in the H param. Can anyone help out?

curl https://api.openai.com/v1/models -H “Authorization: Bearer $OPENAI_API_KEY”
{
“error”: {
“message”: “Incorrect API key provided: $OPENAI_***_KEY. You can find your API key at OpenAI API”,
“type”: “invalid_request_error”,
“param”: null,
“code”: “invalid_api_key”
}
}

Many thanks for your help!
Best,
Jan

Maybe this is resolved in principle since the thread has gone quiet, but I have still had the same problem today (in March 2023) and it seems to affect both terminal commands and python scripts intermittently and unpredictably. The chatbot itself - gpt-3.5-turbo version in beta on playground - suggested that it may be an issue with how the openai module is treating assignments of environment variables in the background. I have no idea, but it reckoned that the dotenv approach would work, and it did.
Installing the latest version of dotenv (1.0.0 on macOS) and then, assuming that NOTHING is in the .env file except the unquoted sk-… API_KEY:

import dotenv
for k in dotenv.dotenv_values():
openai.api_key=k

Because there is only one entry in the dictionary returned by dotenv.dotenv_values() with a blank value and key equal to the OPEN_API_KEY.
It’s ugly, but it works. First post on this forum so apologies if I’ve done or said something wrong.

1 Like

Dear pudepiedj,
thank you very much. Your solution might work on macOS, but do you know how to apply dotenv directly in curl on windows 10? Running the API from curl, is the main issue i face. Did not digg in python yet, but the API runs properly, but not in curl.
Best,
Jan

I can’t help with Windows 10, but I can reproduce the error and - I think - resolve it using curl from a macOS terminal, so perhaps that will help.
The problem appears to be that the assignment of the OPENAI_API_KEY from within Python doesn’t transfer to the environment variables used by terminal, as printenv confirms, so it can’t find it. I have no idea how to solve that but this does work in terminal for me (note carefully the semi-colon after the key definition):

OPENAI_API_KEY=sk-…; echo $OPENAI_API_KEY

curl https://api.openai.com/v1/embeddings
-H “Content-Type: application/json”
-H “Authorization: Bearer $OPENAI_API_KEY”
-d ‘{“input”: “This is a test embedding using ADA”, “model”:“text-embedding-ada-002”}’

Notes: this assigns a variable to the shell not to the environment, but at least it allows the code to run in situ rather than entering the sk-… directly into the curl command. Assigning to an environment variable doesn’t seem to work so maybe the API isn’t accessing the environment correctly? Hope this helps.

POSTSCRIPT:
I think the environment variable can be set temporarily and will then work. This requires a subtle change to the assignment which has something to do with working in a different shell - you may need to use bash where I have zsh (and note the single quotes):

OPENAI_API_KEY=sk-… zsh -c ‘echo $OPENAI_API_KEY’
then curl as before.

Gleaned from bash - How to print / echo environment variables? - Stack Overflow

Dear pudepiedj,
many thanks for your appreciated inputs and explanations. Unfortunately none of it solves it in windows 10. I can access API in Python but still not in Curl and I tried in Powershell too. Best, Jan

A neater solution which may be transferable to Windows 10 suitably modified by using some combination of doskey, call and *.bat is to put the API_KEY inside a file then execute that file from the terminal using what on a Mac is source. I think this method is common and that you will have an equivalent of zsh. Use any text editor to create a file called “.myapikey”, save it and execute it from the terminal.
This is on Mac:

sudo nano /.myapikey
# inside nano insert this line into the .myapikey file
OPENAI_API_KEY="sk-..."
# save the file and close the editor
source /.myapikey
echo $OPENAI_API_KEY
sk-...

I asked gpt-3.5-turbo what the equivalent on Windows 10 is, and it suggested this:

On Windows 10, the equivalent command sequence to set an environment variable from a file and use it in a command prompt window would be:

1. Open a command prompt window.

2. Navigate to the folder where the `.myfile` file is located using the `cd` command.

3. Use the `set` command to set the environment variable. For example:

set /p OPENAI_API_KEY=<.myfile


This command reads the value of `OPENAI_API_KEY` from the `.myfile` file and sets it as an environment variable.

4. Verify that the environment variable is set correctly by typing `echo %OPENAI_API_KEY%` and pressing Enter. This should display the value of the `OPENAI_API_KEY` variable.

Note that the syntax for setting environment variables on Windows is slightly different from MacOS/Linux. On Windows, you use the `%` symbol to enclose the name of the environment variable when you want to reference its value, rather than using the `$` symbol like on MacOS/Linux.
1 Like

This is great @pudepiedj If curl on CMD does not work properly, or only with restrictions. Windows users can use powershell as well:

Get-ExecutionPolicy
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force

$headers = @{
    "Authorization" = "Bearer $env:OPENAI_API_KEY"
    "model" = "text-davinci-002"
}
Invoke-WebRequest -Uri "https://api.openai.com/v1/models" -Headers $headers
Get-ChildItem Env:OPENAI_API_KEY

I will check your proposition :slight_smile:
Best, Jan

(newbie) setting up API. The OpenAI website doesn’t appear to be showing me my full API keys on the API key page? Both the existing key and the one generated by me have ellipses in them, and I don’t see how or where to see the full key. (tried to google this but without success).

You have to copy it when you generate the key. After that you cannot see you whole key.

Thanks! Helpful! Got it this time, and now also see it’s explained in the help.

I just find the solution in the OpenAI community Forum Just Import OS and add this line of code:

os.environ["OPENAI_API_KEY"] = <"your_api_key">