OpenAI 401 invalid_api_key: Causes and Fixes
An HTTP 401 Unauthorized with code: "invalid_api_key" or "Incorrect API key provided" means the endpoint rejected the credentials on the request — most often because a stale environment variable shadowed your config, the Authorization: Bearer header was malformed, or the key was sent to the wrong base_url.
Quick answer
Check four things in order:
- Run
env | grep -iE 'OPENAI|API_KEY'to ensure a stale shell variable is not overriding.envor client config. - Confirm the
Authorizationheader has the exactBearerprefix with no stray whitespace or newline. - Verify the key matches the
base_url(sending a gateway key toapi.openai.com, or an OpenAI key to a gateway, always returns 401). - Remove stale
OpenAI-Organization/OpenAI-Projectheaders when switching keys or gateways.
Typical Error Response
{
"error": {
"message": "Incorrect API key provided: sk-proj-****1234. You can find your API key at https://platform.openai.com/account/api-keys.",
"type": "invalid_request_error",
"param": null,
"code": "invalid_api_key"
}
}Note that OpenAI masks the middle of the key in the error message (sk-proj-****1234). Compare those last 4 characters against the key you intended to send — if they do not match, an environment variable or IDE config is shadowing your key.
Common Causes and Fixes
1. Stale Environment Variable Shadowing .env
python-dotenv (load_dotenv()) does not overwrite existing shell variables by default. If your terminal profile (~/.zshrc, ~/.bashrc) exports an old OPENAI_API_KEY, your app keeps sending the old key even after you update .env.
# Inspect what your current shell actually exports
env | grep -iE 'OPENAI_API_KEY|OPENAI_BASE_URL|OPENAI_ORG_ID'
# Clear stale values in the current session
unset OPENAI_API_KEY OPENAI_BASE_URL OPENAI_ORG_IDIn Python, pass override=True if .env should win:
from dotenv import load_dotenv
load_dotenv(override=True)2. Key and base_url Mismatch
The OpenAI SDK defaults to https://api.openai.com/v1 whenever base_url (or OPENAI_BASE_URL) is omitted or misspelled. Sending a third-party gateway token to api.openai.com returns 401 invalid_api_key.
Verify with a minimal curl request:
curl https://byesu.com/v1/models \
-H "Authorization: Bearer $OPENAI_API_KEY"- Make sure
base_urlends with/v1for OpenAI-compatible SDKs (https://byesu.com/v1). - Strip trailing newlines when reading keys from files (
api_key = Path("key.txt").read_text().strip()).
3. Revoked, Disabled, or Org-Mismatched Keys
- If a key was deleted, rotated, or disabled in the dashboard, create a new key and update every worker/secret store.
- If
OPENAI_ORG_IDorOPENAI_PROJECT_IDis set from a different workspace than the key belongs to, unset both variables.
4. Missing Bearer Prefix in Raw HTTP Clients
Raw fetch / curl / requests calls fail with 401 when sending Authorization: sk-... instead of Authorization: Bearer sk-..., or when sending Anthropic's x-api-key header to an OpenAI endpoint.
Quick Verification
If you use byesu, confirm in Console → Tokens that the token is enabled and test it against https://byesu.com/v1/models with Authorization: Bearer <token>. For Anthropic /v1/messages auth errors, see Claude API 401 authentication_error.