Skip to main content
The Sentry Astro SDK provides error monitoring and performance tracking for Astro applications with support for both client and server-side rendering.
The Astro SDK is currently in Beta. Features and APIs may change. Please report any issues on GitHub.

Installation

1

Install with Astro CLI

The easiest way to add Sentry to your Astro project:
This will:
  • Install the @sentry/astro package
  • Add the integration to astro.config.mjs
  • Set up automatic instrumentation
2

Manual Installation

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 your astro.config.mjs:

Client Initialization

Create src/sentry.client.config.ts:

Server Initialization

Create src/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, create src/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

Add environment variables in Vercel:
  • SENTRY_DSN
  • SENTRY_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

For Astro versions below 3.5.2:
  1. Ensure src/middleware.ts exists
  2. Verify Sentry.handleRequest() is called
  3. Check that output is ‘server’ or ‘hybrid’
Verify:
  1. SENTRY_AUTH_TOKEN is set
  2. sourceMapsUploadOptions is configured
  3. 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