I thought I’d try the openai quickstart python example just to connect to the openai API. I thought it would be a ten min project. It’s maybe 5 or 6 hours later- I don’t even know - and i cant get it working. It’s probably a simple mistake but I’m totally brunt out and can’t absorb anymore info so I’m posting here.
First of all there are credits in my account
Windows 11 computer
I created a project api Key. It a project key staring with “sk-proj-…”
I created an OPENAI_API_KEY environment variable: I verified it’s there.
I did the python quickstart example so I created a venv. (is that screwing things up?)
venv\Scripts\activate
pip install openai.
pip show openai:
Name: openai
Version: 1.64.0
Summary: The official Python library for the openai API
Author:
Author-email: OpenAI support@openai.com
License-Expression: Apache-2.0
Requires: anyio, distro, httpx, jiter, pydantic, sniffio, tqdm, typing-extensions
Required-by:
python --version: 3.13.1
I copied the python quickstart api test program just to call the openai api:
from openai import OpenAI
client = OpenAI()
completion = client.chat.completions.create(
model=“gpt-4o-mini”,
messages=[
{“role”: “system”, “content”: “You are a helpful assistant.”},
{
“role”: “user”,
“content”: “Write a haiku about recursion in programming.”
}
]
)
print(completion.choices[0].message)
I get this error:
(venv) PS C:\My_Programing\Python\todo-openai-function-test> python test_openai.py
An error occurred: 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’}}
As i said I’m spent. I’m probably making a simple mistake. I’d be grateful for any advice to get this going.
Thanks.