All fixes · OpenAI / OpenAI-compatible · 401 · Auth

“Incorrect API key provided” (401 Unauthorized): How to Fix

Incorrect API key provided

The key in your Authorization header is not one the endpoint accepts: it is mistyped, revoked, empty because an environment variable did not load, or it belongs to a different provider than the base URL you are calling. Check the key value, the Authorization header, and that the key belongs to the endpoint you’re hitting.

What causes it

  • Wrong, expired, or revoked key, or a stray space / newline in the value.
  • Key from one provider used against a different provider’s base URL.
  • Environment variable not loaded, so an empty or placeholder key is sent.

How to fix it

  1. Regenerate the key and paste it cleanly (no surrounding whitespace).
  2. Confirm the Authorization header is exactly Bearer <key>.
  3. Make sure the key matches the base URL / provider you’re calling.
  4. Check your env var is actually loaded at runtime.

Running an agent? When wiring an agent to a new provider, a 401 usually means the base URL and key don’t belong to the same provider. Verify both point at the same endpoint.

FAQ

My key looks right but still 401s. Why?

Most often the key is for a different provider than the base URL you’re calling, or an env var didn’t load so an empty value is being sent. Verify the key and the endpoint match.

Related errors