AI and network security

What happens when an API key leaks?

When an API key leaks, anyone who finds it can call the service as you, with every permission the key carries, until the key is revoked. Depending on the key, that means reading or changing data, running up usage bills, or using your account to reach other systems. Deleting the commit or file does not fix it; only revoking the key does.

TL;DR

A leaked API key works for anyone who finds it until you revoke it, and removing it from a repository or chat does not stop copies already taken. Revoke or replace it first, then check the logs, scope the replacement and store it in a secret manager. Long term, replace long-lived shared secrets with short-lived credentials and device-held keys.

Learning objectives

After reading this article you will be able to:

  • Describe what an attacker can do with a leaked API key and why deleting it from code is not enough
  • Respond to a leak in order, from revocation and replacement to log review and clean-up
  • Reduce future exposure with scoped, short-lived credentials and fewer shared secrets

Leaked API key risks

Someone with a leaked API key can do anything the key allows, from anywhere, until it is revoked. An API key is a bearer credential: the service checks that the key is valid, not who is holding it. What that means depends on the key:

  • A cloud provider key can start machines, read storage, change permissions and run up a bill.
  • A payment, email or messaging key can send money, mail or texts in your name.
  • A database or SaaS key can read and export customer data.
  • An AI provider key can run requests charged to your account.
  • A key held by an AI agent or integration can do everything that tool can do, which is often more than anyone remembers granting.

And a leak is rarely noticed at once. GitGuardian’s 2025 report found that 70% of the secrets it saw leaked in 2022 were still valid when it checked, years later.

First-hour API key leak response

Respond to a key leak in this order. Speed on the first step matters more than tidiness on the rest.

  1. Revoke or replace the key. If the service lets you revoke at once, do it. If you need the integration to keep working, create a replacement, deploy it, confirm the new key works, then delete the old one. OWASP’s remediation guidance puts revocation first: exposed keys should undergo immediate revocation.
  2. Check what it was used for. Look at the provider’s access and usage logs from the moment of exposure onward. Look for unfamiliar IP addresses, new resources, permission changes, exports and spikes in usage.
  3. Contain anything it touched. If the key could create users, keys or machines, look for new ones and remove them. If it could read data, treat that data as exposed and follow your incident and notification process.
  4. Remove the secret from where it leaked. Delete it from the file, chat or ticket, and rewrite repository history if needed. Do this after revocation, because copies may already exist.
  5. Find the cause. Work out whether it was committed by mistake, printed in a log, pasted into an AI tool, or shipped in an app. Fix the habit, not only the instance.

If the key was in a public GitHub repository, GitHub’s secret scanning runs automatically on public repositories and notifies participating providers so they can act, such as revoking the credential. Treat that as a safety net, not a plan.

Key scoping and short-lived credentials

The next leak matters less when every key is smaller, shorter-lived and rarer.

  • Scope each key to one job. A key that can only read one resource is a small incident. A key with full access to everything is a breach.
  • Prefer short-lived credentials. AWS recommends temporary credentials over long-term access keys wherever possible, and IAM roles for workloads so no long-lived key is distributed at all. OWASP describes dynamic secrets that are generated per session and expire on their own.
  • Keep keys in a secret manager. Not in code, config files, chat or tickets.
  • Remove keys nobody uses. AWS’s guidance is to review last-used information and delete unused credentials regularly.
  • Never ship a broad key in a client. A key inside a browser bundle or a mobile app can be read by anyone with the app.

Fewer long-lived shared secrets

A secret that does not exist cannot leak. Every long-lived key is a standing way in, copied into every place it is configured. A device-issued identity replaces the shared secret with a key pair whose private half never leaves the device: the device proves who it is with a signature over a fresh challenge, so there is nothing to paste into a repository and nothing to replay.

Offline Protocol is designed around that idea. Devices identify each other with Ed25519 keys they generate and hold themselves, sessions are end-to-end encrypted with MLS, and a local mesh needs no portal account and no API key, so the device-to-device side of an application carries no shared secret at all. An app API key is needed only for hosted services: hosted MCP, SDK telemetry and CLI use in CI. Each of those keys belongs to one application and can be limited to one product and one access level, and the portal stores only a hash of each one, so a lost key is replaced, never recovered. Deleting a key revokes hosted access without touching device identity or local records, so a leak in the cloud never takes a site’s local coordination down with it.

Frequently asked questions

Is deleting the commit enough after leaking a key?

No. Anyone who saw or cloned it still has the key, and it keeps working until the provider revokes it. Revoke first, then clean up the history.

Will the provider revoke a leaked key for me?

Sometimes. GitHub scans public repositories and notifies participating providers when it finds their secrets, so they can act, such as revoking the credential. Do not rely on it; revoke the key yourself.

How do I know if a leaked key was used?

Check the provider's usage and access logs for the period since the leak, looking for calls from unfamiliar addresses, at odd hours, or to resources the key's owner never touches. Unexpected charges are often the first sign.

Should I rotate API keys on a schedule?

Yes, for keys that must stay long-lived. OWASP recommends regular rotation so a stolen credential only works for a short time, and AWS recommends temporary credentials over long-term keys wherever possible.

What should I do if a key was embedded in a mobile app?

Treat it as public, because anyone with the app can extract it. Scope it to the least it needs, set usage limits, monitor it, and replace it in a new app release if it is abused.

Sources

Build it with Offline Protocol

The applications and API keys guide explains which Offline Protocol services need a key, how to scope one to a single product, and how to replace and delete a key that was exposed.

Read the API keys guide