Authentication Methods
The CLI supports three authentication methods, checked in this priority order:SENTRY_AUTH_TOKENenvironment variable (highest priority)SENTRY_TOKENenvironment variable (legacy)- OAuth tokens stored in SQLite (lowest priority)
Environment Variable Authentication
The simplest way to authenticate is via environment variables:- Bypass all expiry and refresh logic
- Are never written to disk
- Take priority over stored OAuth tokens
- Empty or whitespace-only values are treated as unset
SENTRY_AUTH_TOKEN takes precedence over SENTRY_TOKEN to match the legacy sentry-cli behavior.OAuth Device Flow
The recommended authentication method is OAuth device flow:- CLI requests a device code from Sentry’s
/oauth/device/code/endpoint - CLI displays a user code and verification URL
- User visits the URL and enters the code (or scans QR code)
- CLI polls
/oauth/token/endpoint until user completes authorization - Access token and refresh token are stored in SQLite
Device Flow Implementation
The device flow is implemented insrc/lib/oauth.ts:
OAuth Scopes
The CLI requests the following scopes:project:read- Read project dataproject:write- Write project dataorg:read- Read organization dataevent:read- Read eventsevent:write- Write eventsmember:read- Read organization membersteam:read- Read team data
Token Storage
OAuth tokens are stored in SQLite at~/.sentry/config.db:
id = 1) to store the active credential.
Token Retrieval Priority
ThegetAuthToken() function checks sources in priority order:
Automatic Token Refresh
OAuth tokens are automatically refreshed when:- Less than 10% of the token’s lifetime remains (default threshold)
- A 401 Unauthorized response is received from the API
- Forced via
sentry auth refresh
Refresh Flow
The refresh flow is implemented insrc/lib/db/auth.ts:
Token refresh uses a singleton promise to prevent concurrent refresh attempts during parallel API calls.
Self-Hosted Sentry
For self-hosted Sentry instances, configure both URL and client ID:Creating an OAuth App
To use OAuth with self-hosted Sentry (requires 26.1.0+):- Navigate to Settings → Developer Settings in your Sentry instance
- Create a new public OAuth application
- Copy the Client ID
- Set
SENTRY_CLIENT_IDenvironment variable
Configuration Resolution
The CLI reads configuration lazily (not at module load) to respect environment variables set after import:SENTRY_URL before the OAuth flow begins.
Authentication State Commands
Check Status
Manual Refresh
Logout
- Auth tokens
- User info cache
- Organization region cache
- Pagination cursors
Logging out only clears SQLite-stored tokens. Environment variable tokens remain active.
Token Source Tracking
The CLI tracks where each token originated via theAuthSource type:
Error Handling
Authentication errors use theAuthError class with specific reason codes:
AuthError is thrown, providing a seamless authentication experience.