Skip to content

Security basics

OpenAI "Incorrect API key provided" when the key worked yesterday

OpenAI "Incorrect API key provided" means the key that arrived is one it will not accept. Read the key in the message, then check if OpenAI disabled it.

Vlad Tkachenko10 min read
A key card held to a door reader whose lamp shows red, and a faded copy of the same card inside a browser window behind it.

In short

  • OpenAI "Incorrect API key provided" is a 401: the key that came with the request is one OpenAI will not accept. It was mistyped, deleted or disabled, belongs to another organization, or never arrived at all.
  • Read the key printed in the message. The word undefined means your app sent no key, because a setting is empty where the app runs. A masked key means a key arrived and OpenAI refused it.
  • If nobody on your side deleted the key, OpenAI may have. It disables keys it detects on the public internet, and a key in the code your site sends to the browser is on the public internet.
  • Move the call to your server before you create the new key. A new key pasted where the old one was found can be found the same way.

Your app has an AI feature: a chat box, a summary button, something you built with Lovable, Bolt, v0, Cursor or Claude Code that calls OpenAI with your own key. It worked yesterday. Today it answers nothing, and the failed request in your browser's developer tools, or the line in your server log, says this:

{
  "error": {
    "message": "Incorrect API key provided: sk-proj-…a1b2. You can find your API key at https://platform.openai.com/account/api-keys.",
    "type": "invalid_request_error",
    "param": null,
    "code": "invalid_api_key"
  }
}

OpenAI "Incorrect API key provided" means the key that came with your request is one OpenAI will not accept. It arrives as HTTP 401, the status for a request that could not prove who sent it. The key may carry a typo, it may have been deleted or disabled, it may belong to a different organization, or your app may have sent no key at all.

The answer you will find first is to make a new key and paste it in. That brings the feature back. But if nobody on your side deleted the old key, OpenAI may have, because it disables keys it finds on the public internet. Find out where the old key was before you put a new one in the same place.

What does "Incorrect API key provided" mean?

It means OpenAI checked the key your request carried and did not find a working key under it. Every call to OpenAI's API carries a key in a header that reads Authorization: Bearer sk-proj-…, and that key is the whole of the check.

Think of a hotel key card. The reader on the door does not know your face. It reads the card, asks the front desk whether that card opens this room, and lights green or red. The red light has several causes, and the reader shows the same red for all of them: a card from another hotel, a card the desk has wiped, an empty slot, a card the desk cancelled after somebody found it lying in the street.

OpenAI's error codes guide lists the causes it means by this message:

Cause in OpenAI's guideWhat it usually looks like in your app
A typo or an extra space in the keyA key pasted with a trailing space, a line break or quote marks around it
A key that belongs to another organization or projectTwo OpenAI accounts, a personal one and one from work, and the key from the other
A key that has been deleted or deactivatedSomebody removed it in the dashboard, or OpenAI disabled it
An old, revoked key cached locallyA browser or a build still holding the value from before you changed it

Three neighbouring 401s read differently and have fixes of their own. "Invalid Authentication" covers a revoked key, a key that does not match the organization or project the request names, and a key without permission for the endpoint you called. "IP not authorized" means the project has an IP allowlist and your server is not on it. "You must be a member of an organization to use the API" means the account behind the key has left its organization, or the organization was deleted.

Read the key in the message first

The message prints the key OpenAI received, masked except for its first and last few characters. That one value tells you which half of the problem you are in.

  • It says undefined, or null, or nothing at all. Your app sent no key. The variable meant to hold it was empty where the app ran, and JavaScript turned the empty value into a word and sent that. OpenAI played no part in this one.
  • It shows a key whose last characters you recognise. A key arrived and OpenAI refused it. Open the API keys page of your OpenAI project, which shows each key shortened the same way, and look for one that ends in the same characters. If it is there, check which project and organization it belongs to. If it is gone, it was deleted or disabled.
  • It shows a key you do not recognise. Your app is reading another value than the one you think: an old key in a settings file, a variable with a similar name, a value the browser kept.

