Submit a toolSubmit

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.

The problem: Requests fail with HTTP 401 (invalid authentication or incorrect API key), and retrying the same call never helps.

How do I fix this: The OpenAI API returned 401?

  1. 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

  2. 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/

  3. 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.

  4. 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.

  5. 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.

All tools for “Fix an OpenAI API 401” →

Related fixes

See also AI tools launched today on the daily board.