Install and configure Sentry SDK authentication.
Use when setting up a new Sentry integration, configuring DSN,
or initializing Sentry in your project.
Trigger with phrases like "install sentry", "setup sentry",
"sentry auth", "configure sentry DSN".
Install the Sentry SDK, configure DSN-based authentication, and verify error tracking is operational. Covers Node.js (@sentry/node), browser (@sentry/browser), and Python (sentry-sdk) with environment-based configuration and auth token setup for CLI/CI workflows.
Prerequisites
Node.js 18.19+ or 20.6+ (required for ESM support in Sentry SDK v8)
The DSN (Data Source Name) tells the SDK where to send events. It looks like https://<key>@<org>.ingest.sentry.io/<project-id>. Never hardcode it — use environment variables.
# .env (add this file to .gitignore)
SENTRY_DSN=https://[email protected]/0
SENTRY_ENVIRONMENT=development
SENTRY_RELEASE=1.0.0
For production, store the DSN in your secret manager (AWS Secrets Manager, GCP Secret Manager, Vault, etc.) and inject it at deploy time.
Step 3 — Initialize the SDK
Node.js (ESM) — create instrument.mjs at project root:
This file MUST be imported before any other modules. The --import flag ensures Sentry instruments HTTP, database, and framework integrations via monkey-patching at load time.
// instrument.mjs — import BEFORE your app code
import * as Sentry from '@sentry/node';
Sentry.init({
dsn: process.env.SENTRY_DSN,
environment: process.env.SENTRY_ENVIRONMENT || 'development',
release: process.env.SENTRY_RELEASE,
// Performance: 100% in dev, 10-20% in production
tracesSampleRate: process.env.NODE_ENV === 'production' ? 0.1 : 1.0,
// Debug mode — disable in production
debug: process.env.NODE_ENV !== 'production',
// Never send PII by default
sendDefaultPii: false,
integrations: [
// Built-in integrations (httpIntegration, expressIntegration)
// are auto-detected — no manual registration needed
],
});
Send a test event and confirm it appears in the Sentry dashboard:
Node.js:
import * as Sentry from '@sentry/node';
Sentry.captureMessage('Sentry SDK installed successfully', 'info');
// Ensure the event is flushed before process exits
await Sentry.flush(2000);
Python:
import sentry_sdk
sentry_sdk.capture_message("Sentry SDK installed successfully")
# Ensure the event is flushed
sentry_sdk.flush(timeout=2)
Check the Issues tab in your Sentry project within 30 seconds. If the message appears, authentication is working.
Step 5 — Set up auth token for CLI and CI
The DSN authenticates the SDK for sending events. For the Sentry CLI (source maps, releases, deploys), you need a separate auth token.
import * as Sentry from '@sentry/node';
process.on('SIGTERM', async () => {
console.log('Shutting down gracefully...');
await Sentry.flush(5000); // wait up to 5s for pending events
process.exit(0);
});