In the hotel, this is the reader printing the number of the card it has read. A blank number means the slot was empty.

The message prints what arrived. undefined means no key left your app. A masked key means one did, and OpenAI turned it away.

Why "undefined" turns up after you deploy

Because the key lives in a .env file on your own machine, and the host your app runs on has settings of its own, which start empty. People have pasted Incorrect API key provided: undefined into OpenAI's forum since January 2023, and a bug report from that March shows it on an app deployed to Netlify and to Docker.

So you add the key to the host's environment variables, and the name you give it decides who can read it:

  • A name with no public prefix, such as OPENAI_API_KEY, read by code that runs on the server: a Next.js route handler, a serverless function under /api, a Supabase Edge Function. The key stays on the server.
  • A name that starts with VITE_ or NEXT_PUBLIC_. Those prefixes tell the build to write the value into the JavaScript every visitor downloads. Vite is the build tool behind most Lovable and Bolt apps, so a quick fix there tends to reach for VITE_OPENAI_API_KEY, and the error goes away.

After the next deploy both names work. The second one has also published your key. What the two prefixes do to a value has the details.

Then deploy again. Vercel's environment variables page says a change to a variable is not applied to previous deployments, only to new ones, so a key added to the dashboard reaches nothing until the next build.

Why did a working OpenAI key stop working?

Because it was deleted, disabled or expired, and you are not the only one who can switch a key off. OpenAI's help centre, in Keeping your OpenAI account secure, says that when OpenAI detects an API key on the public internet, or leaked inside an app in an app store, it disables that key immediately.

Owners have quoted the email that follows on OpenAI's forum: "We have determined that your OpenAI API key … was leaked, and have disabled it with immediate effect." It names the key, and adds that this "may be because you committed your API key to an online service such as GitHub". Search your inbox and your spam folder for that sentence before anything else.

GitHub is one way OpenAI hears about a key. OpenAI takes part in GitHub's secret scanning partner program: GitHub scans public repositories and public npm packages for the key formats its partners register, sends what it finds to the provider, and the provider decides whether to revoke the key.

Your website is the other place a key sits in public. A key in the JavaScript your site sends to the browser can be read by anyone who loads the page. OpenAI's help centre does not say where it looks. Whether or not it found your key there, a copy in your page is one any visitor could have taken.

In the hotel, this is the front desk cancelling a card that a guest handed in from the street, and leaving a note for you at reception. The email is the note.

Is your OpenAI key in your app's browser code?

Check from the failing request. Press F12, open the Network tab, use the AI feature, and click the request that failed. If it went from your browser straight to api.openai.com, the key went with it, in a header that anyone who opens the same tab can read. If it went to your own server or to a Supabase Edge Function, the key is on the server.

The code gives a second sign. OpenAI's JavaScript library refuses to start in a browser unless an option called dangerouslyAllowBrowser is set to true, so finding that option in your project means the call is made from the page. Why that option is named the way it is, and what an OpenAI key in your frontend can cost you.

You can also check from outside. Our free scan opens your app in a real browser, reads the JavaScript it loads, and reports an OpenAI project key, the kind that starts sk-proj-, if one is in there. It takes about 20 seconds and needs no account: scan your app.

How do you fix "Incorrect API key provided"?

Fix where the key lives, then replace it. In that order, because a new key pasted into the same browser code is published the same way the old one was, and OpenAI can disable it the same way.

A new key in the same place can be found the same way. With the key on a server, the page has nothing to give away.
  1. Read the key in the message. undefined sends you to the host's settings, as above. A masked key sends you on to step 2.
  2. Search your email for OpenAI's notice that the key was leaked and disabled. If it is there, you know the cause, and steps 4 and 6 are not optional.
  3. Open the API keys page of your OpenAI project and find the key by its last characters. If it is gone, it was deleted or disabled. If it is there, look at its project and organization, and at any IP allowlist on the project.
  4. If the call is made from the browser, move it behind an endpoint you own before you make the new key. Moving the key to an Edge Function shows the Supabase version, and who can call it afterwards.
  5. Create the new key in the right project, put it in the server's settings under a name with no public prefix, and deploy again.
  6. Delete the old key if it still exists, then read your usage for the days it was live. OpenAI's usage page can group spend by key, and the post on the quota error shows how to spot the spend you did not make.

