401 vs 403: Difference & How to Fix Each

401 vs 403: What the Difference Actually Is, and How to Fix the One You Got

Two three-digit status codes, both meaning "no," and constantly confused — including in a fair amount of published material on this exact question. The gap between them is not about severity. A useful heuristic: a 401 is about your credentials; a 403 is a refusal that may or may not be about them.

One correction first, because you will meet the backwards version everywhere: a 403 does not come "before" a 401, and a wrong username or password is a credentials problem, which is what 401 is for. Numeric order carries no evaluation-order meaning. A server receiving a request without valid credentials returns 401. A 403 is a refusal, and as the next section shows, it does not tell you the server accepted your identity.

A 403 doesn't prove the server accepted your identity

RFC 9110 §15.5.4 says two things that are easy to read past:

If authentication credentials were provided in the request, the server considers them insufficient to grant access.

a request might be forbidden for reasons unrelated to the credentials

So a 403 does not mean "your credential is fine." It can mean the credential was insufficient, or that the refusal had nothing to do with credentials. The check to run first: did the request carry credentials at all? Look at the request as actually sent (for example with curl -v). If no Authorization header or key went out, fix that before you read anything into the 403.

The one-line distinction

  • 401 Unauthorized — RFC 9110 §15.5.2, titled "401 Unauthorized" — is about credentials: they are missing or not valid for this request.
  • 403 Forbidden — RFC 9110 §15.5.4 — means the server understood the request and refuses to fulfil it. The refusal may or may not be about your credentials.

Which header is required, whether a retry can help, and who has to go change something follow from that distinction.

Side by side

401 Unauthorized 403 Forbidden
Specification RFC 9110 §15.5.2 RFC 9110 §15.5.4
Server's state Credentials missing or not valid Refuses the request; credentials, if sent, were insufficient, or the reason is unrelated to them
Implicit question "Can you authenticate?" "Is this request allowed?"
Required response header WWW-Authenticate (MUST be sent) Not required by 403 itself (a Bearer insufficient_scope error may include one, per RFC 6750)
What the client should do Obtain credentials, resend the request SHOULD NOT automatically repeat the request with the same credentials; MAY repeat it with new or different credentials
Retrying unchanged May succeed once credentials are supplied Not expected to change the outcome

Notice the asymmetry: for a 401 the server must include the challenge. The "SHOULD NOT" in the RFC's 403 text is aimed at the client, which should not automatically repeat the request with the same credentials.

Why the distinction decides your debugging path

A 401 is an invitation. The client reads WWW-Authenticate, obtains credentials, and resends. Many HTTP libraries perform that handshake automatically, which is why a 401 often logs as a request that seemed inexplicably slow and then quietly succeeded.

A 403 is a refusal. Replaying it with the same credentials is not expected to change the outcome, and the RFC says clients SHOULD NOT do it automatically. Retry logic that treats 4xx as retryable turns a 403 into a pointless loop that burns your rate limit and delays the real error.

This is also why an expired token should return 401, not 403. A JWT whose exp has passed is an authentication failure, so a well-behaved API returns 401 with a challenge, letting the client exchange its still-valid refresh token, cache the new access token, and replay the request transparently. Return 403 instead and that refresh loop never starts — the client has no signal to act on. What Is a JWT? covers the claim layout, and Generating JWTs in Node.js walks through creating a token with a real exp.

WWW-Authenticate in practice

Here is what a correct 401 looks like on the wire:

$ curl -i https://api.example.com/v1/invoices
 
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="api", error="invalid_token", error_description="Token expired"
Content-Type: application/json
 
{"error":"unauthenticated","message":"Access token expired"}

A Basic challenge is a much simpler shape — scheme plus a realm, and optionally a charset:

WWW-Authenticate: Basic realm="api", charset="UTF-8"

Compare that with a 403 for an insufficient scope. RFC 6750 defines the Bearer error insufficient_scope: the server "SHOULD respond with the HTTP 403 (Forbidden) status code and MAY include the scope attribute". This sample omits the challenge, and a 403 that includes one is not wrong:

$ curl -i -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." \
    https://api.example.com/v1/invoices/inv_8812
 
HTTP/1.1 403 Forbidden
Content-Type: application/json
 
{"error":"insufficient_scope","message":"This token cannot delete invoices","required_scope":"invoices:write"}

Sending a 401 without WWW-Authenticate is a spec violation, and it is worth checking against any API you are debugging — What Is HTTP? covers the request and response model these headers live in. Without the challenge, the client cannot learn which scheme the server expects, so the invitation carries no instructions.

Bearer vs Basic vs API key

Four ways to present a credential to the same endpoint:

curl -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." \
     https://api.example.com/v1/invoices
 
curl -u "alice:s3cret" https://api.example.com/v1/invoices
 
curl -H "Authorization: Basic YWxpY2U6czNjcmV0" https://api.example.com/v1/invoices
 
curl -H "X-API-Key: sk_live_9f2c..." https://api.example.com/v1/invoices

The first is Bearer — an opaque access token or a JWT. The second is Basic with -u, which base64-encodes username:password for you; the third is that same header spelled out by hand. The fourth is an API key, carried in a custom header rather than in Authorization at all.

Sending a Basic header where Bearer is expected — or the reverse — is one of the most common genuine 401 causes, and it is invisible unless you read the challenge. That is a scheme mismatch, not a casing one: HTTP field names are case-insensitive, so Authorization and authorization are the same header. It is also where bearer token vs API key becomes concrete — an API key is a secret you present as proof of possession, a bearer token is one the server issued you, and neither is a session cookie.

