401 Unauthorized Error: Meaning and Fixes
A 401 Unauthorized error means the server requires authentication to access the requested page or resource, and the request either supplied no credentials or supplied credentials that were not valid. It is an authentication problem, meaning "who are you," which is different from a 403 Forbidden error, which means "I know who you are, but you still cannot access this."
What a 401 error means
HTTP 401 Unauthorized is a client error status code that a server sends when a resource requires authentication and the request did not include valid credentials. A properly behaving server also sends a WWW-Authenticate header describing what kind of authentication is expected, such as Basic or Bearer token authentication. The name is slightly misleading: despite the word "Unauthorized," the code is really about failed or missing authentication, not a lack of authorization for an otherwise identified user.
401 vs. other similar errors
| Code | Meaning |
|---|---|
| 401 Unauthorized | No valid credentials were provided for a resource that requires them |
| 403 Forbidden | The server recognizes the request but refuses access regardless of credentials |
| 404 Not Found | The resource does not exist at that address at all |
| 407 Proxy Authentication Required | A proxy server, not the destination server, needs credentials first |
Common causes
- A website or API endpoint protected by HTTP Basic Authentication, and no username or password was entered or saved correctly.
- An API request missing an authentication header, or sending an expired or invalid API key or access token.
- A session or login token that has expired, so the server no longer recognizes a previously logged-in user.
- Incorrect credentials saved in an application, browser, or script that is reused automatically on each request.
- Server-level password protection, such as a
.htpasswdfile on Apache, applied to a staging site or admin folder. - Clock drift between client and server breaking time-based authentication tokens.
How to fix a 401 error
- Confirm you are using the correct username and password, re-typing them rather than trusting an old saved login.
- For an API, check that the authentication header, API key or bearer token is present, current and formatted the way the API expects.
- Log out and log back in to refresh an expired session or token.
- If you manage the server, check for a
.htaccessand.htpasswdpair protecting the folder, and confirm the credentials inside.htpasswdare correct. - Check that the system clock on the client or server is accurate, since some token schemes reject requests if the times are too far apart.
- If this is a site you do not own, contact the site owner, since 401 usually means you are simply not meant to access that resource without the right credentials.
A minimal Apache basic authentication setup that triggers 401 when credentials are missing or wrong looks like this:
# .htaccess
AuthType Basic
AuthName "Restricted Area"
AuthUserFile /home/user/.htpasswd
Require valid-user
401 errors in APIs
APIs return 401 constantly during development, usually because an API key was left out of a request header, copied with extra whitespace, or because a short-lived access token expired and needs to be refreshed using a refresh token. Reading the specific error body most APIs return alongside the 401 status, rather than just the status code itself, usually points straight to the exact missing or invalid field.
Frequently asked questions
What does a 401 Unauthorized error mean?
It means the server requires valid login credentials for the page or resource you requested, and the credentials you provided were missing, wrong or expired. The server will not show the content until it can verify who you are.
What is the difference between 401 and 403 errors?
A 401 means the server does not know who you are, or your login failed, and asks you to authenticate. A 403 means the server does know who you are but has decided you still cannot access that specific resource, so logging in again will not help with a 403.
How do I fix a 401 error on a website?
Re-enter your username and password carefully, clear saved or cached credentials if the site uses a login popup, and if the issue persists after a correct login attempt, contact the site owner since the account or permissions may need to be fixed on their end.
Why does my API return a 401 error even with a valid API key?
Check that the key is placed in the exact header or parameter the API expects, that it has not expired or been revoked, and that there is no extra whitespace or formatting issue in how it was copied into the request.
Can a 401 error happen without entering a password?
Yes. Many 401 errors occur automatically, for example when a browser or app sends an expired session cookie or access token without asking the user to log in again, which the server still rejects as unauthenticated.
Does a VPN or proxy cause 401 errors?
Not directly through the destination server, but a proxy that itself requires authentication can return a related 407 Proxy Authentication Required error, which is sometimes confused with a 401 from the actual website.
Related topics
- 403 Forbidden Error: Meaning and Fixes Errors & Troubleshooting
- 404 Not Found Error: Meaning and Fixes Errors & Troubleshooting
- What Is Apache (Apache HTTP Server)? Web Servers
- What Is a WordPress Staging Site? WordPress Hosting
- 500 Internal Server Error: Causes and Fixes Errors & Troubleshooting