How to Fix an Exposed API Key
A password-like key that is supposed to stay private is sitting in your app where other people can find it. Anyone who copies it can act as you — run up bills, read your data, or send email in your name.
Why AI tools cause it
AI coding tools often paste an API key straight into a file to make something work, instead of hiding it in a private environment variable. It runs fine, so nothing warns you.
The goal
The key is stored only in a server-side secret / environment variable, never in code or the client bundle, and the exposed value has been rotated (regenerated) because the old one is public.
The steps
- Move the key's value into a server-only environment variable
- Remove the literal value from the code and any client-side bundle (the spot the scan shows)
- Make sure the .env file is in .gitignore
- Rotate (regenerate) the key at the provider — assume the old one is compromised
Fix it with your AI coding tool
Pick your tool and paste the instruction. It is written to make the goal above true, and to never print any secret values back to you.
In my app (the spot the scan shows): a secret key is exposed. Please make this true: The key is stored only in a server-side secret / environment variable, never in code or the client bundle, and the exposed value has been rotated (regenerated) because the old one is public. Steps: Move the key's value into a server-only environment variable; Remove the literal value from the code and any client-side bundle (the spot the scan shows); Make sure the .env file is in .gitignore; Rotate (regenerate) the key at the provider — assume the old one is compromised. Then tell me exactly what you changed, and do not print any secret values back to me.
In my Lovable app (the spot the scan shows): a secret key is exposed. Please make this true: The key is stored only in a server-side secret / environment variable, never in code or the client bundle, and the exposed value has been rotated (regenerated) because the old one is public. Steps: Move the key's value into a server-only environment variable; Remove the literal value from the code and any client-side bundle (the spot the scan shows); Make sure the .env file is in .gitignore; Rotate (regenerate) the key at the provider — assume the old one is compromised. Then tell me exactly what you changed, and do not print any secret values back to me.
In my codebase (the spot the scan shows): a secret key is exposed. Please make this true: The key is stored only in a server-side secret / environment variable, never in code or the client bundle, and the exposed value has been rotated (regenerated) because the old one is public. Steps: Move the key's value into a server-only environment variable; Remove the literal value from the code and any client-side bundle (the spot the scan shows); Make sure the .env file is in .gitignore; Rotate (regenerate) the key at the provider — assume the old one is compromised. Then tell me exactly what you changed, and do not print any secret values back to me.
In my codebase (the spot the scan shows): a secret key is exposed. Please make this true: The key is stored only in a server-side secret / environment variable, never in code or the client bundle, and the exposed value has been rotated (regenerated) because the old one is public. Steps: Move the key's value into a server-only environment variable; Remove the literal value from the code and any client-side bundle (the spot the scan shows); Make sure the .env file is in .gitignore; Rotate (regenerate) the key at the provider — assume the old one is compromised. Then tell me exactly what you changed, and do not print any secret values back to me.
Goal: The key is stored only in a server-side secret / environment variable, never in code or the client bundle, and the exposed value has been rotated (regenerated) because the old one is public. Steps: Move the key's value into a server-only environment variable; Remove the literal value from the code and any client-side bundle (the spot the scan shows); Make sure the .env file is in .gitignore; Rotate (regenerate) the key at the provider — assume the old one is compromised.
Then verify it worked
After you deploy the fix, xlogs re-scans your live app and your files and confirms the key is no longer present.
