The OpenAI API returned 401. Fix the key, org, and project before you retry
Requests fail with HTTP 401 (invalid authentication or incorrect API key), and retrying the same call never helps. Short answer: 5 steps. 1) Read which 401 you got; 2) Send Bearer auth from a server-side secret; 3) Prove the key works with a tiny request; 4) Mint a project key and replace every copy; 5) Check membership and IP allowlists last.
Last updated: · This page is the decision checklist; the tools list for “Fix an OpenAI API 401” is a separate URL.
How do I fix this: The OpenAI API returned 401?
Read which 401 you got
OpenAI documents several 401s: Invalid Authentication (revoked key, wrong org/project, or missing permission), Incorrect API key provided (typo, extra space, deleted key, or a cached old key), You must be a member of an organization, and IP not authorized when an allowlist blocks your client. A 403 for unsupported country/region is a different problem. Error codes: https://developers.openai.com/api/docs/guides/error-codes
Send Bearer auth from a server-side secret
Use Authorization: Bearer $OPENAI_API_KEY. Load the key from an environment variable or secret store, never from front-end code. If you belong to more than one organization or still use a legacy user key, also send OpenAI-Organization and OpenAI-Project. Overview: https://developers.openai.com/api/reference/overview/
Prove the key works with a tiny request
From the same machine and env your app uses, call GET https://api.openai.com/v1/models with that Bearer header. If this fails, the bug is the key, org, project, or IP allowlist, not your prompt code. Trim the value: a trailing newline from a .env paste is enough to trigger Incorrect API key provided.
Mint a project key and replace every copy
In the OpenAI dashboard, open the project you intend to bill, create a new API key, update the env var or secret in every runtime (local, CI, host), then revoke the old key. Revocations take effect within a few seconds; other auth-related updates can take up to about 15 minutes. Clear any local cache that might still hold the old value.
Check membership and IP allowlists last
If the message says you must be a member of an organization, ask an org owner to invite you or add you to the project. If it says IP not authorized, send the request from an allowed network or update the project/org IP allowlist. Do not treat a 401 like a rate limit: backoff will not fix authentication.
Which tools are best to fix an OpenAI API 401?
OpenAI error codes
Separates Invalid Authentication, Incorrect API key, org membership, and IP allowlist 401s.
API authentication
Bearer header, organization and project headers, and how fast revocations apply.
OpenAI API keys
Create a project key, rotate it, and revoke the old one.
Production best practices
Keep keys out of client code; prefer env vars or a secret manager.
Open: 1 spot per week
One labeled sponsor for this topic each week; this week's spot is open. See open weeks →
All tools for “Fix an OpenAI API 401” →
Related fixes
- Stop a Cursor agent loopHow to fix a Cursor agent loop (same edit, or work after Stop)Fix →
- Claude Code vs Codex vs Cursor ~$20 plansClaude Code vs Codex vs Cursor $20 plan limits: how to pick without burning the weekFix →
- Scan a vibe-coded app for leaked API keysCheck a vibe-coded app for leaked API keys before you share itFix →
- Add an MCP server to Claude Code, Cursor, or CodexHow to add an MCP server to Claude Code, Cursor, or CodexFix →
See also AI tools launched today on the daily board.
NewAIToolsToday Daily
Top 3 AI launches, one model or news item that matters, and one simple fix. A 5-minute read, every weekday.