---
title: "Tunneling"
description: "Send browser SDK events to Sentry through your own server, so ad blockers don't drop them."
url: https://docs.sentry.io/platforms/javascript/guides/wasm/configuration/tunneling/
---

# Tunneling for Wasm

Ad blockers often block requests to Sentry, so your events never arrive. A tunnel is an endpoint on your own origin that forwards events to Sentry, and ad blockers let requests to your own origin through.

## [Setting Up the Tunnel](https://docs.sentry.io/platforms/javascript/guides/wasm/configuration/tunneling.md#setting-up-the-tunnel)

Available since: `v11.13.0`

The browser sends its events to a path on your own origin derived from the DSN, for example `/qpo967`, and your server forwards them to Sentry.

If your server doesn't run a Sentry JavaScript SDK, or you want the tunnel as a route in your app, [set up the tunnel manually](https://docs.sentry.io/platforms/javascript/guides/wasm/configuration/tunneling.md#setting-up-the-tunnel-manually) instead.

The tunnel only works while the server SDK is enabled. Without a DSN, or with `enabled: false`, the browser's events are lost.

### [Turn On the Tunnel in the Browser](https://docs.sentry.io/platforms/javascript/guides/wasm/configuration/tunneling.md#turn-on-the-tunnel-in-the-browser)

Set `tunnel: true` in your browser `Sentry.init`:

```javascript
Sentry.init({
  dsn: "https://<key>@o<orgId>.ingest.sentry.io/<projectId>",
  tunnel: true,
});
```

### [Add the Integration to Your Server](https://docs.sentry.io/platforms/javascript/guides/wasm/configuration/tunneling.md#add-the-integration-to-your-server)

Add `serveTunnelIntegration` to your server `Sentry.init`:

```javascript
Sentry.init({
  dsn: "https://<key>@o<orgId>.ingest.sentry.io/<projectId>",
  integrations: [Sentry.serveTunnelIntegration()],
});
```

