Error code 401: Access denied due to invalid subscription key or wrong API endpoint

Hello,

As part of “AI Python for Beginners” course, i wanted to test “Building LLM Prompts with Variables” in my local machine

So i downloaded helper_functions.py from coursera lab to my local machine
install the OpenAI Python library using pip.
Then i run the following code in my local Python IDLE.

#--------------------------
from helper_functions import print_llm_response
game = “soccer”
movie = “catch me if you can”
food = “pasta”
print_llm_response(f"“”
my favourite game is {game}, my favourite movie is {movie} and the food i like best is {food}.
Based on my likes, can you recommend me a song to listen.
“”")
#--------------------------

However, system gave me the following API key error and i couldn’t proceed testing.
Do i need to do any subscription for testing purpose ?
Or where shall I get this key and update it in the program ? Kindly advise. Thanks.

Python 3.13.7 (tags/v3.13.7:bcee1c3, Aug 14 2025, 14:15:11) [MSC v.1944 64 bit (AMD64)] on win32
Enter “help” below or click “Help” above for more information.

= RESTART: …\Local\Programs\Python\Python313\llm-variable.py
Traceback (most recent call last):
File “…\Local\Programs\Python\Python313\llm-variable.py”, line 12, in
print_llm_response(f"“”
File “…\Local\Programs\Python\Python313\helper_functions.py”, line 76, in print_llm_response
llm_response = get_llm_response(prompt)
File “…\Local\Programs\Python\Python313\helper_functions.py”, line 56, in get_llm_response
completion = client.chat.completions.create(
File “…\Local\Programs\Python\Python313\Lib\site-packages\openai_utils_utils.py”, line 286, in wrapper
return func(*args, **kwargs)
File “…\Local\Programs\Python\Python313\Lib\site-packages\openai\resources\chat\completions\completions.py”, line 1147, in create
return self._post(
File “…\Local\Programs\Python\Python313\Lib\site-packages\openai_base_client.py”, line 1259, in post
return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
File “…\Local\Programs\Python\Python313\Lib\site-packages\openai_base_client.py”, line 1047, in request
raise self._make_status_error_from_response(err.response) from None
openai.AuthenticationError: Error code: 401 - {‘error’: {‘code’: ‘401’, ‘message’: ‘Access denied due to invalid subscription key or wrong API endpoint. Make sure to provide a valid key for an active subscription and use a correct regional API endpoint for your resource.’}}

The DL.AI environment provides the necessary API key.

If you want to run the lab locally, you have to provide your own API key.