401 Error? Here’s What It Means and How to Fix It
You click Download on an invoice and get a page that says 401 Unauthorized. The orders page loaded a second ago. It even had your name on it. You click the button again, but the same issue appears.
A 401 error means the server handling that request didn’t get credentials it could accept. Sometimes a fresh sign-in gets you straight back to work. If the error comes from an app or script you’re building, the quickest clue is often what went out with the request. Start with the click or API call that triggered the message.
Table of Contents
- What a 401 error means
- How to Fix a 401 Error in Your Web Browser
- How to find the request that failed
- How to fix a 401 from an API
- What to Check If You Manage the Website
- Why a 401 keeps coming back
What a 401 error means
A single page can make several requests. It might load the layout, then ask for your profile, your saved addresses, and your invoices separately. Each of those requests can succeed or fail on its own. A 401 on the invoice request tells you where the failure occurred, even if all you see on screen is a small banner saying the download failed.
The HTTP standard requires a server sending a 401 to include a response header called WWW-Authenticate. It tells the browser or API client which authentication method the server accepts. A Basic challenge can bring up the browser’s small username and password prompt. A Bearer challenge tells the client to present an access token.
A credential can be present and still fail. Say you left a work portal open while you went to lunch. Your browser might still send its session cookie when you come back, even if the portal has already expired the session that cookie refers to. You’ll see the problem on the next request that needs an active session.
How to fix a 401 in your browser
Start from the site’s home page. If it still shows you as signed in, sign out and back in through the normal sign-in page. Then open the page you wanted from the site’s menus. For a link someone shared with you, check which email address received the invitation. It’s easy to open a work file while your browser is still using your personal account.
Still stuck? Open the same page in a private window and sign in with the same account. That gives you a separate browser session. If the page works there, try clearing the cookies and site data for the affected website in your regular window. You’ll likely need to sign back in, and the site may forget preferences it saved in that browser.
If a fresh sign-in brings you back to the same error, tell support which page failed, what you clicked, and roughly when it happened. Include your time zone. “Clicking Download on invoice 42 returned 401 at 10:15 a.m. Eastern” gives the team something they can look for in their logs.
How to find the request that failed
In Chrome, open DevTools and select Network before you reload the page or reopen the link. Find 401 in the Status column and click the request. In Headers, check its URL and method, then look under Response Headers for WWW-Authenticate. Some apps omit that header. The Response tab may still give you a reason, such as “Session expired.” If the page sends you through several redirects, turn on Preserve log so you can see the earlier requests too.
You might see several failures at once. Say /api/session comes back with a 401 and then the profile and billing requests fail. Check the session request first. Those later calls may depend on it. If you can open everything except one invoice, look at the request for that invoice and see which host handled it. A download link can take you to a separate service with its own sign-in requirements.
If you’re sending someone a screenshot of the Network panel, keep the failed request’s path and status visible. Redact any cookie values or access tokens before sharing it.
How to fix a 401 from an API
Check the request your program actually sent. An API_TOKEN variable can be set in your terminal while a scheduled job runs with a different environment. Use your HTTP client’s request inspector to confirm the expected header is present and has a value. For bearer authentication, the header takes the form Authorization: Bearer YOUR_ACCESS_TOKEN. If your API client adds the prefix itself, check that it appears just once. For an API key, use the header name and format in the service’s docs.
If a protected API returns the OAuth error invalid_token, the access token may have expired, been revoked, or come through malformed. If the token expired, use the provider’s refresh flow when it’s available. Some services require you to sign in again or request a new token using your app’s credentials. If a new token fails immediately, check which service it was issued for and which account or environment it belongs to. A token made for a test API can get rejected by the live endpoint even if you created it five seconds ago.
A redirect is worth checking when an API call stops working after a change to its address. Browsers remove the Authorization header when they follow a redirect to a different origin, such as a new hostname. If that’s what happened, update the client to call the documented API URL directly.
Where to look if you run the website
Find out which part of your setup sent the 401. A gateway or reverse proxy can ask for credentials before a request reaches your app. If the response includes a request ID, search for it in both sets of logs. Otherwise, use the time, path, and HTTP method to narrow the search. If your proxy logs the status it received from the application, check that alongside the status it sent back to the browser.
Think about what changed around the first failure. If a public page starts opening a browser password prompt just after a deploy, look at the rules for that route. In NGINX, for instance, a Basic authentication rule can be inherited from a broader configuration level. A rule intended for a staging site can end up covering a live page if the configurations get mixed up.
When your app reports a missing session cookie, inspect the cookie’s Path attribute in the sign-in response. A cookie scoped to /account goes out on requests under that path. If an update moved downloads to /api/invoices, that request arrives without the cookie. Check which routes are supposed to share a session and make sure the cookie’s scope matches that setup.
Why a 401 keeps coming back
Pay attention to how long things work before they break. Imagine a reporting job that runs fine for an hour, hits a 401, then works for another hour every time you restart it. The restart may be fetching a new token. Check when that token expires and whether the job gets a replacement while it’s still running. The timestamps of the last successful call and the first failure can help you match the problem to an expiry.
If a 401 comes and goes within seconds, check which server handled each request. Imagine signing in on one server and then having your next request sent to another. If the servers keep their sessions in separate stores, the second server may have no record of your login. Look for failures that cluster on a particular server, then check how your app shares session data.
Once you’ve made a change, try the thing you were doing when the 401 interrupted you. Open the same invoice link, or let the reporting job run through its next token renewal. If you changed an access rule, check the protected page both signed in and signed out. Keep the exact link or request that failed. If the error returns after the next update, you’ll have a quick way to reproduce it.
