Maaz Zindani

Loading the index…

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

Sentry says Invalid token (401) during the Next.js build

ErrorSentry 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.

  • Applies to Next.js 15, Next.js 16, @sentry/nextjs 10, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 info
  2. 2Match what you see, then jump to its fix

    1. 1Works on your machine, 401 on CI or the hosting build.SENTRY_AUTH_TOKEN is not set where the build runsFix
    2. 2The variable is set and the build still gets 401.The token is wrong, was replaced, or was copied incompletelyFix
    3. 3A 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

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 build

Confirmed by docs.sentry.io

Did this fix it?

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 info

2Fix

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 CI

3Verify

sentry-cli info runs without an authentication error, and the next build uploads the source maps.

sentry-cli info

Confirmed by docs.sentry.io

Did this fix it?

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/null

2Fix

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 info

Confirmed by docs.sentry.io

Did this fix it?

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

Next step

Want this handled for good?

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