What does OpenAI 'Incorrect API key provided' mean?
OpenAI didn't accept the key in the Authorization header (401, code invalid_api_key). It's a typo, a revoked or deleted key, a key from another project, or an empty env var. Generate a new key at platform.openai.com/api-keys, set it in a server-side env var, and redeploy. If the key was ever in frontend code, treat it as leaked.
Last updated 2026-11-25 · Reviewed by ProofForge
The exact error
{"error":{"message":"Incorrect API key provided: sk-proj-****abcd. You can find your API key at https://platform.openai.com/account/api-keys.","type":"invalid_request_error","param":null,"code":"invalid_api_key"}}Why it happens
- The env var is missing in production, so the SDK sends
undefinedor an old value. - Extra spaces, quotes or a newline copied into the value.
- The key was revoked: by you, by a teammate, or by OpenAI after it was detected as leaked.
How to fix it
- Create a new key at platform.openai.com/api-keys, scoped to the right project.
- Store it only on the server (
OPENAI_API_KEY) and call OpenAI from your backend or an edge function. - Redeploy, and delete the old key if there's any chance it was exposed.
// server route only
import OpenAI from 'openai'
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY })
// never: VITE_OPENAI_API_KEY / NEXT_PUBLIC_OPENAI_API_KEYCheck it's fixed
Make one request from the server. Then search your built JS (dist/ or .next/static) for sk- and confirm it's not there.
Check your live app
A free, read-only public-surface scan. It cannot diagnose private sessions or every cause described above.
FAQ
Why did a key that worked yesterday stop?
It may have been revoked. OpenAI disables keys it finds leaked and emails the owner. Check the API keys page and your inbox.
Is it OK to put the key in a VITE_ or NEXT_PUBLIC_ variable?
No. Those are bundled into public JavaScript that anyone can read.
Does clearing browser cache help?
Only when a stale key is cached in a client-side app, which itself means the key was exposed.