Incorrect API key provided

Hey everyone,

Having some issues starting out. Trying to just connect with an existing assistant in my organization API (not Personal). Created an API key and did change the default organization to the desired one. The code giving me the error is the following:

import os
from dotenv import load_dotenv
from openai import OpenAI


client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
thread = client.beta.threads.create()

Getting the error on the last line, trying to create a thread, so I can chat with the assistant.

openai.AuthenticationError: Error code: 401 - {‘error’: {‘message’: ‘Incorrect API key provided: sk-gl4hF***************************************Tp1k. You can find your API key at’, ‘type’: ‘invalid_request_error’, ‘param’: None, ‘code’: ‘invalid_api_key’}}

Does that API key work with the normal Chat Completions API?

You can use the below code to test it easily.

import openai
from openai import OpenAI

openai.api_key = ""  # Replace with your actual OpenAI API Key

client = OpenAI()

completion =
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Tell me a cool fact about sharks."},

for chunk in completion:
    if chunk.choices[0].delta.content:  # Check if content exists
        print(chunk.choices[0].delta.content, end='', flush=True)

If that works then maybe try doing the client = OpenAI() part in the manner shown above.

Sadly I get the same error…

For some reason I don’t fully understand this worked. Couldn’t get it to work without api_key=openai.api_key in the client line

openai.api_key = ""  # Replace with your actual OpenAI API Key
client = OpenAI(api_key=openai.api_key)
1 Like