Home / Blog / API & webhooks

API & webhooks

Fix a REST API 401: Inspect Authentication Before Retrying

Diagnose a REST API 401 response by checking the authentication challenge, token destination, request headers, and browser access before retrying.

Start with the actual authentication response

A 401 response means the request lacks valid authentication credentials for the requested resource. Begin by inspecting the response status and authentication challenge, then compare the outgoing request with the API's documented authentication method. Repeating the same request with the same invalid credentials usually reproduces the same failure.

The HTTP specification requires a 401 response to include an applicable WWW-Authenticate challenge. An API may also supply a structured error body with its own code. Preserve that diagnostic information while keeping passwords, tokens and full Authorization values out of shared screenshots or logs.

Read a small Bearer-authentication example

GET /profile HTTP/1.1
Host: api.example.com
Accept: application/json

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="example"
Content-Type: application/json

{"error":"authentication_required"}

This fictional request contains no Authorization header, and the response identifies Bearer authentication. A suitable authorized test would use the actual access token issued for this API. The example.com host and error code are illustrative, not a working service or a universal API error format.

Check that the token belongs to the correct environment, audience and account. A production token sent to a staging host, an expired token or a refresh token used where an access token is required can all lead to an authentication failure. Use the provider's documented token flow rather than editing token contents.

Inspect the complete header structure without sharing its secret value. A copied value that already includes the word Bearer can accidentally become Bearer Bearer followed by the token when another client adds the scheme again.

Keep client handling explicit and bounded

async function readProfile(url, token, fetcher = fetch) {
  if (!token) throw new Error("An access token is required.");
  const response = await fetcher(url, {
    headers: {
      Accept: "application/json",
      Authorization: `Bearer ${token}`
    }
  });
  if (response.status === 401) {
    throw new Error("Authentication failed; review the sign-in state.");
  }
  if (!response.ok) {
    throw new Error(`Profile request failed: HTTP ${response.status}`);
  }
  return response.json();
}

This function makes at most one request. It does not guess a refresh endpoint or silently retry forever. If the application supports token renewal, implement the provider's documented flow separately and allow only a bounded retry of the original operation after successful renewal.

Distinguish a readable 401 from a browser access failure

A browser may block access to a cross-origin response when the server's CORS policy does not permit the request. In that case, application code can receive a network-style rejection rather than a readable 401 response. Check the browser's network panel and the API's origin policy before diagnosing the problem as a missing token.

Custom response headers such as WWW-Authenticate may require explicit exposure before browser JavaScript can read them across origins. Do not add a permissive CORS header to the client request and expect it to change server policy. The service or a properly designed application backend must control that policy.

Inspect what the testing client actually sends

In the local REST API Testing Tool, put a Bearer token in the request headers only when testing an endpoint you are authorized to access. Leave the separate Basic-auth field empty for that request: supplying Basic credentials selects a different authentication scheme and can replace the Authorization header.

Compare the final destination after redirects and check whether a gateway, proxy or application generated the response. A login redirect, a cookie-based session and a Bearer-token API are different authentication designs; copying one design's headers into another does not make them interchangeable.

A 403 often indicates that the server refuses an operation even after recognizing the caller, but consult the API's documented status and error codes. Test missing credentials, invalid credentials and a valid permitted request separately. A successful login alone does not prove that the caller may access every resource.