In this categoryTroubleshooting ยท 27
- How to Fix an OpenAI 401 'Invalid API Key' ErrorStart
- How to Fix OpenAI 429 Rate Limit Errors With Backoff
- How to Fix Anthropic Claude API 401 Authentication Errors
- How to Fix Google Gemini 'API Key Not Valid' Errors
- How to Fix an API Key That Loads as Undefined
- How to Handle Anthropic 529 'Overloaded' Errors
- How to Fix Rate Limit Errors from an AI API
- How to Fix CORS Errors When Calling an AI API From the Browser
- How to Fix 'Model Not Found' and Deprecated Model Errors
- How to Rotate a Leaked API Key Without Downtime
- How to Fix SSL Certificate Errors When Calling AI APIs
- How to count tokens before sending a prompt to Claude
- How to Fix 'Context Length Exceeded' Token Limit Errors
- How to fix a context length exceeded error in the Claude API
- How to fix a Claude response that gets cut off mid-sentence
- How to reduce Claude hallucinations by grounding answers in your documents
- How to keep a long Claude conversation under the context limit
- How to stop Claude from calling tools when it should not
- How to handle a Claude Fable 5 refusal with a fallback model
- How to choose the right Claude model for cost and quality
- How to cut Claude API costs with prompt caching
- How to halve Claude costs for bulk jobs with the Batch API
- How to cap spend on a Claude agent with a task budget
- How to Debug an MCP Server That Will Not Connect
- How to Ask an Agent to Explain a Bug Before Fixing It
- How to Roll Back a Bad Deploy Quickly
- How to Fix Cursor Not Indexing Your Codebase
How to Fix an OpenAI 401 'Invalid API Key' Error
Track down why the OpenAI API rejects your key and get a valid request flowing again.
A 401 from the OpenAI API almost always means the key your code sent does not match a live key on your account. The fix is rarely the key itself. It is usually a stray space, the wrong environment variable, a project mismatch, or a key that was rotated out. This guide walks through the checks in the order that catches the most cases first.
- An OpenAI account with billing or free credits available
- Access to the platform.openai.com dashboard
- A terminal with curl installed
Step 1: Read the exact error body
The status code alone is not enough. Print the JSON body the API returned. The message field tells you whether the key is missing, malformed, or simply unknown to the server.
Step 2: Confirm the variable is actually set
If the message shows a blank or truncated key, your environment variable never reached the process. Echo it back, then check for hidden whitespace or quotes that crept in from a copied line.
Step 3: Match the key to the right project
Newer keys start with sk-proj- and are scoped to a single project. If the model you call lives in a different project, or your org header points elsewhere, you get a 401 even with a real key. Set the project explicitly so there is no ambiguity.
OPENAI_API_KEY=sk-proj-your-real-key
OPENAI_PROJECT_ID=proj_abc123
OPENAI_ORG_ID=org_xyz789Step 4: Rotate the key if it was ever exposed
If the key was committed to git, pasted in a chat, or printed in logs, OpenAI may auto-revoke it. Create a fresh secret key, paste it into your secret store, and delete the old one. Never paste a key back into the dashboard search box to test it.
Result
After setting a clean key in the right project, the same curl call returns a 200 with a list of models. In one common case a developer had OPENAI_API_KEY set in their shell but their app loaded a stale .env file, so the process saw an old revoked key. Removing the .env line and relying on the shell variable fixed it in under a minute.
Related guides
How to Fix OpenAI 429 Rate Limit Errors With Backoff
Stop your app from crashing on 429s by adding exponential backoff and reading the retry headers.
How to Rotate a Leaked API Key Without Downtime
Respond to a committed or exposed API key safely by rotating it and purging it from history.
Watch related tutorials
12:38
14:09
17:53
15:00
12:00
1:42:18New guides in your inbox
Fresh step-by-step how-to guides as we publish them. One email a week, no more.