Menu

OpenAI API key exposed in the browser: NEXT_PUBLIC_, VITE_ and dangerouslyAllowBrowser
📰

OpenAI API key exposed in the browser: NEXT_PUBLIC_, VITE_ and dangerouslyAllowBrowser

DEV Community: react·TANK JAY·about 1 month ago
#nEvk03aB
#devtoreact#devto#dev#openai#code
Long article (391 words) — no AI summary yet.
Reading 0:00
15s threshold

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".…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.