How to Securely Store API Keys and Secrets

    6 min read
    ByDaniel AKraftwire Software
    Illustration for How to Securely Store API Keys and Secrets

    Sources

    Every figure above was checked against the page it cites · here they are, so you can check them too.

    Show 1 source
    1. quora.com › What-are-the-best-practices-for-storing-API-keys

      Storing API keys securely requires minimizing exposure, limiting scope and lifetime, and using controlled secrets storage rather than embedding keys in code or public assets.

    Frequently asked questions

    What is the most secure way to store API keys in a web application?
    Never hardcode API keys in source code or commit them to version control. Instead, use environment variables stored in a .env file (which must be added to your .gitignore) or use a dedicated secrets management service like HashiCorp Vault or AWS Secrets Manager to inject keys at runtime.
    What should I do if I accidentally commit an API key to GitHub?
    If an API key is leaked, you must revoke it immediately through the provider's dashboard. After revocation, generate a new key, update your application's environment variables or secrets manager, and audit your logs for any unauthorized activity that occurred while the key was exposed.
    Are environment variables secure enough for production secrets?
    Environment variables are safer than hardcoding because they keep secrets out of the codebase. However, they can still be exposed through server logs or process dumps. For high-security production environments, a dedicated secrets manager is preferred over simple environment variables because it offers better auditing and rotation features.
    Is it safe to store API keys in a note-taking app?
    Standard notes apps like Apple Notes or Google Keep often store data in a way that the provider can access. For sensitive developer data, use a client-side encrypted app like SimplyBoard. This ensures keys are encrypted locally on your device before being synced, meaning only you hold the decryption key.
    Why is API key rotation important for security?
    Key rotation is the practice of periodically replacing old API keys with new ones. This limits the amount of time a leaked key is useful to an attacker. Many organizations rotate keys every 30 to 90 days, or immediately upon any change in personnel who had access to the keys.
    How does the Principle of Least Privilege apply to API keys?
    The Principle of Least Privilege means giving an API key only the minimum permissions it needs to perform its task. For example, if a key only needs to upload files to an S3 bucket, it should not have permission to delete files or list other buckets in the account.

    Start writing with true privacy

    Get Started Free

    Related reading