HTTP status code lookup

401 Unauthorized: What It Means and How to Fix It

A 401 Unauthorized response means the server needs you to authenticate and the request didn't include valid credentials. Despite the name, it's about authentication (who you are), not permission: sending a valid token or logging in can fix it.

A 401 response should include a WWW-Authenticate header that names the expected scheme, such as Bearer or Basic.

401

Unauthorized

Authentication is required and has failed or hasn't been provided. Despite the name, it means unauthenticated.

Look up any other status code →

Common causes

  • No Authorization header was sent, or it's missing the scheme: it must be Authorization: Bearer <token>, not just the token.
  • The token has expired. Decode it and compare the exp claim with the current time.
  • The token was issued for another API or environment, so its aud or iss claim doesn't match, or it was signed with a key this server doesn't trust.
  • Cookies weren't sent with a cross-origin request. fetch needs credentials: "include", and the cookie needs SameSite=None; Secure.
  • The server requires authentication on CORS preflight requests. Browsers never send credentials with OPTIONS preflights, so the server must answer them without checking auth.
  • The clocks on two servers differ, so a fresh token looks like it's not yet valid or already expired.

Debugging steps

  • In your browser's network tab, open the failing request and check that the Authorization header or the cookie is actually there.
  • Paste the token into the JWT decoder and check exp, aud and iss.
  • Repeat the request with curl and the same token. If curl works, the difference is in how the browser sends it, often CORS or cookies.
  • Read the WWW-Authenticate header in the response. With OAuth it often says why, for example error="invalid_token".

Refresh an expired token and retry once

async function apiFetch(url, options = {}) {
  const send = () => fetch(url, { ...options, headers: { ...options.headers, Authorization: `Bearer ${getAccessToken()}` } });
  let response = await send();
  if (response.status === 401) {
    await refreshAccessToken(); // your auth library's refresh call
    response = await send();
  }
  return response;
}

Frequently asked questions

What is the difference between 401 and 403?

401 means the server doesn't know who you are, or your credentials are invalid. 403 means it knows who you are and you aren't allowed. Logging in can fix a 401 but not a 403.

Why do I get 401 only in the browser?

Usually because of CORS or cookies: the browser didn't send the credentials, or the preflight OPTIONS request was rejected because it had no auth header.

Should an API return 401 or 404 for a missing token on a private resource?

Return 401 for a missing or invalid token. Use 404 only when you want to hide whether a resource exists from users who are logged in but not allowed to see it.

More http status code lookup guides

Open the full http status code lookup →

From the blog