Skip to main content
HTTP/4xx (Client Error)

401 Unauthorized

The client must authenticate itself to get the requested response. Similar to 403, but specific for missing or invalid credentials.

❓ Why does this happen?

Missing Authorization header, expired JWT token, or invalid API key.

✅ How to fix it

Log in or refresh authentication tokens before retrying the request with valid credentials.

Return or handle 401 in your stack

// app/api/resource/route.ts
export function GET() {
  return Response.json(
    { error: "Unauthorized", status: 401 },
    {
      status: 401,
      headers: { "WWW-Authenticate": "Bearer realm=\"api\"" }
    }
  );
}

Live 401 mock endpoint

Point your app, tests, or retry logic at this free public URL to reproduce a real 401 response (CORS enabled, optional ?delay=ms up to 5000).

https://castov.com/api/mock/401

401 vs similar status codes

Frequently Asked Questions

What does HTTP Status 401 mean?
The client must authenticate itself to get the requested response. Similar to 403, but specific for missing or invalid credentials.
Why am I getting a 401 status code?
Missing Authorization header, expired JWT token, or invalid API key.
How do I fix a 401 Unauthorized error?
Log in or refresh authentication tokens before retrying the request with valid credentials.
What is the difference between 401 Unauthorized and 403 Forbidden?
401 means the server does not know who you are (missing or invalid credentials) — authenticate and retry. 403 means it knows who you are but you are not allowed — retrying with the same credentials will never work.
What is the difference between 401 Unauthorized and 407 Proxy Authentication Required?
407 asks for credentials for an intermediate proxy (Proxy-Authorization); 401 asks for credentials for the origin server (Authorization).
How can I test how my app handles a 401 response?
Send a request to https://castov.com/api/mock/401 — it returns a real 401 Unauthorized response with CORS enabled. Add ?delay=2000 to simulate a slow server.

Other 4xx status codes

Browse the full HTTP status codes directory or troubleshoot terminal errors in the Error Fix Encyclopedia.