Unable to access API in VSCode

Hi, I have looked for search other questions and tried way to debug before I have to post up here.

TL; DR, I was working on a project using OpenAI Response API and stored the key in .env file. I tried many times with just the key, key with project ID, key with org ID. None of it works.

I also noticed that API key can only created if it is associated with a given project. Is it not possible to create account / org level API key for general use not specific to project scope? Thanks

Codes below is created for my API testing:

Error msg: openai.AuthenticationError: Error code: 401 - {‘error’: {‘message’: ‘Incorrect API key provided: [api-key] You can find your API key at https://platform.openai.com/account/api-keys.’, ‘type’: ‘invalid_request_error’, ‘param’: None, ‘code’: ‘invalid_api_key’}}

from openai import OpenAI

client = OpenAI(api_key=“sk-…”)

response = client.chat.completions.create(
model=“gpt-4o”,
messages=[
{“role”: “user”, “content”: “Tell me a joke”}
]
)

print(response.choices[0].message.content)

do this - swap the key to your env and it should be gucci bro

The whole point of stashing keys in environment variables is not to hardcode/expose the key in the first place…

I mean you can place the key in several protective layers this is just the sdk format …

I even say it at the bottom. Hope this helps

Thanks! Fixed. i used client = OpenAI(api_key=os.getenv(some alias)) instead

also just noticed other typos in the last line resposne.output…