Short answer: Never paste an API key into a chat, a ticket or a public repository. Keep keys in environment variables or a secrets manager, give each key the smallest permissions it needs, and rotate any key that may have leaked.
API keys connect your apps to services such as AI models, payment systems and cloud storage. Whoever has the key can use the service as you, and in many cases run up your bill. Keys leak far more often through careless copying than through clever attacks.
Where keys leak
- Chats and prompts. Pasting code or logs that contain a key into an AI assistant, a forum or a support chat.
- Public code. A key committed to a Git repository, even briefly. Automated scanners look for them.
- Screenshots and screen shares. A terminal or settings page with a key visible.
- Front-end code. A key placed in browser JavaScript is visible to every visitor.
- Shared documents and messages. Keys pasted into a team chat or a spreadsheet.
Keep keys out of your code
- Store keys in environment variables or a secrets manager, and read them at run time.
- Put your local environment file (often named
.env) in.gitignoreso it is never committed. - Use a server-side call for anything secret. If a browser needs the service, your server should make the call and hide the key.
Limit what each key can do
- Create separate keys for each app and each environment (testing and production).
- Give each key the least permission it needs, and set spending limits or quotas where the provider allows.
- Restrict keys by IP address or domain if the service supports it.
Before you paste anything into an AI tool
Search your text for strings that look like keys, tokens and passwords, and replace them with a placeholder such as YOUR_API_KEY. Check our privacy checklist for AI tools for what else should stay out of prompts.
If a key leaks
- Revoke or rotate it immediately in the provider's dashboard. Deleting the message is not enough.
- Check usage logs for activity you do not recognize.
- Remove it from Git history if it was committed, using your host's guidance, and treat it as exposed anyway.
- Create a new key and update your apps.
- Turn on two-factor authentication for the provider account.
A quick team policy
- Keys live in one approved place.
- Nobody sends keys by chat or email.
- Keys are rotated on a schedule and whenever someone leaves.
- Code reviews check for hard-coded secrets.
For wider protection, see the small business cybersecurity checklist.
Frequently asked questions
Is it safe to put an API key in front-end code?
No. Anything in front-end code can be read by visitors. Keep keys on a server and call the service from there.
How do I know if my key was exposed?
Look for unexpected usage or charges in the provider's dashboard, alerts from the provider, and any place you may have pasted the key. If in doubt, rotate it.
What is the difference between rotating and revoking a key?
Revoking disables a key. Rotating means creating a new key, switching your apps to it, and then revoking the old one.
Discussion
Loading comments...