Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

GitHub REST API returns 401 Unauthorized to a React fetch call

Error401 Unauthorized

Short answer

GitHub sends 401 when the credentials in the request are not valid: a wrong, expired or revoked token, a password instead of a token, or a header that never contained the token. In React the last one is common, because an env variable that is not exposed to the browser arrives as undefined.

  • Applies to React 19, Vite 7, GitHub REST API
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Call the API from your terminal with the same token. If curl works, the token is fine and the React code sends something different. If curl also returns 401, the token is the problem.

    curl --request GET \
      --url "https://api.github.com/user" \
      --header "Authorization: Bearer YOUR-TOKEN"
  2. 2Match what you see, then jump to its fix

    1. 1curl with the same token also returns 401The token is invalid, expired or revokedFix
    2. 2The request uses btoa(user + ':' + password) or Basic authenticationA username and password were sent instead of a tokenFix
    3. 3The Network tab shows the request header Authorization: Bearer undefinedThe env variable never reached the browser, so the header says undefinedFix

What does the error look like?

  • 401 Unauthorized
  • Authorization: Bearer undefined

Cause 1 of 3

The token is invalid, expired or revoked

1Check

GitHub says that authenticating with invalid credentials first returns 401 Unauthorized. Its troubleshooting guide lists a token that has expired or been revoked among the things to check. Open your token settings and look at the expiry date.

curl -i --header "Authorization: Bearer YOUR-TOKEN" https://api.github.com/user

2Fix

Create a new fine-grained personal access token with the permissions the endpoint needs, and use it. GitHub recommends fine-grained tokens over classic tokens.

curl --request GET \
  --url "https://api.github.com/user" \
  --header "Authorization: Bearer NEW-TOKEN" \
  --header "X-GitHub-Api-Version: 2026-03-10"

3Verify

The request returns 200 and your profile JSON.

curl -s -o /dev/null -w "%{http_code}\n" --header "Authorization: Bearer NEW-TOKEN" https://api.github.com/user

Confirmed by docs.github.com

Did this fix it?

Cause 2 of 3

A username and password were sent instead of a token

1Check

GitHub's troubleshooting guide states that username and password basic authentication is not supported. A personal access token, or a GitHub App or OAuth app token, is required.

grep -rnE "Basic |btoa\(" src

2Fix

Send a token in the Authorization header, using the Bearer scheme.

const res = await fetch('https://api.github.com/user', {
  headers: { Authorization: `Bearer ${token}` },
});

3Verify

Check res.status is 200 in the browser Network tab.

console.log(res.status);

Confirmed by docs.github.com

Did this fix it?

Cause 3 of 3

The env variable never reached the browser, so the header says undefined

1Check

Vite exposes only variables prefixed with VITE_ to client code; others log as undefined, and .env files are read only at server start, so a change needs a restart.

console.log(import.meta.env.VITE_GITHUB_TOKEN);

2Fix

Rename the variable with the VITE_ prefix in .env and restart the dev server. Treat it as public, though: Vite's docs warn that VITE_ variables end up in the built source, so use this only for a throwaway token while developing.

# .env.local
VITE_GITHUB_TOKEN=your-token

// code
headers: { Authorization: `Bearer ${import.meta.env.VITE_GITHUB_TOKEN}` }

3Verify

Restart the dev server, then confirm the header in the Network tab shows a token value.

npm run dev

Confirmed by vite.dev

Did this fix it?

How do I stop it coming back?

  • Never ship a GitHub token in browser code: put it on a backend or serverless function that makes the call and returns the result.
  • Give each token the smallest permissions it needs, and set an expiry you can track.
  • Log the status and the response body on every failed call so 401 and 403 are told apart.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.