401 vs 403: Unauthorized vs Forbidden, and which one to return
401 means "we don't know who you are", 403 means "we know, and the answer is no". The difference in one table, real examples, the mistakes frameworks make, and how to debug each.
· 5 min read · By the Spot Downtime team
Both codes mean “you can't have this”, and their names make it worse: 401 Unauthorized is really about authentication, while 403 Forbidden is about authorization. Here's the one-line version:
The short answer
403: “I know who you are (or it doesn't matter), and you're not allowed. Trying again won't help.”
The difference at a glance
| 401 Unauthorized | 403 Forbidden | |
|---|---|---|
| Question it answers | Who are you? | Are you allowed? |
| Credentials | Missing, expired or invalid | Valid, or irrelevant |
| Will retrying with other credentials help? | Yes, that's the point | Usually not, unless it's a different account |
| Required header | WWW-Authenticate, saying how to authenticate | None |
| Typical causes | No token, expired token, wrong API key | Missing role or scope, IP block, file permissions, WAF rule |
When a server should return 401
The request didn't prove who sent it:
- No
Authorizationheader or session cookie at all. - An access token that has expired, been revoked or was signed with a key the server no longer trusts.
- A wrong password for HTTP Basic auth, or an API key that was rotated.
- A malformed header, like
Authorization: abc123where the server expectsBearer abc123.
HTTP requires a 401 response to include a WWW-Authenticate header that names the scheme the client should use. It's also the best clue when debugging, because many servers explain the failure there:
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="api", error="invalid_token", error_description="The access token expired"When a server should return 403
The server knows who you are, or doesn't need to know, and refuses anyway:
- A logged-in user who isn't an admin opens an admin page.
- An OAuth token without the scope the endpoint needs, such as a read-only token used to delete.
- A request from a blocked country or IP range, or one that a WAF rule or bot protection flagged.
- A web server that can't read the file because of file permissions, or a directory with listing turned off.
Logging in again doesn't change any of these, which is exactly what 403 tells the client: don't prompt for credentials, show an “access denied” message.
And when 404 is better than 403
A 403 confirms that the thing exists. For private resources, like another customer's invoice at /invoices/1234, that alone can leak information. Many APIs (GitHub's among them) answer 404 Not Found instead, so outsiders can't probe for valid IDs. HTTP explicitly allows this.
The decision in code
Authentication comes first, authorization second, and each has its own status:
function requireRole(role) {
return (req, res, next) => {
if (!req.user) {
// Not authenticated: tell the client how to authenticate
res.set("WWW-Authenticate", 'Bearer realm="api"');
return res.status(401).json({ error: "authentication_required" });
}
if (!req.user.roles.includes(role)) {
// Authenticated, but not allowed
return res.status(403).json({ error: "insufficient_permissions" });
}
next();
};
}Where 401 and 403 get mixed up
Plenty of real servers don't follow the textbook, so the code alone doesn't always tell you which problem you have:
- Django REST Framework returns 403 for unauthenticated requests when the first authentication class can't produce a
WWW-Authenticateheader, which is the case for session authentication. - Amazon S3 returns 403
AccessDeniedfor an object that doesn't exist when you lack permission to list the bucket, instead of 404. - CDNs and firewalls such as Cloudflare answer 403 before the request reaches your app, so the response isn't from your code at all.
- Single-page apps sometimes treat any 401 as “log out and redirect to login”. If your API returns 401 for a permissions problem, users get logged out every time they click a button they're not allowed to use.
Debugging a 401
- Read the
WWW-Authenticateheader and the response body; they often say exactly what's wrong. - Check the token's expiry. For a JWT, decode the payload and compare
expwith the current time, allowing for clock skew. - Make sure the header survives the trip. Some proxies and redirects drop
Authorization, and curl doesn't forward it to a different host when following a redirect. - Confirm the key belongs to this environment: a staging key against production is a classic 401.
curl -i https://api.example.com/v1/me -H "Authorization: Bearer $TOKEN"More causes and fixes in our full guide to 401 Unauthorized.
Debugging a 403
- Find out who answered. A Cloudflare or other CDN error page, a
serverheader likecloudflare, or a ray ID means the edge blocked it, not your app. - Check the user's roles and the token's scopes against what the endpoint requires.
- For static files, check permissions: the web server's user needs read access to the file and execute access to every directory above it.
- Try from another network. If it works there, an IP or geo rule is blocking you.
Our guide to 403 Forbidden walks through finding which layer is blocking a request.
Watch for them in production
A sudden wave of 401s usually means an expired certificate or signing key, a rotated secret that didn't reach every service, or a broken login provider. A wave of 403s often means a new firewall or bot-protection rule is blocking real customers. Neither takes your site “down”, so a homepage check won't notice.
An API monitor that calls an authenticated endpoint with its own test credentials and expects a 200 catches both. Spot Downtime's API checks let you add request headers such as Authorization and set the expected status code. You can also start for free and look up any code in the HTTP status code reference.
Get new guides by email
Practical posts on monitoring, outages, error fixes and on-call, when they're published. No spam.
Keep reading
- HTTP errors · APIs428 Precondition Required: why the API wants If-Match and how to send itA 428 means the server refuses unconditional writes and requires headers like If-Match. How to read the ETag, send the condition and handle 412 in your client.November 15, 2026 · 6 min read
- HTTP errors · APIs412 Precondition Failed: If-Match, ETags and lost updatesA 412 means a conditional header such as If-Match or If-Unmodified-Since didn't hold. Why it happens, how to fix it in clients and servers, and how it differs from 409.November 14, 2026 · 7 min read