Skip to content
>_VibeUnstuck

401 Incorrect API key provided

Last verified:

The fix

OpenAI rejected the key it received. Right after a rotation, the app is usually still sending the old value, a value with extra characters, or a key from the wrong project. Anthropic's matching failure is invalid x-api-key.

  1. 1

    See which key the process sent

    OpenAI's error quotes a masked form of the key it got. Compare the visible characters with the key you just created. In your own logs, print the length and the last four characters from the same process that makes the request. Do not log the whole key. If those characters are the old key, the new value never reached this process.

  2. 2

    Put the raw key in the server env and redeploy

    The server variable is OPENAI_API_KEY or ANTHROPIC_API_KEY, with no NEXT_PUBLIC_ or VITE_ prefix and no wrapping quotes. A trailing space or newline counts as part of the key. Restart next dev or the Vite dev server after an .env change. On a host, saving the variable does not change the deployment that is already running. Redeploy, and rebuild without the old build cache when the value is inlined. A shell export from before the rotation can still be what the process sees.

  3. 3

    Match the project or workspace

    An OpenAI key belongs to the project that created it. If the request sends OpenAI-Organization or OpenAI-Project, those headers have to be that key's organization and project. A key from another project fails this 401 even when the string looks valid. An Anthropic key created for one workspace only works in that workspace. A key that is not scoped to a workspace needs the anthropic-workspace-id header. A missing header is the 400 described above, not invalid x-api-key.

  4. 4

    Create a replacement only if this value is dead

    Retrying the same 401 will not revive a revoked, deleted, or expired key. Create a new key, update the env var, redeploy, and confirm one request. Then revoke the old one. If you revoke before the new deployment is live, you get this error on purpose. A 429 about quota or a spend limit is a different page. The key was accepted.

Why it happens

OpenAI documents this 401 as Incorrect API key provided. The message starts with Incorrect API key provided: and then a masked piece of the key they received. Their docs list a typo or an extra space, a key that belongs to a different organization or project, a key that was deleted or deactivated, and an old revoked key still cached locally. A second 401, Invalid Authentication, is a revoked key, a key that does not match the organization or project on the request, or a key without permission for that endpoint.

Anthropic's equivalent is HTTP 401 with type authentication_error and message invalid x-api-key. The docs describe that 401 as an issue with the API key, for example malformed, revoked, or expired. The SDK reads ANTHROPIC_API_KEY. A different failure is HTTP 400 invalid_request_error when the key is not scoped to a workspace and the request omits anthropic-workspace-id. That message starts with This API key is not scoped to a workspace. It is not this 401.

Right after a rotation, the usual case is that the process is not sending the key you just created. The dashboard has the new value. The deployment that is live was built earlier, or a local server is still holding the old one. A wrapping quote, a trailing space, or a newline becomes part of the value. OpenAI calls that out as an extra space. A Supabase Invalid API key is a different product and a different page.

The next error you'll probably hit

Once this is fixed, the usual follow-up is Rotate a leaked API key without taking the app down. Create the new key, point the host at it, redeploy, and prove a request works. Then revoke the old key. Revoking first is how production starts failing.

Prompt to paste back into your AI tool

Paste this above the error.

The API returns 401. OpenAI body starts with: Incorrect API key provided
Or Anthropic returns 401 authentication_error with message: invalid x-api-key

The env var is <OPENAI_API_KEY or ANTHROPIC_API_KEY>. We rotated it recently.

Do not put the key in NEXT_PUBLIC_ or VITE_. Do not log the full key. Do not retry the 401 in a loop. Do not revoke the old key until a request with the new one succeeds. Do not switch to a Supabase service role key. This is not an RLS error.

1. Show where the client or SDK reads the key. If it is undefined, wrong prefixed, or still the previous value, say so.
2. Tell me to save the raw key with no quotes or surrounding whitespace, restart local dev, and redeploy the host.
3. If OpenAI headers OpenAI-Organization or OpenAI-Project are set, they must match this key's project. If Anthropic returns the 400 about anthropic-workspace-id, say that is a different error.
4. Do not rewrite unrelated files.

Common questions

+The dashboard shows the new key and the API still says incorrect. Why?

The process that made the request is still using the previous value. Redeploy the host. Restart a local dev server. For a NEXT_PUBLIC_ or VITE_ value, rebuild so the bundle is not still carrying the old string.

+I pasted the key with quotes into the host. Does that matter?

Yes, if the host stored the quote characters. OpenAI's docs name an extra space as a cause of this 401. A wrapping quote fails the same way. Save the key again without quotes or surrounding whitespace, then redeploy.

+Anthropic says authentication_error and the key looks right. What else is it?

The SDK is reading ANTHROPIC_API_KEY from the process, which may still be the old export. A 400 that starts with This API key is not scoped to a workspace is a missing anthropic-workspace-id header, not a bad key string.

Related errors