Sentry for a new app: a public DSN and two secret tokens

Set up Sentry for a new Next.js app: project and DSN, the build token for source maps, API scopes, EU region hostnames and common failures.

Simon, Founder of Shipyard

By · Founder of Shipyard

6 minute read

Three credentials with three different rules

Setting up Sentry for a new app means creating a project, copying its DSN into the app, and giving the build a token so it can upload source maps. The DSN can be public. The build token is a secret that belongs only in your build environment. And if a tool creates the project for you, that tool holds a third, more powerful token which should never reach the app at all.

Mixing these up is the most common mistake: people hide the DSN with great care and then paste an API token with write access into a client-side variable.

ValueWhat it can doSecret?Where it goes
DSNSend events into one project. It cannot read anythingNoSENTRY_DSN on the server and NEXT_PUBLIC_SENTRY_DSN for the browser
Organization tokenUpload source maps and other build-time tasks; fixed, limited permissionsYesSENTRY_AUTH_TOKEN in the build environment or CI
Personal token or internal integrationCall the Sentry API with the scopes you ticked, for example create or delete projectsYesOnly in the tool that manages Sentry for you

What a DSN contains

DSN stands for Data Source Name. It is a URL with three parts: a public key, the host of the Sentry server that receives events, and the numeric ID of the project. Sentry's documentation states plainly that DSNs are safe to keep public, because they only allow new events to be submitted. The realistic abuse is somebody sending junk events to your project, and the defence is to revoke the key and issue another under the project's Client Keys (DSN) settings.

One behaviour catches people out. An SDK that starts with an empty DSN does not raise an error. It stays silent and sends nothing. Server SDKs will read SENTRY_DSN from the environment when no DSN is passed in code, but a browser has no environment, so the browser side needs the value handed to it explicitly. In Next.js that means a variable whose name starts with NEXT_PUBLIC_, present when the app is built.

Text
https://<public_key>@<host>/<project_id>

https://examplePublicKey@o0.ingest.sentry.io/0

The install command

Sentry's current quickstart offers an AI-assisted sentry init command and still documents the older framework wizard for people who prefer a fixed template. The wizard is one command, run in the project folder.

npx @sentry/wizard@latest -i nextjs

The files an installation leaves behind

Whichever route you take, a complete App Router installation consists of the same pieces. Knowing them lets you spot the one that is missing when only half of your errors arrive.

Why source maps need their own token

Production JavaScript is minified, so an unprocessed stack trace points at column 48,211 of a bundle. Source maps translate that back to your file and line. The upload happens during the production build only, never in next dev, and it authenticates with SENTRY_AUTH_TOKEN.

Use an organization token for this. You create one under Settings, Developer Settings, Organization Tokens. Its permissions are fixed and deliberately narrow, any member can create one, and the full value is shown a single time. If your stack traces in Sentry still show minified code after a deploy, the first thing to check is whether that variable exists in the environment where the build ran, not where the app runs. On hosts that separate build-time and run-time variables these are different lists.

Creating the project through the API

A project is created with one POST to the team that will own it. The organization token from the previous section cannot do this. Sentry's guidance is to use an internal integration when you need fuller API access on behalf of an organization; a personal token also works and is simpler for one person. Personal tokens are bound to your user, their scopes are chosen at creation and cannot be edited afterwards, and they stop working if you leave the organization.

The default_rules field is worth a look. It defaults to true, which gives the project an alert for every new issue. Passing false creates a silent project that notifies nobody until you add rules.

Terminal
curl https://sentry.io/api/0/teams/{org_slug}/{team_slug}/projects/ \
  -H 'Authorization: Bearer <auth_token>' \
  -H 'Content-Type: application/json' \
  -d '{"name":"myapp","slug":"myapp","platform":"javascript-nextjs","default_rules":true}'
CallScopes that allow it
Create a project in a teamproject:write or project:admin
List a project's client keys to read the DSNproject:read, project:write or project:admin
List the organization's teamsorg:read, org:write or org:admin
Delete a projectproject:admin only

Regions decide the hostname

A Sentry organization lives in one data storage location. It is selected once and cannot be changed afterwards short of creating a new organization. The API has a hostname for each location: us.sentry.io for the United States and de.sentry.io for the European Union. Sentry's API reference names sentry.io as the general host and says the region hostname keeps a request inside your storage location and usually answers faster. It does not describe what a call to the wrong region returns. Shipyard's own check reads a 404 for an organization slug you know exists as a likely region mismatch, so if a script fails that way for an EU organization, try https://de.sentry.io as the base URL before you doubt the slug. Your organization's settings page shows which region it is in.

Symptoms and their causes

What you seeLikely causeFix
401 from the APIToken mistyped, revoked, or the wrong kindCreate a new token and paste it whole
403 from the APIToken lacks a scope for that callPersonal tokens cannot be edited: create a new one with the scopes in the table above
403 on project creation with correct scopesThe organization has disabled project creation by membersUse a token with org:write, or hold the Team Admin role on that team
404 on a real organizationPossibly the wrong region hostname, or a mistyped slugCheck the slug, then try de.sentry.io for an EU organization
Project creation fails for lack of a teamA project must belong to a team and the organization has noneCreate a team under Settings, Teams
Server errors arrive, browser errors do notThe public DSN variable was absent at build timeSet NEXT_PUBLIC_SENTRY_DSN and redeploy
Stack traces are minifiedSENTRY_AUTH_TOKEN missing in the buildAdd it to build variables and rebuild

Sampling before the first invoice

Errors are always captured. Tracing is the part with a dial. The configuration files take a tracesSampleRate between 0 and 1, and Sentry's own example uses 1.0 in development and 0.1 in production. Start there and adjust after looking at the usage stats page, because a busy endpoint traced at 100 percent consumes quota far faster than errors ever will.

To prove the whole chain works, the wizard adds a page at /sentry-example-page with a button that throws one error in the browser and one on the server. Press it on the deployed site, not only locally. A local test passes even when the production variables are missing.

The part Shipyard handles

When Sentry is in your stack, Shipyard uses the token you stored in your keychain to look for a project whose slug equals the project name. If none exists it creates one with the platform javascript-nextjs in the team you named, or in the organization's first team if you named none. It reads the first client key, creating one called Shipyard if the list is empty, and writes the public DSN to your host as both SENTRY_DSN and NEXT_PUBLIC_SENTRY_DSN. The scopes it asks for are project:read, project:write, team:read and org:read, plus project:admin only if you want a teardown to delete the project.

It does not create the Sentry account, and it does not create or store a SENTRY_AUTH_TOKEN, so source map upload is yours to add. It also leaves alert rules and sampling at their defaults.

Sources

Launch in an hour, not a weekend

Shipyard connects your accounts once, then sets up the domain, hosting, database, email and payments for every new product.

See how it works

Read next