Fix guides · Cursor, Claude Code, Replit, Bolt ·
Fix 401 "invalid x-api-key" / "Incorrect API key provided"
401 Unauthorized: invalid x-api-key / Incorrect API key provided
The AI provider received your request but rejected the API key that came with it. Anthropic reports this as "invalid x-api-key" and OpenAI as "Incorrect API key provided", both with HTTP status 401. The usual fix is to make sure the right, current key reaches the code that makes the call: set the environment variable where the code actually runs, spell its name correctly, remove stray spaces or quotes, and restart or redeploy.
Common causes – and how to tell which one you have
1. Env variable not loaded where the code runs
How to tell: It works on your machine but fails after deploying, or it fails right after you edited .env. Logging whether the variable is defined (true/false, never the value) just before the call shows false or undefined.
Fix: Add the variable to your host's settings (Vercel or Netlify environment variables, Replit Secrets), then redeploy. On Vercel, changes only apply to new deployments. Locally, restart the dev server because .env is read at startup.
2. Name mismatch between code and .env
How to tell: The code reads one name, for example process.env.ANTHROPIC_API_KEY, but your .env or dashboard uses another, like CLAUDE_KEY or OPENAI_KEY. Or the key sits in .env.local and your setup only loads .env.
Fix: Use one exact name everywhere. The official SDKs read ANTHROPIC_API_KEY and OPENAI_API_KEY by default. Make sure the file your framework loads is the one that holds the key.
3. Typo, whitespace or quotes in the value
How to tell: The variable is defined but still rejected. Its length (log the length, not the value) doesn't match the key in your provider dashboard, or the value was pasted with a trailing space, newline or extra quotation marks.
Fix: Copy the key again from the provider dashboard and paste it with no spaces around it. Write it as KEY=value in .env, or with plain matching quotes. Never use smart quotes.
4. Key for the wrong provider or wrong endpoint
How to tell: An OpenAI key (starts with sk-) is sent to Anthropic, an Anthropic key (sk-ant-) to OpenAI, or an OpenRouter or other proxy key is sent to the original provider's URL. It can also happen when the code sends the key in the wrong header: Anthropic expects x-api-key, OpenAI expects Authorization: Bearer.
Fix: Match each key to its provider and base URL. The simplest way is to use the provider's official SDK, which sets the correct header for you.
5. Key revoked, deleted or rotated
How to tell: The key used to work. Or it was committed to a public repo or exposed in browser code, and it no longer appears as active in the provider's console. Providers may automatically disable keys found in public places.
Fix: Create a new key in the provider console, update it in every place it's stored (local .env, host dashboard, CI), and redeploy. Delete the old key if it leaked.
6. An old key in your shell overrides .env
How to tell: You updated .env but the error persists. Running echo $ANTHROPIC_API_KEY or echo $OPENAI_API_KEY in the terminal prints something, usually set in ~/.zshrc or ~/.bashrc. Libraries like dotenv don't overwrite variables that already exist, and Claude Code uses ANTHROPIC_API_KEY instead of your login if that variable is set.
Fix: Remove or update the export line in your shell profile, open a new terminal, and run the app or tool again.
How to fix it
- Right before the API call, log whether the key is defined and its length, for example console.log(!!process.env.ANTHROPIC_API_KEY, process.env.ANTHROPIC_API_KEY?.length). Never log the key itself.
- If it's undefined, check that the name in the code exactly matches the name in .env and in your host dashboard (ANTHROPIC_API_KEY or OPENAI_API_KEY).
- Make sure the call runs on the server (API route, server action, edge function), not in browser code. Browser code can't safely read secret env variables.
- If it's defined but rejected, copy a fresh key from the correct provider's console and paste it with no spaces, newlines or extra quotes.
- Restart your local dev server. On Vercel, Netlify or Replit, add the variable in the dashboard or Secrets and redeploy or restart.
- Run the request again and confirm a successful response, then remove the debug log.
- If the key was ever committed to git or exposed in the browser, revoke it, create a new one, and make sure .env is listed in .gitignore.
Using Lovable, Bolt or v0?
In Bolt or similar tools, tell the AI: "API calls return 401 invalid API key. Make sure the key is read from an environment variable on the server only, and tell me the exact variable name to set." Then add that exact name and a freshly copied key to wherever the app runs. That could be the project's env or secrets settings, your Netlify or Vercel project's environment variables (redeploy afterwards), or a Supabase Edge Function's secrets if the call happens there. In Replit, add it in the Secrets tool and restart the app.
Using Cursor, Claude Code or Windsurf?
Ask the agent to find every place the key is read and every place it's set (.env files, host config, shell profile), and to show you the variable name the code expects. Have it add a temporary log that prints whether the key is defined and its length, but never the key itself, then run the request again and confirm it returns 200. Afterwards, have it remove the log. In Claude Code, if you see this while using a subscription, check for a stray ANTHROPIC_API_KEY in your environment. In Cursor, check any custom API key you entered in the model settings.
The “fix” to avoid
Agents often "fix" this by hardcoding the key in the source file, or by renaming it with a NEXT_PUBLIC_ or VITE_ prefix so it's "available everywhere". Both put your secret into git history or the browser bundle, where anyone can copy it and run up your bill. Also avoid wrapping the call in a try/catch that returns fake data: the error goes quiet, but the feature is still broken.
Copy-paste fix prompt for your AI coding tool
Paste this into Cursor, Claude Code, Lovable or Bolt:
My API calls fail with "401 Unauthorized: invalid x-api-key / Incorrect API key provided". Find where the key is read in code and where it's set (.env, .env.local, hosting dashboard, shell profile). Check these in order: 1) the env variable is undefined in this runtime, or its name differs between code and config; 2) the call runs in browser code instead of on the server; 3) the value has whitespace or quotes, or is truncated; 4) a key from the wrong provider is sent to the wrong base URL or header; 5) a stale shell export overrides .env. Fix the cause, keep the key server-side only, and make sure .env is in .gitignore. Add a clear startup error if the variable is missing. Temporarily log only whether the key is defined and its length, rerun the request to confirm it succeeds, then remove the log. Tell me the exact variable name to set on my host.
Questions people ask
Why does my API key work locally but give 401 after deploying?
Your .env file isn't uploaded to the host, so the deployed app has no key or an empty one. Add the variable in your host's environment settings and redeploy. On Vercel, new values only apply to new deployments.
Does "Incorrect API key provided" mean I'm out of credits?
No. A 401 means the key itself was rejected. Running out of credits or quota shows up as a different error, such as OpenAI's 429 quota error, so check the key first.
Why does Claude Code say my API key is invalid when I'm logged in with a subscription?
If an ANTHROPIC_API_KEY environment variable is set, Claude Code uses it instead of your login. Remove or fix that variable in your shell profile, open a new terminal, and try again.