Web Development & EngineeringPractitioner Q&A Guide
5. How do you keep API keys and secrets safe in production?
Direct Answer
Keep secrets in environment variables on the server, never in code, never in the browser, and never in a repository. Rotate any secret that has ever been exposed.
Full Explanation
The rule is simple: if a secret appears in your codebase, in a screenshot, or in client-side JavaScript, it is compromised. Server-only environment variables exist for exactly this reason — the server can read them, the browser cannot. Repositories deserve special care: a key committed once lives in history forever, even if you delete it later.
Practical Steps
Store all keys in your hosting provider's environment settings, marked server-only. Access them only in server code, never in client components. Use different keys for development and production. Add a pre-commit check that blocks secret patterns. If a key leaks, revoke and replace it immediately — do not just delete it from the code.
Realistic Example
A developer accidentally committed a payment API key to a public repository. Within hours it was abused. The fix was not deleting the commit — it was revoking the key, issuing a new one, and adding secret scanning so it could not happen again.
Common Mistake to Avoid
Prefixing a secret with a public marker or embedding it in frontend code "temporarily." Temporary secrets in client code become permanent leaks.
Parent Topic
Explore the Complete Full-Stack APIs, Databases & Architecture Curriculum
This guide is part of our comprehensive educational track at Abdul Subhan Academy. Explore structured lessons, actionable execution steps, and industry best practices.
Keep Learning
Related Questions in Full-Stack APIs, Databases & Architecture
Web Development & Engineering
Read Answer1. What is an API and how does it work in simple terms?
Web Development & Engineering
Read Answer4. What is authentication vs authorization, and how do you implement both?
Web Development & Engineering
Read Answer