If none of that finds it, OpenAI's guide also suggests clearing your browser's cache and cookies, for an old key that was cached locally, and checking that the request header carries the key you expect.

Keeping the next key out of your app's code

Check what your site sends from outside, and keep checking, because a builder that rewrites your AI feature can move the call back into the browser and the key with it.

Reeve Monitor keeps checking from outside:

  • the full security check every hour, including whether a paid-API key such as OpenAI's has turned up in your app's code
  • whether your app answers, every 60 seconds, and up to 10 more pages you choose
  • an alert by email, Telegram, Slack or webhook when the app goes down or a page breaks
  • a monthly report on your app, written for you

See what Monitor checks

The free scan shows what a visitor can read today: whether an OpenAI, Anthropic or Stripe secret key is in the code your site sends, along with eight other checks. About 20 seconds, and no account.

Scan your app free in 20 seconds

What to do now

What to do

  • Read the key printed in the message. undefined is an empty setting where your app runs; a masked key is a key OpenAI refused.
  • Search your inbox for OpenAI's email saying the key was leaked and disabled.
  • If the request goes from the browser to api.openai.com, move the call to your server before you make a new key.
  • Give the key a name with no VITE_ or NEXT_PUBLIC_ prefix, and deploy again after you add it.
  • Delete the old key if it still exists, and read your usage by key for the days it was live.

If the key was in your browser code, the post on an OpenAI key in your frontend covers why no OpenAI setting makes it safe there, and where the call should live instead.

FAQ

Why does OpenAI say "Incorrect API key provided" when I copied the key correctly?

Because the key that reached OpenAI is not always the one you copied. A space or a line break picked up on the way in, a setting that is empty where the app runs, or an old value the browser kept can all send something else. The message prints what arrived, mostly masked, so compare its last characters with the keys in your OpenAI dashboard.

Does OpenAI disable API keys that leak?

Yes. OpenAI's help centre says that when it detects an API key on the public internet, or leaked inside an app in an app store, it disables the key immediately. Owners have quoted the email that follows, which says the key was leaked and has been disabled with immediate effect. The next request with that key gets "Incorrect API key provided".

What does "Incorrect API key provided: undefined" mean?

Your app sent the word undefined where the key should be, because the variable meant to hold the key had no value. It usually works on your own machine and fails after you deploy, since the host has no copy of your local settings. Add the key to the host's server-side settings, keep it out of any name starting VITE_ or NEXT_PUBLIC_, and redeploy.

Will a new API key fix "Incorrect API key provided"?

It fixes the request for as long as the new key stays private. If the old key stopped because OpenAI found it published, a new key put back into the same code is published too. Move the call to your server first, then create the key, then delete the old one if it still exists.

Is "Incorrect API key provided" the same as "You exceeded your current quota"?

No. "Incorrect API key provided" is a 401, which means OpenAI did not accept the key. "You exceeded your current quota" is a 429, which means the key was accepted and the account behind it has nothing left to spend. The two can follow each other: a key somebody else has been using can empty the balance first and be disabled later, when OpenAI finds it.

Written by

Vlad Tkachenko

Founder, Reeve

I spend my time looking at apps built with Lovable, Bolt, v0, Cursor and Replit, and at the short list of mistakes that keep turning up in them.

More about the author

Read next

All articles

Not sure where your own app stands?

Run a free scan and get a plain-language grade from A to F in about 20 seconds. No account, no card.

Scan your app free

Automated external check, not a full audit. Absence of findings is not a guarantee of safety.