Skip to content
Digital Security

// article

How to Store API Keys and Secrets Without Leaking Them

Safer habits for developers who work with API keys, tokens, and secrets on laptops and in repositories.

18 Jun 2026 3 min read
How to Store API Keys and Secrets Without Leaking Them

// statistical data

Real statistics for this topic

Verified sources

API keys, tokens, and secrets leaked in public repositories or insecure storage are an entry path for attackers into production systems.

183 hari

CISA credential exposure duration on public GitHub

In 2025–2026, a CISA contractor left administrative credentials in a public GitHub repository for 183 days. Secret leakage risk spares no organization.

Source: CISA CPG Adoption Report · 2025
83%

CISA stakeholders reduced exploitable services

CISA CPG 2025 report: 83% of stakeholders reduced internet-exposed services. Attack surface management is not optional.

Source: CISA CPG Adoption Report · 2025

Figures are summarized from public reports. Use the source links to review methodology, geography, and reporting period.

An API key looks like a random string, but its value can equal a house key. Applications use it to reach email, payments, storage, or AI services. When a key enters a public repository, CI log, or screenshot, someone else may use it before you notice. This does not happen only because a developer is careless. A local configuration file is convenient and gets committed. A debugging log helps explain an error but prints an Authorization header without masking it. Clear working habits close both paths.

Separate code from secrets

Code can live in a repository. Secrets should arrive through environment variables, a secret manager, or an approved injection method. A local . Env file can help during development, but add it to . Gitignore and provide an . Env. Example containing variable names only. Do not put secrets in comments, branch names, issues, wikis, or commit messages. Removing a line from the latest version does not remove it from Git history. Treat a repository as an archive, not a place for something you want hidden.

Limit each key’s capability

Create separate keys for development, staging, and production. Grant the smallest useful permission set. A key that reads objects does not need to delete a bucket. A key for one service does not need access to every project. Set an expiration when the platform supports it. Rotation becomes easier when applications do not share one key. Record the owner, service, environment, creation date, and revocation method without writing down the secret value.

Protect logs and work tools

Filter headers, URLs, and payloads before they enter logs. Many libraries support redaction. Use synthetic data for debugging. Take care when uploading terminal screenshots or pasting errors into a forum or chatbot. Enable secret scanning in the repository and pipeline. It can find strings that resemble keys, but it is not a replacement for process. When the scanner warns, stop the workflow and inspect the key.

If a key has leaked

Revoke or disable it first. Do not wait to confirm whether someone used it. Create a new key with the same or narrower permissions, install it through a safe path, and review usage logs from the time of exposure. Look for copies in commit history, issues, logs, and developer machines. Remove unnecessary traces according to organisational procedure, but remember that deletion does not replace revocation. Record the impact, time, service, and recovery steps.

A pre-push checklist

  • No token appears in source code or screenshots.
  • The local . Env is not tracked by Git.
  • Production uses an approved secret store.
  • Keys have sensible permissions and lifetimes.
  • Logs do not print sensitive headers or payloads.
  • A tested rotation method exists. A good secret is not one that never changes. It is one you can limit, monitor, and revoke without shutting down the whole system.

About the author

Syukra
SyukraIndependent Cybersecurity Researcher

Saya riset threat intelligence dan hardening. Saya pakai Microsoft DR, Verizon DBIR, FBI IC3, ENISA sebagai sumber primer. Saya uji panduan di perangkat saya.

Comments

comments powered by Disqus