Use the server that serves your app. The tunnel is on the same origin as your page. If a separate dev server like Vite serves your page locally, use a [fixed path](https://docs.sentry.io/platforms/javascript/guides/wasm/configuration/tunneling.md#using-a-fixed-path) and proxy it to your server.

No additional setup is needed. The server SDK answers tunnel requests before they reach your app, so your routes and middleware don't need any changes.

To check the tunnel, trigger an event and look in your browser's Network tab for a `POST` to a path on your own origin that returns `200`.

### [Using a Different DSN in the Browser](https://docs.sentry.io/platforms/javascript/guides/wasm/configuration/tunneling.md#using-a-different-dsn-in-the-browser)

The server answers one path per DSN it allows. If your browser uses a different DSN than your server, add it to `allowedDsns`:

```javascript
Sentry.init({
  dsn: "https://<key>@o<orgId>.ingest.sentry.io/<projectId>",
  integrations: [
    Sentry.serveTunnelIntegration({
      allowedDsns: ["<your browser DSN>"],
    }),
  ],
});
```

### [Using a Fixed Path](https://docs.sentry.io/platforms/javascript/guides/wasm/configuration/tunneling.md#using-a-fixed-path)

To serve the tunnel at a path you choose, set it on both sides:

```javascript
Sentry.init({
  dsn: "https://<key>@o<orgId>.ingest.sentry.io/<projectId>",
  tunnel: "/monitoring",
});
```

*Other available variations of the above snippet: Server*

### [What the Tunnel Forwards](https://docs.sentry.io/platforms/javascript/guides/wasm/configuration/tunneling.md#what-the-tunnel-forwards)

* Only events for your server's DSN and your `allowedDsns`, so the tunnel can't be used as an open proxy.
* The visitor's real IP address, without cookies or auth headers.

## [Setting Up the Tunnel Manually](https://docs.sentry.io/platforms/javascript/guides/wasm/configuration/tunneling.md#setting-up-the-tunnel-manually)

The browser setup works the same as for the automatic tunnel, except that you point the browser SDK to the tunnel path you want to use, for example `/tunnel`:

```javascript
Sentry.init({
  dsn: "https://<key>@o<orgId>.ingest.sentry.io/<projectId>",
  tunnel: "/tunnel",
});
```

How you serve that path depends on your server. If it runs a Sentry JavaScript SDK, you can use the tunnel helpers it comes with. Otherwise, write the endpoint yourself.

### [With a Sentry JavaScript SDK on Your Server](https://docs.sentry.io/platforms/javascript/guides/wasm/configuration/tunneling.md#with-a-sentry-javascript-sdk-on-your-server)

Keep `serveTunnelIntegration` on your server. Set `intercept: false` so your route answers the requests instead of the SDK, and set `path` to your route so tunnel requests stay out of your traces:

```javascript
Sentry.init({
  dsn: "https://<key>@o<orgId>.ingest.sentry.io/<projectId>",
  integrations: [
    Sentry.serveTunnelIntegration({ intercept: false, path: "/tunnel" }),
  ],
});
```

Answer the requests at your endpoint with `handleTunnelRequest`, which forwards them to Sentry:

```javascript
import express from "express";
import * as Sentry from "@sentry/node";

const app = express();

app.post("/tunnel", (req, res) => Sentry.handleTunnelRequest(req, res));
```

*Other available variations of the above snippet: Fetch API*

Add the route before any middleware that reads the request body.

To forward a different browser DSN, add it to `allowedDsns` on the integration, as described in [Using a Different DSN in the Browser](https://docs.sentry.io/platforms/javascript/guides/wasm/configuration/tunneling.md#using-a-different-dsn-in-the-browser). `handleTunnelRequest` uses the same list.

### [On Other Servers](https://docs.sentry.io/platforms/javascript/guides/wasm/configuration/tunneling.md#on-other-servers)

Your endpoint must:

* Forward the request body to Sentry, without cookies or auth headers.
* Accept only envelopes for your own DSNs, so it can't be used as an open proxy.
* Send the envelope only to the host of that DSN.

Example endpoints

```csharp
// This functionality is now built-in to the Sentry.AspNetCore package.
// See https://docs.sentry.io/platforms/dotnet/guides/aspnetcore/#tunnel
// docs for more information.

// This example shows how you could implement it yourself:

using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text.Json;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;

// Change host appropriately if you run your own Sentry instance.
const string host = "sentry.io";
// Set knownProjectIds to a list with your Sentry project IDs which you
// want to accept through this proxy.
var knownProjectIds = new HashSet<string>() {  };

var client = new HttpClient();
WebHost.CreateDefaultBuilder(args).Configure(a =>
    a.Run(async context =>
    {
        context.Request.EnableBuffering();
        using var reader = new StreamReader(context.Request.Body);
        var header = await reader.ReadLineAsync();
        var headerJson = JsonSerializer.Deserialize<Dictionary<string, object>>(header);
        if (headerJson.TryGetValue("dsn", out var dsnString)
            && Uri.TryCreate(dsnString.ToString(), UriKind.Absolute, out var dsn))
        {
            var projectId = dsn.AbsolutePath.Trim('/');
            if (knownProjectIds.Contains(projectId) && string.Equals(dsn.Host, host, StringComparison.OrdinalIgnoreCase)) {
              context.Request.Body.Position = 0;
              await client.PostAsync($"https://{dsn.Host}/api/{projectId}/envelope/",
                  new StreamContent(context.Request.Body));
            }
        }
    })).Build().Run();
```

*Other available variations of the above snippet: Rails*

#### [Credentials on Tunnel Requests](https://docs.sentry.io/platforms/javascript/guides/wasm/configuration/tunneling.md#credentials-on-tunnel-requests)

The browser sends cookies and HTTP authentication to an endpoint on the same origin as your app. If your endpoint forwards request headers, it sends them on to Sentry.

Forward only the request body, or tell the browser to send no credentials:

```javascript
Sentry.init({
  dsn: "https://<key>@o<orgId>.ingest.sentry.io/<projectId>",
  tunnel: "/tunnel",
  transportOptions: {
    fetchOptions: { credentials: "omit" },
  },
});
```

If a layer in front of your tunnel requires credentials, like Vercel Deployment Protection or Cloudflare Access, keep sending them and forward only the request body.
