How to Generate and use your Private OpenAI API Key

Photo by Sarah Penney on Unsplash

Step 1: Sign up for an OpenAI account: Visit the OpenAI website (https://openai.com/) and sign up for an account if you haven’t done so already. Provide the necessary information and complete the registration process.

Step 2: Navigate to API settings: In the OpenAI dashboard, go to the API settings section. This is where you can generate your API key. (https://platform.openai.com/account/api-keys)

Step 3: Click on + Create a new secret key

Step 4: Give it any name for your reference (optional)

Step 5: Click on Create secret key

Step 6: Copy the key and save it somewhere as you will not be able to copy it again, but you can always generate a new key

Step 7: Click Done

Step 8: Create a .env file in the same folder where your program is

Step 9: Write the below line:

OPENAI_API_KEY=PASTE_YOUR_KEY

Note: DO NOT forget to replace paste_your_key with your real copied key which you did in the 6th step:

Viola! You are done with the setup, Now your API Key is secured and hidden from the public.

Now in order to call the external .env file in your program follow the below steps:

pip install python-dotenv
Note: install it via cmd line
from dotenv import load_dotenv
#dotenv helps loading the env file
import os
#os module will help access the env file
load_dotenv()
#loads env file
openai_api_key = os.getenv("OPENAI_API_KEY") #no change is required here
#access the env file

Write the above line as it is, we have already taken care of the key in the .env file, we are doing this so that your key does not get exposed publicly by mistake as it can be misused.

Now, continue to the Custom ChatGPT Blog here:

Follow our Social Accounts- Facebook/Instagram/Linkedin/Twitter

Join AImonks Youtube Channel to get interesting videos.

--

--

Shashank Kashyap
π€πˆ 𝐦𝐨𝐧𝐀𝐬.𝐒𝐨

6+ years experienced Data Analyst Lead, EY | Completed Masters in Machine Learning & AI from LJMU | Aspires to become an ML Expert| Automating Tasks with Python