Skip to main content

HTTP · 6 common causes

401 Unauthorized

Updated

The request carried no valid authentication, so the server does not yet know who you are.

The exact message

$ curl -i https://api.example.com/v1/me
HTTP/1.1 401 Unauthorized
www-authenticate: Bearer realm="api", error="invalid_token"
content-type: application/json

{"error":"invalid_token","message":"The access token expired"}

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is 401 Unauthorized.

How to fix it

Cause 1 of 6

No credentials were sent at all.

Attach them: Authorization: Bearer <token> for OAuth and JWT, or whatever header the API documents. Check the actual request in the Network tab or with curl -v — a header added after the request object was built does not arrive, and that is invisible from the code alone.

Cause 2 of 6

The token has expired.

Refresh it. A JWT carries an exp claim you can inspect with a local decoder such as the jwt-decode package. Do not paste production tokens into an online decoder — a token is a live credential.

Cause 3 of 6

Cookies are not being sent on a cross-origin request.

fetch omits cookies cross-origin unless you set credentials: 'include', and the server must reply with Access-Control-Allow-Credentials: true and a specific origin. The cookie itself also needs SameSite=None; Secure to travel cross-site.

Cause 4 of 6

The header is malformed.

It is Bearer followed by exactly one space and the token; Basic auth needs base64 of user:password. A trailing newline picked up from a shell variable or a file is invisible and breaks it — printf rather than echo when building it in a script.

Cause 5 of 6

The endpoint expects a different credential type.

Some APIs take an API key on one prefix and a session cookie on another, or require a tenant header alongside the token. Check the documentation for that specific route rather than the general auth page.

Cause 6 of 6

The API returns 401 where it means 403.

The status is chosen by whoever wrote the API, and plenty conflate the two. If you are certain the credential is valid, treat it as an authorisation problem instead and check scopes, roles and plan limits.

Stopping it coming back

Refresh tokens before they expire rather than after a failure, and surface the response body in your client's error handling — most APIs explain the exact reason there.

401 Unauthorized — common questions

What is the real difference between 401 and 403?
401 is about identity: the server does not know who you are, so authenticating may help. 403 is about permission: it knows who you are and you still may not do this, so retrying with the same credentials will not help.
Should a 401 include a WWW-Authenticate header?
The specification says yes — it tells the client which scheme to use, and many APIs include an error code in it. It is worth reading, and worth sending if you are building the API.
Why do I get 401 on some endpoints but not others?
Public routes have no auth middleware. If a route that should be public returns 401, the middleware is mounted too broadly; if a protected route returns 200 without a token, it is mounted too narrowly, which is the more serious mistake.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor