# AuthenticationError: Error code: 401

**URL:** <https://community.openai.com/t/authenticationerror-error-code-401/766947>\
**Category:** API\
**Created:** [May 20, 2024, 1:03pm UTC](https://community.openai.com/t/authenticationerror-error-code-401/766947 "2024-05-20T13:03:14Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![StefanK](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/stefank/32/374790_2.png) [@StefanK](https://community.openai.com/u/StefanK)\
**Post date:** [May 20, 2024, 1:03pm UTC](https://community.openai.com/t/authenticationerror-error-code-401/766947/1 "2024-05-20T13:03:14Z")

</div>

Hello,

I tried to run some inference in a Jupyter Notebok but I cannot manage to get a valid connection.

The documentation and looking here in the forum or online didn’t help me at all.

I added a credit card and have a balance of 10 USD.

The key is copy and pasted correctly.

In the Notebook I tried to set the key with these 3 versions:

```auto
openai.api_key = 'sk-proj-XXX'
os.environ['OPENAI_API_KEY'] = 'sk-proj-XXX'
%env OPENAI_API_KEY='sk-proj-XX'

```

When I try to run this code:

```auto
import os
from openai import OpenAI

client = OpenAI(
    # This is the default and can be omitted
    api_key=os.environ.get("OPENAI_API_KEY"),
)

gpt_model = "gpt-3.5-turbo-0125"

chat_completion = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": "Say this is a test",
        }
    ],
    model=gpt_model,
)

```

or this

```auto
client = OpenAI(
  organization='org-XXX', # it has the actual org ID
  project='proj_XXX', # it has the actual project ID
)

gpt_model = "gpt-3.5-turbo-0125"

chat_completion = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": "Say this is a test",
        }
    ],
    model=gpt_model,
)

```

I get the same error message:

```auto
AuthenticationError: Error code: 401 - {'error': {'message': "Incorrect API key provided: 'sk-proj ********************************************** Ldy'. You can find your API key at https://platform.openai.com/account/api-keys.", 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_api_key'}}

```

As I am fairly new to this. What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![QuantumCoderr](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/quantumcoderr/32/192644_2.png) [@QuantumCoderr](https://community.openai.com/u/QuantumCoderr)\
**Post date:** [May 20, 2024, 1:09pm UTC](https://community.openai.com/t/authenticationerror-error-code-401/766947/2 "2024-05-20T13:09:38Z")

</div>

Hmmm…  
Try it:

```auto
from openai import OpenAI

client = OpenAI(
      api_key="API_KEY_OPENAI"
)

```

maybe you specified the wrong key? or the key was created incorrectly. When creating a key, try giving it all access rights.

---

<div class="post-metadata">

**Author:** ![StefanK](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/stefank/32/374790_2.png) [@StefanK](https://community.openai.com/u/StefanK)\
**Post date:** [May 20, 2024, 1:14pm UTC](https://community.openai.com/t/authenticationerror-error-code-401/766947/3 "2024-05-20T13:14:42Z")

</div>

I tried it and I get the same result.

The key has all permissions:

 ![image](https://us1.discourse-cdn.com/openai1/original/4X/d/0/c/d0cdb9f21106e5f4da385b036904b2d8096b650f.png)

---

<div class="post-metadata">

**Author:** ![geecee](https://avatars.discourse-cdn.com/v4/letter/g/96bed5/32.png) [@geecee](https://community.openai.com/u/geecee)\
**Post date:** [May 20, 2024, 4:19pm UTC](https://community.openai.com/t/authenticationerror-error-code-401/766947/4 "2024-05-20T16:19:44Z")

</div>

Have you tried generating a new key and seeing if you get the same result?

---

<div class="post-metadata">

**Author:** ![\_j](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/_j/32/766292_2.png) [@\_j](https://community.openai.com/u/_j)\
**Post date:** [May 20, 2024, 4:27pm UTC](https://community.openai.com/t/authenticationerror-error-code-401/766947/5 "2024-05-20T16:27:06Z")

</div>

> [@QuantumCoderr](#):
>
> ```auto
> client = OpenAI(
> api_key="API_KEY_OPENAI"
> )
> 
> ```

That’s not going to work. Sending the string and not your actual key.

The OpenAI python client will use an environment variable `API_KEY_OPENAI` automatically - **when you set it in the OS or the environment**.

It was just two days ago I produced an AI response telling someone how to set an environment variable: [https://chatgpt.com/share/b2c99472-e3ee-4067-ab2c-f39a7e29c0ed](https://chatgpt.com/share/b2c99472-e3ee-4067-ab2c-f39a7e29c0ed)

There is no need to put anything as parameters in the client, unless you want a particular timeout or are billing to a different organization you are a member of.

---

<div class="post-metadata">

**Author:** ![QuantumCoderr](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/quantumcoderr/32/192644_2.png) [@QuantumCoderr](https://community.openai.com/u/QuantumCoderr)\
**Post date:** [May 20, 2024, 6:13pm UTC](https://community.openai.com/t/authenticationerror-error-code-401/766947/6 "2024-05-20T18:13:48Z")

</div>

What?! I simply specified API\_KEY\_OPENAI as a stub that needs to be replaced. What’s not clear here?

---

<div class="post-metadata">

**Author:** ![\_j](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/_j/32/766292_2.png) [@\_j](https://community.openai.com/u/_j)\
**Post date:** [May 20, 2024, 6:39pm UTC](https://community.openai.com/t/authenticationerror-error-code-401/766947/7 "2024-05-20T18:39:12Z")

</div>

> [@QuantumCoderr](#):
>
> `)`

It would be clear if it didn’t look like an environment variable.

`client = OpenAI(api_key="sk-proj-use your actual API key string here")`

However, hard-coded keys are a path to leaked keys.

---

<div class="post-metadata">

**Author:** ![StefanK](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/stefank/32/374790_2.png) [@StefanK](https://community.openai.com/u/StefanK)\
**Post date:** [May 21, 2024, 6:45pm UTC](https://community.openai.com/t/authenticationerror-error-code-401/766947/8 "2024-05-21T18:45:57Z")

</div>

I got it to work today. I created yet another key and it worked. Honestly, I have no idea what went wrong.

Thanks for the support!

---

<div class="post-metadata">

**Author:** ![StefanK](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/stefank/32/374790_2.png) [@StefanK](https://community.openai.com/u/StefanK)\
**Post date:** [May 21, 2024, 6:47pm UTC](https://community.openai.com/t/authenticationerror-error-code-401/766947/9 "2024-05-21T18:47:44Z")

</div>

I tried both variations anyhow yesterday, neither would work, but it did today. While not good practice, I don’t mind hard-coding the key here. Once I am done with the task, I will delete the key and this project isn’t going to be committed anywhere either.

Thanks for the reminder!

---

<div class="post-metadata">

**Author:** ![austinye6](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/austinye6/32/512396_2.png) [@austinye6](https://community.openai.com/u/austinye6)\
**Post date:** [March 7, 2025, 3:25pm UTC](https://community.openai.com/t/authenticationerror-error-code-401/766947/10 "2025-03-07T15:25:45Z")

</div>

I’ve encountered the same problem. And using a new API-key worked.

---

<div class="post-metadata">

**Author:** ![akbari.adrian](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@akbari.adrian](https://community.openai.com/u/akbari.adrian)\
**Post date:** [October 29, 2025, 2:49pm UTC](https://community.openai.com/t/authenticationerror-error-code-401/766947/11 "2025-10-29T14:49:41Z")

</div>

you need to set your env variable without the ““  
set OPENAI\_API\_KEY=sk-XXX
