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/401401 vs similar status codes
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.
407 asks for credentials for an intermediate proxy (Proxy-Authorization); 401 asks for credentials for the origin server (Authorization).
Frequently Asked Questions
What does HTTP Status 401 mean?
Why am I getting a 401 status code?
How do I fix a 401 Unauthorized error?
What is the difference between 401 Unauthorized and 403 Forbidden?
What is the difference between 401 Unauthorized and 407 Proxy Authentication Required?
How can I test how my app handles a 401 response?
Other 4xx status codes
Browse the full HTTP status codes directory or troubleshoot terminal errors in the Error Fix Encyclopedia.