After using OpenAI in numerous projects for some time, I decided to create a new apiKey for my latest project. However, upon initializing OpenAI, I encountered the following error:
Unhandled Runtime Error Error: The OPENAI_API_KEY environment variable is missing or empty; either provide it, or instantiate the OpenAI client with an apiKey option, like new OpenAI({ apiKey: 'My API Key' }).
Below is the code snippet that led to the error:
import OpenAI from 'openai';
export const openai = new OpenAI({
apiKey: process.env['OPENAI_API_KEY'], // This is the default and can be omitted
});
I have confirmed that the apiKey is present both in my .env file and within my OpenAI portal.
Any idea what's causing this issue?
Thank you in advance!