Open_ai_api_key issue

I have this python code

#Initialize OpenAI API (you'll need to set up your API key)
cursor_settings = get_cursor_settings('openai_api_key')  # Define cursor_settings here or retrieve it from a function

openai.api_key = cursor_settings.get('openai_api_key')

When I launch my flask application regarding a chatbot, I have this error in the terminal

PS C:\Users\royam\OneDrive\Documents\cursor-study_chatbot> py app.py
Traceback (most recent call last):
  File "C:\Users\royam\OneDrive\Documents\cursor-study_chatbot\app.py", line 8, in <module>
    cursor_settings = get_cursor_settings('openai_api_key')  # Define cursor_settings here or retrieve it from a function
                      ^^^^^^^^^^^^^^^^^^^

I have checked various support tickets regarding this issue in Cursor, I have checked the Opeanai billing feature, the subscription is Pay as you go billing, and the openai_api_key has been used in Google collab with no issue. Can you please help. I do not want to enter the openai_api_key directly in the code

Hi @Royam0820 ,

Apologies if I am misunderstanding your question, but I am not sure if you are wanting to know:

A) Where to enter your own OpenAI key in Cursor, in order to make requests in Cursor chats with your own key

B) How to use environment variables in a coding project

C) Something else

If A:
If you have a Pro Cursor subscription, then you don’t need to use your own API key, unless you want to, but then it will be at your own cost. You can add your own keys in Cursor Settings > Models.

IF B:
I’m a bit confused where your original Python code may have come from, and why it has a function named get_cursor_settings - perhaps you could explain the scenario a bit more?

IF C:
Perhaps you could explain the scenario a bit more?

If you need a bit more introductory information about Cursor, perhaps this post can assist:

Hello Litecode, sorry for the confusion, I have built via composer a simple webchat application using Flask, it is using openai_api_key so I setup that key in cursor_settings and I am using the function get_cursor_settings to get the key … however, it seems that I have issue with the cursor_settings regarding openai. I just purchased a yearly subscription for cursor, so I will try your suggestion and remove the openai_api_key in the cursor settings
I will try that

Just to clarify, there is a difference between:

  • Using an API key in your code (which you would normally save in an environment variable)

  • Using an API key within Cursor, so that it uses the API key when you are making chat requests within Cursor

I could be wrong, but it seems like you are just seeking advice about how to use an OpenAI key within your code project?

Is that correct?

If so, there is no inbuilt functionality (that I know of) that has a function named get_cursor_settings which allows you to reference an API key that is saved in Cursor.

It seems like you are wanting to learn how to use environment variables in a Python project?

Is that correct?

(i am signing off for the day, but if the issue is a Python coding issue, hopefully someone here with experience in Python can assist, Python is not my main language, good luck!).

@litecode first of all thank you for your help and support, very much appreciated. I was more likely wrongly thinking that if I setup my openai_api_key in the cursor settings, I could simply make a call to it via this instruction
openai.api_key = cursor_settings.get('openai_api_key')
just like it is possible in Google colab to do so via this instruction
os.environ['OPENAI_API_KEY'] = userdata.get('OPENAI_API_KEY')
So finally, to solve my issue I end up writing a .env file to setup the openai_api_key.

1 Like