Part 8 of 10 · Security

API keys, OAuth client credentials or mTLS: how should services authenticate?

API keys are simple and fine for low-risk public APIs if they're hashed, scoped and rotatable. OAuth client credentials give short-lived, scoped tokens and central control. Mutual TLS proves identity at the connection level and suits internal service meshes. Signed requests (HMAC) protect…

Yellow network cables with RJ45 plugs, services talking to each other

Short answer: the verdict depends on who’s knocking, and we’ll see each one earned:

  • Public API for customers’ scripts and integrations: API keys, done properly (hashed, scoped, prefixed, rotatable), or OAuth client credentials if customers need fine-grained, short-lived access.
  • Your own services talking to each other: mTLS (often via a service mesh) for identity on the connection, plus short-lived tokens for user context if needed.
  • Third-party integrations acting for a user: OAuth authorization code, never the user’s password or a shared admin key.
  • Webhooks you send or receive: signed requests (HMAC or signatures) with timestamps.

Here’s the puzzle. A request arrives at your API, and there’s no human on the other end to type a password. Something claims to be a customer’s script, or your own orders service, or an email provider reporting a bounce. How do you know it’s telling the truth? A service must prove who it is some other way, and each answer below uses a different proof: a stored secret, a short-lived token, a certificate or a signature.

Case file

The case A request arrives with no human to type a password: is the caller who it claims to be?
Suspects API keys, OAuth client credentials, mTLS, signed requests (HMAC)
Culprit Trusting the network: “it’s internal” isn’t authentication
The fix Hashed, scoped API keys for public APIs, mTLS inside, HMAC signatures for webhooks

The suspects at a glance #

API key OAuth client credentials mTLS Signed requests (HMAC)
What proves identity A long random string in a header A client authenticates, receives a short-lived token A client certificate checked in the TLS handshake A signature over the request with a shared secret
Lifetime of what’s sent Long (until rotated) Minutes Certificate lifetime (hours to days with automation) Per request
Revocation Delete the key Disable the client; tokens expire soon Revoke or let short certs expire Rotate the secret
Replay if intercepted Yes, until revoked Yes, until the token expires No (bound to the TLS session) No, with timestamp + nonce checks
Effort Lowest Medium (an auth server) Highest (a PKI, cert rotation), lower with a mesh Low to medium
Good for Simple public APIs, internal scripts Partner and enterprise APIs Internal service mesh, high-security B2B Webhooks, AWS-style APIs

The row to watch is “Replay if intercepted”. It’s the question every investigation below keeps coming back to: if someone overhears the proof, can they use it again?

Case 1: a public API #

API keys get dismissed as the unsophisticated option. That’s a false lead: a well-designed one is a small joy. What I’d use: API keys, designed like credentials, not like IDs.

  • Generate 256 random bits, show the key once, and store only its hash (SHA-256 is fine: the key is random, not a password; the password post explains why passwords need more).
  • Prefix keys (sk_live_…, sk_test_…) so secret scanners (GitHub’s, GitGuardian, trufflehog) can spot leaked ones, and so test keys can’t touch live data. A leaked key that announces itself is a clue left for the good guys.
  • Scope and expire them: per-key permissions, optional expiry, last-used timestamps shown to the customer.
  • Multiple keys per account so customers can rotate without downtime: create new, deploy, delete old.
  • Rate-limit per key and log usage.

When customers need delegated, fine-grained or short-lived access, add OAuth client credentials (see the OAuth flows post): the customer’s system exchanges its credentials for a token with the scopes it needs.

Case 2: internal microservices #

The usual suspect here is the network itself: “it came from inside the VPC, so it’s fine”. What I’d use instead: mTLS for service identity, plus authorisation rules per service.

Sequence diagram: cert: spiffe://prod/orders, cert: spiffe://prod/payments, + short-lived JWT carrying the end user, if anySequence diagram: cert: spiffe://prod/orders, cert: spiffe://prod/payments, + short-lived JWT carrying the end user, if any
Diagram source
sequenceDiagram
  participant A as orders service (cert: spiffe://prod/orders)
  participant B as payments service (cert: spiffe://prod/payments)
  A->>B: TLS handshake, both present certificates from the internal CA
  B->>B: verify A's cert chain#59; policy: orders may call POST /charges
  A->>B: POST /charges (+ short-lived JWT carrying the end user, if any)
  B-->>A: 201

The reveal is a lovely one. What mTLS gets right is that identity becomes a property of the connection itself: there’s no token to steal from a header, because proof happens in the handshake.

  • A service mesh (Istio, Linkerd) or SPIFFE/SPIRE issues short-lived certificates automatically and rotates them, which removes most of the pain of running your own PKI.
  • Authorise by service identity: “orders may call payments’ POST /charges”, not “anything inside the VPC is trusted”.
  • If the downstream service needs to know which user a call is for, pass a short-lived token minted at the edge (sessions or JWTs post).

Case 3: webhooks #

HMAC signing is almost too simple to be this effective: only someone holding the shared secret can sign this exact body at this exact time. It’s the kind of solution you admire for how little it needs.

Sending: sign each delivery with HMAC-SHA256 over timestamp + "." + body using a per-endpoint secret; include the timestamp and signature in headers; let customers rotate the secret with an overlap period.

Receiving: verify the signature with a constant-time comparison, reject timestamps older than a few minutes (replays), store the event ID to ignore duplicates, and respond quickly, doing the work from a queue. The blogging platform I run verifies every email provider’s webhook this way before trusting a bounce or complaint.

False leads to avoid #

  • Keys in URLs (?api_key=…): they end up in logs, browser history and Referer headers. Use a header.
  • One all-powerful shared key across services or customers: impossible to rotate or attribute.
  • Long-lived cloud credentials in CI: use OIDC federation (GitHub Actions to AWS, for example) to get short-lived credentials per job.
  • Trusting the network: “it’s internal” isn’t authentication. Assume an attacker is already inside one service.

Remember this

  • Ask what the caller can prove right now, not where it sits on the network.
  • Treat API keys as credentials: hashed, prefixed, scoped, rotatable.
  • Sign webhooks with a timestamp so overheard requests can’t be replayed.

Case closed: what I’d do by default #

  1. Public API: hashed, prefixed, scoped API keys; OAuth client credentials for enterprise needs.
  2. Internal: mTLS via a mesh or SPIFFE, authorisation per service.
  3. Webhooks: HMAC signatures with timestamps, idempotent handling.
  4. No secrets in URLs or code; short-lived credentials wherever possible.

What ties all four cases together is a thoughtful shift: from “who’s allowed on the network?” to “what can this caller prove, right now?” Once you start asking the second question, the right mechanism for each case tends to suggest itself.

Like (sign in first) 0 likes 💬 0

Written by

Vishal Kumar

Associate Director at CapGrid, running product and engineering in a CPTO capacity. 10+ years building distributed, multi-tenant SaaS and AI platforms. AWS Certified Solutions Architect - Professional. Based in Gurugram.

Comments

No comments yet.

Sign in to comment. We'll email you a link; no password needed.