API Keys vs JWTs: You Are Authenticating Different Things
An API key identifies an app, a JWT proves a user. They differ in lifetime, revocation, and what a gateway can verify without a database call.
On this page
“Should we use API keys or JWTs?” is usually the wrong question. They answer different questions. One says which application is calling. The other says which human is acting. Mixing them up produces APIs that either cannot revoke access or cannot tell users apart.
Two flows, side by side
The token flow (a signed-in user):
- The user logs in to your frontend.
- Credentials go to an identity service.
- On success it issues a JWT.
- The frontend sends the JWT in the
Authorizationheader on each call. - The API gateway validates signature, expiry and claims.
- If valid, the request is forwarded to the service.
- The service handles it, often using claims like the user ID and scopes.
The API key flow (a third-party developer):
- The developer registers on your portal.
- The portal issues an API key and stores a verifier in a secure key store.
- The developer’s app sends the key in a header on every request.
- The gateway asks a key validation service (or checks the store) whether the key is valid.
- If valid, the request is forwarded to the public API.
Same gateway, very different trust model.
What each one proves
The trade-offs that matter in production
| API key | JWT | |
|---|---|---|
| Lookup per request | Yes (store or cache) | No (signature check) |
| Revocation | Immediate (delete the key) | Waits for expiry unless you add a denylist |
| Typical lifetime | Months or until rotated | Minutes to an hour |
| Carries identity | No | Yes, via claims |
| If leaked | Valid until revoked | Valid until it expires |
| Best for | Server-to-server, developer apps | User sessions, cross-service identity |
Because a gateway accepts a valid JWT without asking anyone, you cannot pull it back mid-flight. Keep access tokens short (minutes), pair them with a refresh token you can revoke, and keep the signing algorithm pinned in your verifier rather than trusting the token’s header.
Handling API keys properly
- Hash them at rest. Store a hash and show the full key once at creation, exactly like a password. A leaked database should not leak working keys.
- Use a visible prefix. Something like
ck_live_plus an ID lets users and secret scanners recognize and report leaked keys. - Header, never URL. Query strings end up in logs, browser history and referrers.
- Scope and rotate. Give keys the minimum permissions and support overlapping rotation so clients can switch without downtime.
Combining them
A common shape: the developer’s app authenticates itself with an API key, so you know who to rate limit and bill. The end user signs in and the app receives a short-lived JWT carrying identity and scopes. The gateway checks both: the key for “is this a known client within quota”, the token for “is this user allowed to do this”.
Do
Match the credential to the question: API keys for client identity and quotas, JWTs for user identity and permissions.
Don't
Put user authorization decisions on a long-lived API key, or issue long-lived JWTs “to avoid refresh complexity”. Both turn a leak into a long incident.
Takeaway
API keys and JWTs are not rivals. One identifies a caller, the other proves a user, and their revocation and lifetime properties follow from that. Decide which question each endpoint is asking, and use both where both questions apply.
Frequently asked questions
What is the core difference between an API key and a JWT?
An API key is an opaque secret that identifies the calling application or project and is looked up server-side. A JWT is a signed token carrying claims such as user ID and scopes that any service with the right key can verify without a lookup. Keys say which app is calling; JWTs say which user is acting.
Why is revoking a JWT harder than revoking an API key?
Because a JWT is self-contained and verified by signature, a gateway accepts it until it expires without asking a database. Revoking early needs extra machinery like a denylist. API keys are checked against a store on each call, so deleting the key takes effect immediately.
How should API keys be stored on the server?
Store only a hash of the key, like a password, and show the full key once at creation. Keep an identifier prefix so users can recognize keys. If your database leaks, hashed keys cannot be used to call your API.
Should I use both in the same system?
Often, yes. Third-party developers get API keys that identify their app for quotas and billing, while end users sign in and receive short-lived JWTs that carry identity and permissions. The key answers who built the client, the token answers who is using it.



/* Comments */
Comments are offline right now — we reconnect automatically, nothing is lost.