Safeguard
Security Guides•

How do you detect API key misuse from your API logs?

Log a key ID on every call, never the key, then watch for new sources, new endpoints, bursts and errors. A baseline method with OWASP REST guidance.

Safeguard Security Team
4 min read

Log a key identifier, never the key itself, on every API call along with source, endpoint, status and volume. Build a simple baseline for each key, then alert on use from a new network, endpoints the key never touched, sudden bursts and repeated authentication errors.

Why keys need watching

An API key is a long lived password for a program. It gets pasted into scripts, committed to repositories and left in logs. The OWASP REST Security Cheat Sheet says keys issued to third parties are relatively easy to compromise, and that they should not be the only protection for sensitive or high value resources. It advises revoking a key if the client breaks the usage agreement.

When a key leaks, the first sign is often odd traffic, not an alert from the key holder.

What to log per request

  • Key ID or client ID, a stable label that is not the secret.
  • Tenant that owns the key.
  • Verified source IP. See which client IP to log behind a proxy.
  • Method and route template, not the raw URL.
  • Status code and response size.
  • Duration and request ID.

The REST cheat sheet is firm on one point: passwords, tokens and API keys should not appear in the URL, since they can be captured in web server logs. Accept keys in headers, and strip the header from logs. It also advises logging token validation errors to detect attacks and sanitising log data to prevent log injection.

Baseline per key

For two weeks, compute for each key its usual source networks, endpoints, call rate by hour and error rate. Store the summary, not every event, so it stays cheap.

Detections to build

SignalExample conditionWhat it may mean
New sourceKey seen from an address block it never usedLeaked key or moved workload
New endpointsKey touches routes outside its usual setReconnaissance with a stolen key
BurstRate far above hourly normScraping or data pull
Auth errorsMany failures then a successKey guessing
Dormant key wakesNo use for months, then activityOld key found in an archive
Many 429 responsesRepeated rate limit hitsAbuse or a broken client

The REST cheat sheet says to return 429 Too Many Requests when requests arrive too quickly, so a run of 429 responses is both control output and a signal.

Response plan

  1. Contact the key owner and confirm whether the activity is expected.
  2. If not, revoke the key and issue a new one through a separate channel.
  3. Review what the key accessed during the suspicious window.
  4. Search your own code and logs for the exposed key to find the leak.
  5. Add scoping, expiry and per key limits so the next leak has less reach.

Scoped, short lived keys help most. They make misuse both rarer and easier to spot.

Try it yourself

Click through it at your own pace, or open it full screen.

Quick check

Is it safe to log the first few characters of a key?

A short prefix is a common way to identify a key. Prefer a separate key ID, and keep any prefix too short to help guessing.

What if the traffic is a customer's new server?

Confirm with them. Letting customers register expected networks reduces these false alarms.

Safeguard includes API security scanning in its scan set, alongside dependency and code analysis. Try a free scan at gold.safeguard.sh/scan.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.