Software

401 Unauthorized: Causes and Fixes With API Examples

401 Unauthorized means the server could not authenticate the request: credentials missing, wrong or expired. Test API responses, and 401 vs. 403.

401 Unauthorized means the request lacks valid authentication: no credentials were sent, or the ones sent are wrong or expired. Despite the name, it is about authentication (who you are). A server returning 401 must include a WWW-Authenticate header saying how to authenticate. If you are authenticated but not allowed, the correct code is 403 Forbidden.

Four responses from a test API

We called an endpoint that needs a Bearer key with the reports:read scope. Only the relevant lines are shown:

RequestStatusWWW-Authenticate
no Authorization header401Bearer realm="api"
expired key401Bearer realm="api", error="invalid_token", error_description="key expired"
mistyped key401Bearer realm="api", error="invalid_token"
valid key, missing scope403Bearer realm="api", error="insufficient_scope", scope="reports:read"

The header is the most useful clue. With no error attribute, the server is saying “send credentials”. invalid_token means the credentials were rejected, and error_description (when the server provides it) tells you why. insufficient_scope comes with a 403 because the key itself was fine.

Find the cause

DiagnoseWork through a 401

Does the request that reaches the server carry credentials?

Check with curl -v, the browser's network tab or a request log. Redirects and proxies can strip Authorization.

  • No →

    Send the credentials.

    Add the Authorization header (e.g. Authorization: Bearer KEY). If your client followed a redirect, it probably dropped the header; call the final URL directly.

    curl -H 'Authorization: Bearer YOUR_KEY' https://api.example.test/endpoint
  • Yes →

    What does WWW-Authenticate say?

    • error="invalid_token" →

      Did the credential work before?

      • Yes, it stopped working →

        Refresh or replace the credential.

        Tokens expire and keys get rotated. Use the refresh token, or create a new key and update your secret store. Check the server clock too: large clock skew makes valid tokens look expired.

      • No, it never worked →

        Check the credential itself.

        Copy the key again without spaces or quotes, use the test key against the test environment and the live key against production, and make sure it belongs to the right account.

    • Just a scheme (Basic / Bearer realm=…) →

      Use the scheme the server asks for.

      A server that answers 'Basic' wants a username and password, not a Bearer token; one that answers 'Bearer' wants a token. Match the scheme and header name exactly.

    • No WWW-Authenticate header →

      Something in front of the app answered.

      A 401 without WWW-Authenticate often comes from a gateway, proxy or framework middleware rather than the API itself. Check gateway auth settings and whether the request reached your application logs.

401 vs 403 vs 407

CodeMeaningTypical fix
401 Unauthorizednot authenticatedsend valid credentials
403 Forbiddenauthenticated (or not needed), but not alloweddifferent account, role or scope
407 Proxy Authentication Requireda proxy wants credentialsauthenticate to the proxy (Proxy-Authorization)

Some sites return 404 instead of 403 to avoid revealing that a resource exists.

401 in the browser

A 401 with WWW-Authenticate: Basic makes browsers show their own username/password dialog. A login page that redirects to a form does not use 401 at all; it uses cookies and 302 redirects, and an expired session usually sends you back to the form.

For API developers

  • Return 401 with WWW-Authenticate when credentials are missing or invalid, and 403 when they are valid but insufficient.
  • Use RFC 6750’s error values (invalid_request, invalid_token, insufficient_scope) so clients can react programmatically.
  • Do not say whether the user or the password was wrong; a generic invalid_token avoids helping attackers enumerate accounts.

How API keys work, and how to store them, is covered in what an API key is. If the server rejects the request format rather than the credentials, see the 400 Bad Request guide.

Sources

  1. RFC 9110: HTTP Semantics, 401 and 403 — IETF
  2. RFC 6750: Bearer Token Usage — IETF
  3. 401 Unauthorized — MDN

How we made this

Reproduced in a local lab. Responses were produced by a Node.js test API on localhost and fetched with curl using dummy Bearer keys (missing, expired, mistyped, valid without scope). Header and body lines are quoted verbatim.

Performed by Claude, an AI assistant working in the BinConvert repository (macOS, Node.js 25.8.2, curl 8.7.1), November 16, 2026.

This article was drafted with AI assistance. Every example, calculation and command output on this page was re-run or checked before release; each response was reproduced again and compared. Review: automated test suite and source check, November 16, 2026.

Spotted an error? Tell us and we will correct it.

Copied to clipboard!