The Astro SDK is currently in Beta. Features and APIs may change. Please report any issues on GitHub.
Installation
Install with Astro CLI
The easiest way to add Sentry to your Astro project:This will:
- Install the
@sentry/astropackage - Add the integration to
astro.config.mjs - Set up automatic instrumentation
Version Compatibility
- Astro 3.5.2+: Automatic server instrumentation
- Astro 3.0+: Manual middleware setup required
- Works with SSR and static site generation
Basic Setup
Astro Configuration
Add Sentry to yourastro.config.mjs:
Client Initialization
Createsrc/sentry.client.config.ts:
Server Initialization
Createsrc/sentry.server.config.ts:
Server-Side Instrumentation
Automatic (Astro 3.5.2+)
For Astro 3.5.2+, server instrumentation is automatic. No middleware setup needed.Manual (Astro versions below 3.5.2)
For older versions, createsrc/middleware.ts:
Disable Automatic Instrumentation
If you want to opt out:Error Handling
API Routes
Server-Side Rendering
Client-Side Components
Performance Monitoring
Custom Spans
Database Queries
Context and User Information
Setting Context in API Routes
Client-Side User Context
Source Maps Upload
Configuration
Environment Variables
Create a.env file:
Framework Integrations
React Integration
Vue Integration
Deployment
- Vercel
- Netlify
- Cloudflare Pages
- Node.js Adapter
Add environment variables in Vercel:
SENTRY_DSNSENTRY_AUTH_TOKEN
Best Practices
SSR Configuration
Configure both client and server for full coverage.
Middleware Order
Place Sentry.handleRequest() first in middleware sequence.
Source Maps
Always upload source maps for production deployments.
Error Boundaries
Use try-catch in API routes and page components.
Troubleshooting
Middleware Not Working
Middleware Not Working
For Astro versions below 3.5.2:
- Ensure
src/middleware.tsexists - Verify
Sentry.handleRequest()is called - Check that output is ‘server’ or ‘hybrid’
Source Maps Not Uploaded
Source Maps Not Uploaded
Verify:
SENTRY_AUTH_TOKENis setsourceMapsUploadOptionsis configured- Build completes successfully
Next Steps
API Routes
Monitor API endpoint performance
SSR Pages
Track server-rendered pages
Session Replay
Debug with session recordings
Adapters
Configure for your deployment platform