# What does OpenAI 'Incorrect API key provided' mean?

**Short answer:** 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

```text
{"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 `undefined` or 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

1. Create a new key at platform.openai.com/api-keys, scoped to the right project.
2. Store it only on the server (`OPENAI_API_KEY`) and call OpenAI from your backend or an edge function.
3. Redeploy, and delete the old key if there's any chance it was exposed.

```ts
// 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_KEY
```

## Check 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.

> **Scan box (use the site's ScanBox component):** Check whether your live app has this problem. Paste your URL for a free outside-in scan covering exposed keys, open database rules, CORS and security headers. [Scan my app free →](https://proofforge.live/#scan)

## 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.

## Sources

- [OpenAI: Error codes](https://platform.openai.com/docs/guides/error-codes)
- [OpenAI: Best practices for API key safety](https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety)

## Related

- [Fix: OpenAI key exposed](/fix/openai-key-exposed)
- [OpenAI key leaked: first 10 minutes](/leaked-keys/openai)
- [Exposed API key checker](/tools/exposed-api-key-checker)
- [Fix: secrets in VITE_ / NEXT_PUBLIC_ vars](/fix/vite-env-secrets)
- [All errors](/errors)
