OpenAI API 401 Incorrect API Key Provided
Quick answer
A 401 means the key is wrong, revoked, from another organization or project, or blocked by an IP allowlist. Create a fresh key in the right project and fix your environment variable.
Most likely fix: Check the header and variable
How to fix it
Try these in order — the most common fix is first.
1. Check the header and variable
- Print the key length (not the key) at runtime
- Remove spaces and newlines
- Restart the shell or server so new env vars load
Did this fix it?2. Create a new key
- platform.openai.com > API keys > Create new secret key in the correct project
- Update your secrets store
Did this fix it?3. Check organization and project
- Confirm OpenAI-Organization and OpenAI-Project headers if you set them
Did this fix it?4. Check IP allowlists
- Settings > Security: add your server IP or remove the restriction
Did this fix it?
Symptoms
AuthenticationError: Incorrect API key provided, or Invalid Authentication.
Why it happens
- Typo or trailing whitespace
- Key deleted or revoked
- Key from a different org or project
- Stale cached environment variable
- IP not on the allowlist
- Removed from the organization
This guide also covers
- openai 401 incorrect api key
- openai invalid authentication
- openai api key not working
Still not working?
If OpenAI keeps failing, you're not alone. See every OpenAI problem users report or the alternatives people switch to.
Have an app idea? Our small team builds any software: web and mobile apps, games, desktop, AI tools. Price agreed together.
Talk to usThe same problem in other apps
All ai & editing tools not working guides →
- Gemini Image Generation Not Working (Google Gemini)
Other OpenAI errors
- OpenAI API 429 Too Many Requests (Rate Limit)
- OpenAI API Error: You Exceeded Your Current Quota (insufficient_quota)
- OpenAI API 500 and 503 Server Errors (server_is_overloaded)
- OpenAI API Request Timed Out / 504 Gateway Timeout
- OpenAI API Credits Expired or Balance Disappeared
- OpenAI "Your Organization Must Be Verified" 403 Error
- OpenAI API model_not_found: Model Does Not Exist or You Do Not Have Access
- OpenAI API context_length_exceeded: Maximum Context Length Error