Articles

Check and fix an exposed OpenAI API key in Next.js

Nov 11, 2026 · 5 min read · Next.js

To find out whether your OpenAI API key is exposed in your Next.js app, search the live site’s JavaScript and watch its network requests, which takes about two minutes. You finish with a yes or no, and if the answer is yes, a replaced key and the OpenAI call moved to your server.

On this page6 sections

Before you start

You need the address of your deployed site (not the editor preview), a browser with DevTools, and access to your OpenAI dashboard, your host’s environment variable settings and your repository. The OpenAI help pages cited here were read on 11 November 2026. The maker of the service described in the last section wrote this page.

Check whether the key reaches the browser

  1. Open your host’s environment variable settings and look for a name that starts with NEXT_PUBLIC_ and contains OPENAI, as a fix guide for keys in frontend code suggests. At build time Next.js inlines every NEXT_PUBLIC_ value into the JavaScript sent to the browser, and variables without the prefix stay on the server, according to a write-up on NEXT_PUBLIC_ leaks. A name such as NEXT_PUBLIC_OPENAI_API_KEY means the key shipped to visitors.

  2. Search your repository for api.openai.com, new OpenAI( and dangerouslyAllowBrowser. The fix guide says OpenAI’s JavaScript library refuses to run in a browser by default and that this option turns the protection off.

  3. Load your live site, then open DevTools. Choose Sources in Chrome or Debugger in Firefox, press Ctrl or Cmd + Shift + F, and search all files for sk-proj-, sk- and api.openai.com, as a guide to exposed AI keys describes. A hit on one of those, or on the first characters of your key shown in the OpenAI dashboard, means the key or a direct OpenAI call is in files every visitor can read.

  4. Switch to the Network tab and use the AI feature once. A request from the browser straight to api.openai.com carries an Authorization header of the form Bearer followed by the key, and that means the key is public. A request to your own route, such as /api/chat, is the right shape. Check every page that has an AI feature.

Replace the key and move the call to the server

If a check found your key, work through these steps in order.

  1. In the OpenAI dashboard, open the API keys page and delete the exposed key. OpenAI’s account security page says to delete the affected key, and the fix guide says a deleted key stops working at once. Deleting is what makes old copies in bundles and caches useless; removing the key from your code does not, per the guide to exposed AI keys. Expect the AI feature to stay down until step 10.

  2. Go to Settings, then Organization, then Usage, and select the project that held the key; the fix guide gives this path. Look for requests you did not make and keep the details. If you find any, open a new chat on any OpenAI Help Center page to contact Support, as the account security page describes.

  3. Create a new key and store it in an environment variable named OPENAI_API_KEY, with no NEXT_PUBLIC_ prefix. OpenAI’s API key safety page recommends that name and environment variables over keys in code.

  4. Move the call into a route handler at app/api/chat/route.ts and have the browser send a POST request to /api/chat instead of calling OpenAI. The code is adapted from the NEXT_PUBLIC_ write-up:

    TypeScript
    // app/api/chat/route.ts: runs on the server
    import "server-only";
    import OpenAI from "openai";
    
    const openai = new OpenAI({
      apiKey: process.env.OPENAI_API_KEY, // no NEXT_PUBLIC_ prefix
    });
    
    export async function POST(req: Request) {
      const { prompt } = await req.json();
      // call OpenAI here, then return Response.json(...)
    }

    The server-only import turns an accidental client import of that module into a build-time error. OpenAI’s API key safety page says requests should always go through your own backend server.

  5. Add four checks to that route: refuse signed-out users, rate-limit per user, cap input size and output tokens, and pick the model and system prompt on the server. The guide to exposed AI keys warns that moving the key server-side without the sign-in check just moves the problem.

  6. Deploy, then repeat steps 3 and 4 on the new deployment.

What OpenAI recommends for key safety

OpenAI’s help pages list a few more practices. Give each team member a key of their own, invited from the Members page, because sharing keys is against the Terms of Use. Never commit a key to your repository: private repositories are more secure, but a data breach can still leak a key. Consider a key management service in production, and IP allowlisting, which rejects requests from other addresses even when they carry a valid key.

Set a monthly spend limit with hard enforcement, and use a separate key for each feature or project so usage is easier to track. Project keys can carry an expiration date. OpenAI says it disables keys it detects on the public internet, but the guide to exposed AI keys advises against counting on that happening quickly.

If it does not work

A bundle built before the change keeps the old key until a new build replaces it. If the leaked key is missing from the API keys list, check the legacy user keys page in your account. Hard enforcement on a spend limit can block legitimate requests, and spend may slightly exceed the limit, OpenAI states. A key removed in a later commit stays readable in old commits; the NEXT_PUBLIC_ write-up names git filter-repo and BFG for purging history, though deleting the key in step 5 is what stops it working.

Having a person test your keys and secrets

Shipmate’s Launch Lockdown costs $950 once, as listed on 9 November 2026, and keys and secrets is one of the areas a person tests, among the others it covers by hand. Fixes come as pull requests, the work is re-checked after you merge, and a 15-minute call goes through the findings. Lockdown needs read access to your repository so the fixes can arrive as pull requests. Testing happens only with your written permission, nothing destructive is run on live data, and a staging copy is preferred. Shipmate’s free check costs $0, needs no card or code access, scans public pages only, and emails results with a two-day target.