Sentry says Invalid token (401) during the Next.js build
Sentry reported an error: Invalid token (http status: 401)Short answer
The Sentry build plugin uploads source maps with an auth token, and Sentry refused that token. Usual causes: SENTRY_AUTH_TOKEN is missing in the build environment, the token was copied wrongly or replaced, or the project lives in another region or a self-hosted Sentry and sentryUrl is not set. Create an organization token and check it with sentry-cli info.
Fix it fast
1Find out which cause you have
Check what the build machine actually sees. Run the same check where the build runs, not only on your laptop.
export SENTRY_AUTH_TOKEN=<your-sentry-auth-token> sentry-cli info2Match what you see, then jump to its fix
- 1
Works on your machine, 401 on CI or the hosting build.SENTRY_AUTH_TOKEN is not set where the build runsFix - 2
The variable is set and the build still gets 401.The token is wrong, was replaced, or was copied incompletelyFix - 3
A fresh token still fails, and your Sentry URL is not https://sentry.io.The project is in another region or on a self-hosted SentryFix
- 1
What does the error look like?
Sentry reported an error: Invalid token (http status: 401)Source maps do not upload and production stack traces stay minified
Cause 1 of 3
SENTRY_AUTH_TOKEN is not set where the build runs
1Check
The Sentry docs say to put the token in the environment and also add it to CI. A .env file on your laptop does not reach a remote build.
grep -n "authToken\|SENTRY_AUTH_TOKEN" next.config.*2Fix
Add SENTRY_AUTH_TOKEN as a build-time secret in the CI or hosting settings and read it in withSentryConfig.
// next.config.js
module.exports = withSentryConfig(nextConfig, {
org: 'my-org-slug',
project: 'my-project-slug',
authToken: process.env.SENTRY_AUTH_TOKEN,
})3Verify
Trigger a new build and watch for the upload step to finish without the 401 line.
npm run buildConfirmed by docs.sentry.io
Cause 2 of 3
The token is wrong, was replaced, or was copied incompletely
1Check
Organization tokens are shown only once, right after creation. Later only the last characters show, so a half-copied or old token cannot be recovered. Run sentry-cli info with the exact value the build uses.
sentry-cli info2Fix
Create a new organization token in Settings > Developer Settings > Organization Tokens, then replace the stored secret with it.
# Settings > Developer Settings > Organization Tokens > Create New Token
# then store the value as SENTRY_AUTH_TOKEN in CI3Verify
sentry-cli info runs without an authentication error, and the next build uploads the source maps.
sentry-cli infoConfirmed by docs.sentry.io
Cause 3 of 3
The project is in another region or on a self-hosted Sentry
1Check
The plugin talks to https://sentry.io/ unless sentryUrl says otherwise. The docs say to set it for self-hosted instances or regions other than sentry.io. The CLI reads SENTRY_URL.
grep -n "sentryUrl\|SENTRY_URL" next.config.* .env* 2>/dev/null2Fix
Set sentryUrl in withSentryConfig to the base URL of your Sentry instance.
module.exports = withSentryConfig(nextConfig, {
org: 'my-org-slug',
project: 'my-project-slug',
authToken: process.env.SENTRY_AUTH_TOKEN,
sentryUrl: 'https://sentry.example.com/',
})3Verify
Run the CLI check against the same URL.
export SENTRY_URL=https://sentry.example.com/
sentry-cli infoConfirmed by docs.sentry.io
How do I stop it coming back?
- Use an organization token for CI, and store it only as a secret.
- Save the token in your password manager when you create it: Sentry will not show an organization token again.
- Add sentry-cli info as a first step in the build pipeline so a bad token fails early.
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
Web apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
Clerk cannot read its publishable key in a Next.js app
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY is undefined
Clerk reads NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY.
3 causes, each with a fixWeb apps
The shell cannot find create-next-app
command not found: create-next-app
create-next-app is not meant to be installed as a shell command.
2 causes, each with a fixWeb apps
ESLint fails with: Cannot find module 'next/babel'
ESLint stops on the first file with a Babel or parser error instead of listing lint results
next/babel is a Babel preset that ships inside the next package.
3 causes, each with a fixWeb apps
Firebase Auth says the API key is not valid
Firebase: Error (auth/api-key-not-valid.-please-pass-a-valid-api-key.)
The apiKey your app sends is either empty, wrong, or blocked.
4 causes, each with a fixWeb apps
Jest fails with SyntaxError: Unexpected token when testing a Next.js component
Jest failed to parse a file.
Jest only understands plain JavaScript until a transform and a few file mappings tell it more.
3 causes, each with a fix