env.inspector
03 / 10Danger patternRoute Handlersresponse bodiesnetwork tab

Route Handlers (API routes)

A Route Handler only ever runs on the server — that part is guaranteed. What is not guaranteed is what you decide to put in the response body, and a JSON response is exactly as public as a page's HTML.

Try both endpoints

Both routes read the exact same server-only STRIPE_SECRET_KEY. Click each and compare what actually comes back.

GET /api/leak-secret
// response will appear here
GET /api/safe-derived
// response will appear here

The two implementations

app/api/leak-secret/route.js
export async function GET() {
  return Response.json({
    ok: true,
    stripeSecretKey: process.env.STRIPE_SECRET_KEY,
    databaseUrl: process.env.DATABASE_URL,
  })
}
app/api/safe-derived/route.js
export async function GET() {
  const key = process.env.STRIPE_SECRET_KEY || ''
  return Response.json({
    ok: true,
    stripeKeyConfigured: Boolean(key),
    stripeKeyLast4: key.slice(-4),
  })
}
✕This is a public HTTP endpoint, full stop
Anyone who can reach your deployed URL can call /api/leak-secretdirectly — no browser, no JavaScript, no login required unless you add one. Running "on the server" protects the code that computes a value; it says nothing about what you choose to send back.
Verify it yourself
  1. 1
    Open DevTools → Network, click both buttons above, and look at each request. You'll see the full key in the response for one and only a last-4 preview in the other.
  2. 2
    Try it outside the browser entirely: run curl https://your-app.vercel.app/api/leak-secret from a terminal once deployed. No dev tools needed — the leak is just sitting in plain HTTP.
  3. 3
    Check response headers on both requests too. Server, X-Powered-By, and caching headers can leak information about your stack even when the body looks clean — worth a scan on real projects.
Self-check0/3 answered
  • Because Route Handlers run on the server, anything they return is automatically safe.

  • The fix is to compute derived, non-sensitive fields from the secret and return only those.

  • Adding authentication to a Route Handler is unrelated to whether its response leaks secrets.