Common causes of each

Status Typical causes
401 Authorization header missing entirely; header present but malformed; wrong scheme (Token instead of Bearer, or Basic sent to a Bearer endpoint); expired access token; revoked or logged-out session; wrong Basic username or password; credentials stripped by a proxy, CDN, or browser redirect
403 Insufficient role or scope; the resource is not owned by this principal; feature absent from the current plan or licence; geographic or IP restriction; a WAF or abuse-policy rule; correct permissions but the action itself is disallowed — deleting, exporting, or writing to a locked record

One clarification, because a 403 is often assumed to mean "wrong permissions": authorization can fail at resource level or at capability level. A resource-level 403 says this object is off limits — someone else's record. A capability-level 403 says this action is off limits — you may read the order but not cancel it. A third category is a plain state rule: "this order has already shipped," which is not a permissions problem at all. That is the argument for making 403 bodies machine-readable: anything distinguishing insufficient_scope from resource_locked has to be told which occurred.

A client-side troubleshooting procedure

Work in this order. Each step depends on the one before it.

  1. Read the status code first. If it is neither 401 nor 403, none of the rest of this applies.
  2. If it is a 401, read WWW-Authenticate and match your scheme exactly. A Basic challenge means Basic credentials; Bearer means an access token. A missing header is the server's bug.
  3. Confirm the credential was actually transmitted. This is where browsers fail silently. Authorization is not a CORS-safelisted header, so a cross-origin request carrying it triggers a preflight; if the server omits Access-Control-Allow-Headers: Authorization, the preflight fails, the browser blocks the request and reports a network error. It does not send the request without the header. Separately, a cross-origin redirect strips Authorization unless you re-add it after the hop.
  4. Decode the token and read its claims. Check exp against the current time, then aud (is this token for this API?) and iss (did the expected issuer sign it?). The JWT Decoder shows the header and every claim — it decodes and displays them, so use it to read exp, aud, and iss, not as proof that the signature is valid.
  5. If it is a 403, check whether credentials were sent at all, then read the rules. Per RFC 9110 §15.5.4, credentials that were provided were considered insufficient, or the refusal is unrelated to them. Re-read the rules on that resource: who owns it, what the role or scope allows, what state the object is in, and whether the plan or region permits the action.

The REST API Client is built for steps 2 through 4: send the request, read the status, and inspect the response headers to see the challenge the server actually returned. One caveat stated plainly — it sends from your browser, so it obeys the same CORS rules as any browser request. If the target API does not send permissive CORS headers, the browser blocks the response regardless of what you type.

To move the same request into a terminal, the cURL Converter hub produces a reproducible command with the Authorization header written out, while cURL to Python and cURL to Fetch give you runnable code. Running outside the browser is the fastest way to eliminate CORS as a variable.

When you do not control the server

If the API belongs to someone else, treat the two codes differently. A 401 is usually your problem: a missing header, a stale token, a wrong scheme, or a dropped credential can all be fixed on your side. A 403 is usually final for that request as written — repeating it without changing something (credentials, scope, resource, or action) is not expected to help, and retry loops built on 403 are the fastest way to turn a clear rejection into a 429. If you do hit rate limiting, note that 429 is the one 4xx that genuinely is retryable, and the server says how long to wait with Retry-After, expressed as a number of seconds or as an HTTP date.

Designing your own API correctly

  • Return 401 for every authentication failure — an expired token, a malformed token, an unknown key — paired with a WWW-Authenticate header that names the scheme and, where useful, an error code.
  • Return 403 for refusals about rights, scope, ownership, or resource state, and make the body say which.
  • Make 403 bodies machine-readable, with a stable error code plus a required_scope or message, so clients branch correctly without string-matching prose.
  • Keep 401 retryable and 403 terminal in your client libraries, so a token refresh happens automatically and a permission error surfaces immediately.

Frequently asked questions

Should an expired JWT return 401 or 403? 401. Expiry is an authentication failure, and the client needs the challenge in order to know it should refresh. A 403 gives no refresh signal, so the request simply dies. The JWT expired error reference covers the failure mode in more detail.

Why is my 403 not retryable? Because it is not a challenge. The RFC says a client SHOULD NOT automatically repeat the request with the same credentials, and MAY repeat it with new or different ones. If you believe the 403 is wrong, the fix is a different credential, scope, or action — not another identical attempt.

Why does the browser strip my Authorization header after a redirect? Browsers remove Authorization on a cross-origin redirect, to prevent leaking credentials to a host you did not intend to reach. Re-add it after the hop, or redesign the flow so the credential never crosses an origin boundary. A preflight omitting Access-Control-Allow-Headers: Authorization fails differently: the browser blocks the request and reports a network error, with no redirect involved.

Do I really need WWW-Authenticate on a 401? Yes — RFC 9110 requires it. It is how the client learns whether to send Basic, Bearer or something else.

Is 403 ever the right answer for a missing API key? 401 is what the spec describes for missing credentials, but some real APIs return 403 in that case, so check the API's documentation.

The takeaway

The difference is not which code is more serious. A 401 is about credentials: it says authenticate. A 403 is a refusal that may or may not be about them, so check whether credentials were sent, then read the rules on the resource.

  • REST API Client — Send a request from your browser and read back the status code and response headers, so you can see the exact WWW-Authenticate challenge the server returned.