The client said undefined , so the key got a prefix. Then the SDK refused to run in the browser, so it got a flag:
" use client " ; const openai = new OpenAI ({ apiKey : process . env . NEXT_PUBLIC_OPENAI_API_KEY , dangerouslyAllowBrowser : true , });
Enter fullscreen mode
Exit fullscreen mode
It works. And the OpenAI API key is now in plain text in a JavaScript file every visitor downloads. (A made-up example, but a very common one.)
Prefixes publish values
Next.js inlines NEXT_PUBLIC_ values "into any JavaScript sent to the browser".
Vite: "VITE_* variables should not contain sensitive information such as API keys."
Create React App: "Do not store any secrets (such as private API keys) in your React app!"
The OpenAI and Anthropic JS SDKs block browsers by default "to avoid exposing your secret API credentials".…