401 Incorrect API key provided - loop

Im still getting an incorrect api key, even though it’s correct. I am totally lost, I’ve even added the API key into my windows env variables, still doesnt work. Has anyone gotten a solution for this? Thank you very much.

Open Command Prompt (Win + R, type cmd, and press Enter).

run this command:

setx OPENAI_API_KEY "your-api-key-here"

Restart your Command Prompt to apply the changes.

verify by using this command:

echo %OPENAI_API_KEY%

Check it by doing this:

curl https://api.openai.com/v1/chat/completions ^
  -H "Content-Type: application/json" ^
  -H "Authorization: Bearer %OPENAI_API_KEY%" ^
  -d "{ \"model\": \"gpt-4o-mini\", \"messages\": [{\"role\": \"user\", \"content\": \"Hello, how are you?\"}] }"

or in powershell this:

curl https://api.openai.com/v1/chat/completions `
  -H "Content-Type: application/json" `
  -H "Authorization: Bearer $env:OPENAI_API_KEY" `
  -d '{ "model": "gpt-4o-mini", "messages": [{"role": "user", "content": "Hello, how are you?"}] }'

Or even this as a temporary check in powershell:

$env:OPENAI_API_KEY="your-api-key-here"

curl https://api.openai.com/v1/chat/completions `
  -H "Content-Type: application/json" `
  -H "Authorization: Bearer $env:OPENAI_API_KEY" `
  -d '{ "model": "gpt-4o-mini", "messages": [{"role": "user", "content": "Hello, how are you?"}] }'
1 Like