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 code | 401 |
|---|---|
| Name | Unauthorized |
| Class | 4xx Client Error |
| Defined in | RFC 9110 §15.5.2 |
| Cacheable by default | No, unless Cache-Control says so |
| Safe to retry automatically? | No: change the request first |
| Related headers | WWW-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?
- Include a WWW-Authenticate header (for example Bearer realm="api").
- 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.
- 400 Bad Request
- 402 Payment Required
- 403 Forbidden
- 404 Not Found
- 405 Method Not Allowed
- 406 Not Acceptable
- 407 Proxy Authentication Required
- 408 Request Timeout
- 409 Conflict
- 410 Gone
- 411 Length Required
- 412 Precondition Failed
- 413 Content Too Large
- 414 URI Too Long
- 415 Unsupported Media Type
- 416 Range Not Satisfiable
- 417 Expectation Failed
- 418 I'm a teapot
- 421 Misdirected Request
- 422 Unprocessable Content
- 423 Locked
- 425 Too Early
- 426 Upgrade Required
- 428 Precondition Required
- 429 Too Many Requests
- 431 Request Header Fields Too Large
- 451 Unavailable For Legal Reasons
- 499 Client Closed Request
Frequently asked questions
401 vs 403?
Why does my API return 401 right after login?
Is 401 a client or server error?
Related
- 403 Forbidden HTTP 403 Forbidden means the server understood the request but refuses to allow it.
- 407 Proxy Authentication Required HTTP 407 Proxy Authentication Required means a proxy server between the client and the destination needs crede.
- 400 Bad Request HTTP 400 Bad Request means the server cannot process the request because of a client error, such as malformed .
- Open the HTTP Status Codes Reference → Free, no signup.