401 vs 403
Unauthorized vs Forbidden
Authentication vs Authorization
Understanding the key difference
Who are you?
The server doesn't know your identity. You need to prove who you are by providing valid credentials.
- Missing authentication token
- Expired login session
- Invalid username/password
What can you do?
The server knows who you are, but you don't have permission to access this resource.
- Insufficient user role/permissions
- Resource restricted to admins
- IP address blocked
- User hasn't logged in yet
- Login credentials are missing or invalid
- Authentication token is expired
- Response should include
WWW-Authenticateheader
- User is logged in and identified
- User lacks necessary permissions/role
- Resource is restricted to specific users
- Re-authenticating won't help access the resource
401 is about identity (Who are you?), while 403 is about permissions (What can you do?). If the user could fix the issue by logging in with different credentials, use 401. If even an admin account couldn't access it, use 403.
Real-World Scenarios
When should you use each status code? Here are practical examples to help you decide.
Correct response
Don't use these
Correct response
Don't use these
Correct response
Don't use these
Correct response
Don't use these
Correct response
Don't use these
| 🔐 401 | 🚫 403 | |
|---|---|---|
| Name | Unauthorized | Forbidden |
| Category | 4xxClient Error | 4xxClient Error |
| Description | The client must authenticate itself to get the requested response. | The client does not have access rights to the content. |
| When to Use |
|
|
| Common Causes |
|
|
The client must authenticate itself to get the requested response.
- No authentication credentials provided
- Authentication token is invalid or expired
- User needs to log in to access the resource
- Missing authentication token
- Expired login session
- Invalid credentials
The client does not have access rights to the content.
- User is authenticated but lacks required permissions
- Resource is restricted regardless of authentication
- IP address or region is blocked
- Insufficient permissions
- IP blocked
- Resource restricted to certain users
Decision Tree
Does the request include authentication credentials?
Are the credentials valid?
Does the authenticated user have permission to access this resource?
Allow access (200 OK)
Status Code: 200
Use 403 Forbidden
Status Code: 403
Use 401 Unauthorized
Status Code: 401
Use 401 Unauthorized
Status Code: 401