GitHub REST API returns 401 Unauthorized to a React fetch call
401 UnauthorizedShort 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.
Fix it fast
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"2Match what you see, then jump to its fix
- 1
curl with the same token also returns 401The token is invalid, expired or revokedFix - 2
The request uses btoa(user + ':' + password) or Basic authenticationA username and password were sent instead of a tokenFix - 3
The Network tab shows the request header Authorization: Bearer undefinedThe env variable never reached the browser, so the header says undefinedFix
- 1
What does the error look like?
401 UnauthorizedAuthorization: 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/user2Fix
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/userConfirmed by docs.github.com
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\(" src2Fix
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
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 devConfirmed by vite.dev
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fixWeb apps
Axios request is not cancelled when a React component unmounts
A cancelled request is logged as an error in the catch block
Axios cancels a request when you pass it an AbortSignal and later call abort() on its controller.
4 causes, each with a fix