# Your API error response shows your secret key: stop sending raw backend errors to the browser

> Canonical page (link this when citing): https://unbreakmyapp.vercel.app/fixes/backend-error-leaks-api-key-to-browser.html
> Category: Env vars & leaked keys | Tools: Lovable, Bolt, Cursor, Replit, v0 | Published: 2026-10-30 | Updated: 2026-10-30
> Weakness type: CWE-209 Generation of Error Message Containing Sensitive Information (https://cwe.mitre.org/data/definitions/209.html)

## The error

An error from your API route or edge function shows up in the browser (Network tab, toast or console) containing an Authorization: Bearer sk-... header, a database connection string, process.env values, a full stack trace or the third-party request config, e.g. res.json({ error }) or return new Response(JSON.stringify(err))

## Why it happens

Your keys can be kept out of the frontend bundle and still leak at runtime. AI builders often write catch blocks like catch (err) { return res.status(500).json({ error: err }) } or send err.message, err.response.data or JSON.stringify(err) straight back to the client so the UI can show 'what went wrong'. Many error objects carry far more than a message: an Axios error includes the request config with its headers (including your Authorization bearer token), database drivers can include the connection string or SQL, some SDKs echo the request body, and a debug helper may dump process.env. Anyone who triggers an error, for example by sending a bad input, can open DevTools and read it. Development-mode error pages and verbose logging turned on in production make it worse. The fix is to log the details only on the server and send the browser a generic message with a reference ID, then rotate any key that was ever exposed.

## How to fix it

1. Reproduce it safely: open your app, open DevTools → Network, trigger failures (submit empty or very long input, log out mid-action, call the API route with curl and a bad body) and read every error response body. Search them for sk_, sk-, Bearer, key, secret, password, postgres:// and eyJ (JWTs).
2. Search your server code for catch blocks that return the error object: res.json({ error: err }), res.send(err), JSON.stringify(err), err.response.data, err.config or err.stack sent in a response. Check API routes, server actions, Supabase edge functions and any proxy route that calls OpenAI, Stripe or another paid API.
3. Replace them with a safe pattern: generate an ID (crypto.randomUUID()), log the full error with that ID on the server only (console.error or your error tracker), and return a fixed message such as { error: 'Something went wrong', id } with an appropriate status code.
4. Where users need a specific message (validation errors, 'email already in use'), return only messages you wrote yourself, never text copied from a third-party error.
5. Redact secrets in your logs too: don't log whole request configs or headers, and strip Authorization, cookies and API keys before logging, because hosting logs are often visible to everyone on the team or integrations.
6. Make sure production runs in production mode (NODE_ENV=production, no debug flags, no framework dev error overlay) and remove any /debug, /env or /health route that prints environment variables.
7. Rotate every key that appeared in an error response, even once: create a new key in the provider dashboard, update it in your hosting environment variables, redeploy, then revoke the old key. Check the provider's usage and billing for activity you don't recognise.
8. Re-run the first step after deploying to confirm error responses now contain only the generic message and ID.

## Fix prompt (paste into Lovable, Bolt, Cursor, Replit or v0)

```text
Audit my backend for secrets leaking through error responses. My server code is in <API ROUTES / SERVER ACTIONS / SUPABASE EDGE FUNCTIONS FOLDER>. Find every catch block or error handler that sends the error object, err.message from a third-party SDK, err.response.data, err.config, err.stack or JSON.stringify(err) back to the client. Replace them with a shared helper that creates an error ID with crypto.randomUUID(), logs the full error server-side with that ID (redacting Authorization headers, cookies, API keys and connection strings), and returns { error: 'Something went wrong', id } with the right status code. Keep user-facing validation messages I wrote myself. Remove any route that prints environment variables, make sure nothing runs in debug or development mode in production, and list every secret that may already have leaked so I can rotate it. Don't print or expose any key values in your answer.
```

## FAQ

### My keys are in environment variables, so how can they leak?

Environment variables keep keys out of your code, but your server still uses them in requests. If the error from that request is sent back whole, the key or Authorization header can travel with it.

### Is showing err.message safe?

Your own messages are. Messages from SDKs, database drivers and HTTP clients can include URLs, queries, request details and sometimes credentials, so log them on the server and show a generic message instead.

### Do I really need to rotate the key if it only showed in an error?

Yes. Anyone who saw the response, any browser extension, and any log or screenshot may have it. Rotating is quick and is the only way to be sure the old key is useless.

### How do I still debug production errors?

Log the full error on the server with an ID, return that ID to the user, and look it up in your hosting logs or an error tracker such as Sentry when someone reports a problem.

## Sources (official documentation)

- [OWASP: Error Handling Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Error_Handling_Cheat_Sheet.html)
- [OWASP: Logging Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Logging_Cheat_Sheet.html)

## Related fixes

- [OpenAI / Stripe secret key leaked in the browser](https://unbreakmyapp.vercel.app/fixes/api-key-leaked-in-frontend.html)
- [Someone reported a security hole in my vibe-coded app: what to do first](https://unbreakmyapp.vercel.app/fixes/vibe-coded-app-security-hole-reported-first-steps.html)
- [Missing security headers (CSP, X-Frame-Options, nosniff) on a Vercel or Netlify app](https://unbreakmyapp.vercel.app/fixes/missing-security-headers-csp-vercel-netlify.html)
- [Supabase: Edge Function returned a non-2xx status code (how to find the real error and fix it)](https://unbreakmyapp.vercel.app/fixes/supabase-edge-function-non-2xx-status-code.html)
- [Your API keys may have leaked: how to rotate every key without breaking your app](https://unbreakmyapp.vercel.app/fixes/rotate-api-keys-after-leak-or-breach.html)

---
Unbreak My App: free fixes for broken vibe-coded apps. https://unbreakmyapp.vercel.app/
