Prove who is calling and that the message was not changed, using certificates or signatures.
Two ways to prove who is calling an API, and that the message was not changed on the way, without relying on a password or a key sent in the open.
An API key says “I am app X”. But a key is just a string. If someone copies it, they are app X. And the key does not say anything about the message: someone in the middle could change the body and the server would not notice.
You already know TLS from the SSL/TLS topic: the server shows a certificate, signed by a trusted CA (certificate authority), and the browser checks it. With mTLS the server also says “now show me yours”. The client sends its own certificate and proves it owns the matching private key. The server checks that the certificate was issued by a CA it trusts.
With HMAC signing, both sides know a secret key. The client builds a fixed text from the request (the method, the path, the query, a fingerprint of the body, and the time). It runs this text and the key through an HMAC function and sends the result as a header. The key itself is never sent. The server builds the same text from what it received, computes the HMAC with its own copy of the key, and compares.
Text to sign (lines joined with newlines): POST /v1/payments amount=100¤cy=USD <- query sorted by name 2026-12-03T10:15:00Z <- timestamp n-7f3a <- one-time nonce <fingerprint of the body> <- a hash of the exact bytes Signature = HMAC(secret key, text to sign) Headers sent: X-Key-Id: key-123 <- tells the server which secret to use X-Timestamp: 2026-12-03T10:15:00Z X-Nonce: n-7f3a X-Signature: <the signature>
| Way | What the server learns | If it is stolen | Protects the body? |
|---|---|---|---|
| API key | Which app the caller claims to be | The thief is the app, until the key is revoked | No |
| HMAC signature | The caller holds the shared secret, and the request is unchanged | One request is copied. It dies after the time window | Yes |
| mTLS client certificate | Exactly which service connected, backed by a CA | Needs the private key. Short lifetimes limit the damage | Yes, plus encryption |
Someone on the way changes the amount from 100 to 9000 and the account number.
POST /v1/payments 1000 n-7f3a 024f618d
POST /v1/payments 1000 n-7f3a 8d060f17
The signature here comes from a small toy hash so the demo is repeatable. It is NOT real cryptography. A real system uses a proper HMAC (for example HMAC with SHA-256), a secret key and a constant-time comparison. Times and numbers are illustrative.
| Term | Meaning in easy words |
|---|---|
| mTLS | Mutual TLS: both the client and the server show certificates and check each other |
| Certificate | A signed document that ties a name to a public key |
| CA (certificate authority) | A trusted body that signs certificates. A company can run a private CA |
| Private key | The secret half of a key pair. Whoever holds it can prove the identity |
| HMAC | A keyed hash: a signature made from a message and a shared secret key |
| Canonical string | The exact, agreed text of the request that gets signed |
| Nonce | A number used once. Remembering it stops a copied request being replayed |
| Replay attack | Sending a valid, recorded request again later |
| Constant-time compare | Comparing two values in the same time wherever they differ, so timing leaks nothing |
| Key rotation | Replacing keys or certificates on a schedule, or after a leak |
| Revocation | Cancelling a certificate before its date runs out |
| Choice | We gain | We lose |
|---|---|---|
| mTLS instead of API keys | Strong identity at the connection | Certificate management work |
| HMAC instead of mTLS | Works through proxies that end TLS, and per message proof | Both sides share a secret, and you handle replays yourself |
| Short certificate lifetime | A stolen key stops working soon | Needs reliable automatic renewal |
| Wide time window | Tolerates clock differences | A longer replay window |
| Signing the whole body | Any change is caught | The body must be read fully before it is checked |