Incorrect API key again. Anyone?

Just subscribed to the paid plan: I’m able to run the online examples or even the playground but I’m getting the infamous “Incorrect API key provided” error using chatgpt-wrapper and/or curl. Of course, I double checked my environment variable is correctly setup (no extra spaces or characters).

curl https://api.openai.com/v1/completions   -H "Content-Type: application/json"   -H "Authorization: Bearer $OPENAI_API_KEY"   -d '{
  "model": "text-davinci-003",
  "prompt": "Translate this into 1. French, 2. Spanish and 3. Japanese:\n\nWhat rooms do you have available?\n\n1.",
  "temperature": 0.3,
  "max_tokens": 100,
  "top_p": 1,
  "frequency_penalty": 0,
  "presence_penalty": 0
}'
{
    "error": {
        "message": "Incorrect API key provided: sk-MTj1a******************************************************************************************fHXb. You can find your API key at https://platform.openai.com/account/api-keys.",
        "type": "invalid_request_error",
        "param": null,
        "code": "invalid_api_key"
    }
}

1 Like

You can’t use the ChatGPT website as an endpoint, you should use the OpenAI API library and pass it your API key, best practice is to use environment variable to store your key or a key handling service.

Place this in .bashrc to ensure it is remembered across sessions.
export OPENAI_API_KEY=your_openai_api_key

In python

import openai

API_KEY = os.getenv("OPENAI_API_KEY")  
openai.api_key = API_KEY

openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who won the world series in 2020?"},
        {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
        {"role": "user", "content": "Where was it played?"}
    ]
)

from OpenAI Platform

Thanks for the reply, but as you may see my CURL command doesn’t call the “website” but calls the HTTP API endpoints :

The openai python package is just a wrapper to the API HTTP endpoints under the hood.

Even the CURL command from the doc is failing:

➜ curl https://api.openai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
     "model": "gpt-3.5-turbo",
     "messages": [{"role": "user", "content": "Say this is a test!"}],
     "temperature": 0.7
   }'
{
    "error": {
        "message": "Incorrect API key provided: sk-MTj1a******************************************************************************************fHXb. You can find your API key at https://platform.openai.com/account/api-keys.",
        "type": "invalid_request_error",
        "param": null,
        "code": "invalid_api_key"
    }
}

Needless to say, same behavior for your python example:

openai.error.AuthenticationError: Incorrect API key provided: sk-qQ06L******************************************************************************************rDfa. You can find your API key at https://platform.openai.com/account/api-keys.

OpenAI any help?

SOLVED: I generated a new key and it’s all working fine now.

1 Like