Start free · Free tools · Fair usage limits
Browse Tools

Categories

Custom AI Solutions – AI2Flows
LIVE NEWS
Loading latest AI & SEO news…
Home›Tools›HTTP Status Codes Reference›HTTP 401 Unauthorized
4xx Client Error

HTTP 401 Unauthorized

HTTP 401 Unauthorized means the request lacks valid authentication credentials: no token, an expired token or a wrong password.

Quick answer

HTTP 401 Unauthorized means the request lacks valid authentication credentials: no token, an expired token or a wrong password. Despite the name it means “unauthenticated”. The server must send a WWW-Authenticate header describing how to authenticate.

Last updated · Maintained by FreeAIToolz · How we test

What does HTTP 401 Unauthorized mean?

Compare with 403: a 401 says “I don’t know who you are”, a 403 says “I know who you are, and you can’t do this”. Sending valid credentials can turn a 401 into a success; it cannot fix a 403.

Status code401
NameUnauthorized
Class4xx Client Error
Defined inRFC 9110 §15.5.2
Cacheable by defaultNo, unless Cache-Control says so
Safe to retry automatically?No: change the request first
Related headersWWW-Authenticate, Authorization (request)

What causes HTTP 401?

  • Missing Authorization header or session cookie.
  • Expired or revoked access token.
  • Wrong API key or password.

How do I fix HTTP 401?

  1. Include a WWW-Authenticate header (for example Bearer realm="api").
  2. On the client, refresh the access token once and retry; if it fails again, send the user to log in.

What should a client do after HTTP 401?

Authenticate (or refresh the token) and retry once. Do not loop on repeated 401s.

Does HTTP 401 affect SEO?

Content behind 401 is not indexed. Do not put pages you want ranked behind authentication.

Example 401 response

HTTP/1.1 401 Unauthorized
Date: Mon, 01 Dec 2026 12:00:00 GMT
WWW-Authenticate: Bearer realm="api", error="invalid_token"
Content-Type: application/json

{"error":{"status":401,"message":"Unauthorized"}}

How to return 401 Unauthorized in code

Express (Node.js)

res.status(401).json({ error: 'Unauthorized' });

Next.js route handler

return Response.json({ error: 'Unauthorized' }, { status: 401 });

FastAPI (Python)

raise HTTPException(status_code=401, detail="Unauthorized")

PHP

http_response_code(401);

Nginx

location = /example {
    return 401;
}

Other 4xx status codes

The request contains an error or cannot be fulfilled as sent. Fix the request before retrying.

Frequently asked questions

401 vs 403?
401 means not authenticated (no or bad credentials). 403 means authenticated but not allowed. Logging in can fix a 401; it will not fix a 403.
Why does my API return 401 right after login?
Common causes: the token is not sent in the Authorization header, it has the wrong prefix (Bearer), the server clock is off so the token looks expired, or a proxy strips the header.
Is 401 a client or server error?
401 is in the 4xx class, so it is classed as a client error: the request needs to change before it can succeed.