Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Next.js and React

API calls start failing CORS after Sentry tracing is added to a Vue app

Your error

Reason: missing token 'sentry-trace' in CORS header 'Access-Control-Allow-Headers' from CORS preflight channel3 more lines it can show

Fix it fast

Most common cause, 1 of 3

The default target list sends trace headers to your own API on another port

Add sentry-trace and baggage to the headers the API allows.

Fix
import cors from 'cors'

app.use(cors({
  origin: 'http://localhost:5173',
  allowedHeaders: ['Content-Type', 'Authorization', 'sentry-trace', 'baggage'],
}))

// any other server: answer the preflight with
Access-Control-Allow-Headers: Content-Type, Authorization, sentry-trace, baggage
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Missing token 'sentry-trace' or 'baggage' in Access-Control-Allow-Headers means the Sentry browser SDK (@sentry/vue 11) added its tracing headers to an API call and the preflight answer did not allow them. The default tracePropagationTargets matches any localhost URL. Add both headers to the API's allowed list, or narrow tracePropagationTargets to hosts that accept them.

  • Applies to @sentry/vue 11, Vue 3.5, Vite 8, Express 5, cors 2.8
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Replay the browser's preflight from a terminal and look at which request headers the API says it accepts. When sentry-trace and baggage are absent from that answer, the API is the side that blocks the call.

Run this first
curl -i -X OPTIONS http://localhost:8000/api/users \
  -H "Origin: http://localhost:5173" \
  -H "Access-Control-Request-Method: GET" \
  -H "Access-Control-Request-Headers: sentry-trace, baggage"

Then find the line you see in this list and open its fix:

  1. 1The default target list sends trace headers to your own API on another portThe app runs on localhost:5173, the API on localhost:8000 or similar, and the console names sentry-trace or baggage
  2. 2A third-party API you cannot configure matches a target patternThe blocked request goes to a host you do not run, such as a maps, payment or analytics endpoint
  3. 3propagateTraceparent is on, which adds a third headerReason: missing token 'traceparent' in CORS header 'Access-Control-Allow-Headers' from CORS preflight channel

What does the error look like?

  • Reason: missing token 'sentry-trace' in CORS header 'Access-Control-Allow-Headers' from CORS preflight channel
  • Reason: missing token 'baggage' in CORS header 'Access-Control-Allow-Headers' from CORS preflight channel
  • Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8000/api/users.
  • API requests worked until Sentry.init with browserTracingIntegration was added

Cause 1 of 3

The default target list sends trace headers to your own API on another port

You seeThe app runs on localhost:5173, the API on localhost:8000 or similar, and the console names sentry-trace or baggage

Check

Missing token 'sentry-trace' on a call from localhost:5173 to localhost:8000 comes from Sentry's default tracePropagationTargets, ['localhost', /^\//], which covers every URL that contains localhost. A different port makes the API a different origin, so the browser first sends a preflight asking whether these extra headers may be used, and the API has to name them in its answer. Check whether the API sets an explicit header list.

Check
grep -rn "allowedHeaders\|Access-Control-Allow-Headers" server/

Fix

Add sentry-trace and baggage to the headers the API allows. With the Express cors package, leaving allowedHeaders out also works, because it then echoes the headers the browser asked for.

Fix
import cors from 'cors'

app.use(cors({
  origin: 'http://localhost:5173',
  allowedHeaders: ['Content-Type', 'Authorization', 'sentry-trace', 'baggage'],
}))

// any other server: answer the preflight with
Access-Control-Allow-Headers: Content-Type, Authorization, sentry-trace, baggage

Verify

Repeat the curl preflight. The reply now lists both headers, and the request goes through in the browser.

Verify
curl -s -i -X OPTIONS http://localhost:8000/api/users -H "Origin: http://localhost:5173" -H "Access-Control-Request-Method: GET" -H "Access-Control-Request-Headers: sentry-trace, baggage" | grep -i allow-headers

Confirmed by docs.sentry.io

Did this fix it?

Cause 2 of 3

A third-party API you cannot configure matches a target pattern

You seeThe blocked request goes to a host you do not run, such as a maps, payment or analytics endpoint

Check

Missing token 'sentry-trace' on a request to a service you do not run means one of your tracePropagationTargets patterns matches that service. A string entry, like the default 'localhost', matches any URL that contains it. A loose string or regular expression pulls in outside services that will never allow the Sentry headers. Read the list in your Sentry.init call.

Check
grep -rn "tracePropagationTargets" src

Fix

List only the origins you control, with anchored patterns. If you do not need traces joined across frontend and backend at all, an empty list stops the headers completely.

Fix
Sentry.init({
  // keep app and your other options as they are
  dsn: import.meta.env.VITE_SENTRY_DSN,
  integrations: [Sentry.browserTracingIntegration()],
  tracesSampleRate: 0.2,
  tracePropagationTargets: [/^\/api\//, 'https://api.example.com'],
  // or: tracePropagationTargets: [] to send no trace headers
})

Verify

Reload the app and look at the request headers in the Network panel: the third-party call no longer carries sentry-trace.

Verify
grep -rn "tracePropagationTargets" src

Confirmed by docs.sentry.io

Did this fix it?

Cause 3 of 3

propagateTraceparent is on, which adds a third header

You seeReason: missing token 'traceparent' in CORS header 'Access-Control-Allow-Headers' from CORS preflight channel

Check

Missing token 'traceparent' appears when propagateTraceparent is true, because the SDK then sends a traceparent header too, so an allow list that already has sentry-trace and baggage still fails. Search your Sentry setup for the option.

Check
grep -rn "propagateTraceparent" src

Fix

Add traceparent to the allowed headers next to the two Sentry headers.

Fix
Access-Control-Allow-Headers: Content-Type, sentry-trace, baggage, traceparent

Verify

Run the preflight with all three headers in Access-Control-Request-Headers and confirm each one comes back allowed.

Verify
curl -s -i -X OPTIONS http://localhost:8000/api/users -H "Origin: http://localhost:5173" -H "Access-Control-Request-Method: GET" -H "Access-Control-Request-Headers: sentry-trace, baggage, traceparent" | grep -i allow-headers

Confirmed by docs.sentry.io

Did this fix it?

How do I stop it coming back?

  • Write tracePropagationTargets out in Sentry.init, listing only the API origins you run.
  • Add sentry-trace and baggage to the API's allowed CORS headers in the same change that turns tracing on.
  • Make sure proxies and gateways in front of the API pass sentry-trace and baggage through unchanged.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, Workforce.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.