Error in API key

I create a ChatGPT key and paste it in the space of the code which is for the api key. However, when I run the code, even in VSCode or in the jupyter notebook from the class, it has an error which says no api key provideed. What can I do to solve that problem? How do I write that api key?

1 Like

Please see this similar thread, it might help you. Hopefully you can access the ChatGPT course discussions.

1 Like

Also, @sebasca9, just to make sure it’s clear - for this course you do not have to create your own key if you use the notebook in the course. You can just use the key that’s provided for you and is set automatically when you run this code:

import openai
import os

from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())

openai.api_key  = os.getenv('OPENAI_API_KEY')

I am trying to write on my own local computer. It’s showing api key is not provided. How can I fix it?

You’ll need to purchase your own API key from OpenAI.

I have created the api key in chatgpt and assigned as told in the course. will i have to purchase it then it will work?

Sorry, I am not a mentor for this course, so someone else will have to answer that.

Hi,
Perhaps the confusion comes from this line:

openai.api_key = os.getenv(‘OPENAI_API_KEY’)

That reads the KEY from the “environment”, typically a file in your working folder named “.env”, there is a simpler way to this →
After you have a key, either from free trial or purchased, just replace the line

openai.api_key = os.getenv(‘OPENAI_API_KEY’) with
openai.api_key = “YOUR_ OPENAI_API_KEY”

But, be aware of not to share your code, since the KEY can be copied and used by anyone how has access to it.

Hope it helps,

1 Like

I tried this but when we change openai.api_key = os.getenv(‘OPENAI_API_KEY’) with openai.api_key = “YOUR_ OPENAI_API_KEY” it gives syntax error