Skip to content
Velloreth
Cybersecurity 5 min read

API Authentication Methods Compared

API keys, JWTs, OAuth and sessions each solve a different problem. Learn how they work, when to use each, and the common mistakes.

By

The four approaches

API keys — a long-lived secret that identifies an application.

Sessions — a server-side record, with a cookie holding an opaque identifier.

Tokens (JWT) — a signed, self-contained claim.

OAuth — a framework for delegated authorization.

They solve different problems, and the right choice depends on who is calling and what they need.

API keys

An API key is a long-lived secret that identifies an application.

Authorization: Bearer sk_live_abc123

Use it for server-to-server calls, where the caller is a trusted service rather than a user.

The advantages: simple to issue, simple to verify, and easy to revoke.

The disadvantages: it does not identify a user, it does not expire by default, and it grants whatever the key is scoped to. A leaked key is a full compromise of that scope.

Best practice: scope the key to the minimum, rotate it periodically, and store only a hash of it so a database leak does not expose the key.

Sessions

A session is a server-side record, and the cookie holds an opaque identifier.

Set-Cookie: session=abc123; HttpOnly; Secure; SameSite=Lax

The advantages: the server can revoke a session immediately, the token carries no data, and the cookie attributes protect it.

The disadvantages: the server must store the session, which is state, and a distributed system needs a shared store.

The cookie attributes matter:

  • HttpOnly — not readable by JavaScript, so an XSS bug cannot steal it.
  • Secure — sent only over HTTPS.
  • SameSite=Lax — not sent on a cross-site POST, which mitigates CSRF.
  • SameSite=Strict — not sent on any cross-site request, which is stronger but breaks an inbound link.

JWTs

A JWT is a signed, self-contained claim.

header.payload.signature

The payload is base64-encoded, not encrypted. Anyone can read it. The signature proves it was not tampered with.

{
  "sub": "user_42",
  "exp": 1735689600,
  "scope": "articles:read"
}

The advantages: the server does not need to store anything, so it scales and works across services. The claims travel with the token.

The disadvantages: it cannot be revoked before it expires, so a stolen token is valid until then. The payload is visible, so it must not contain a secret.

Best practice: a short expiry, a refresh token for a new access token, and a jti claim so a revocation list is possible.

The classic mistake: accepting alg: none, or trusting the algorithm from the token header. The server must pin the expected algorithm.

OAuth

OAuth is an authorization framework for delegated access. It lets an application act on a user’s behalf at another service without the user sharing their password.

The authorization code flow is the one for a web application:

  1. The application redirects the user to the provider.
  2. The user consents.
  3. The provider redirects back with a code.
  4. The application exchanges the code for a token, server-side.

PKCE is required for a public client, such as a mobile app or a single-page application, because it cannot keep a client secret.

OpenID Connect is the authentication layer on top of OAuth. It adds an ID token, which is a JWT with the user’s identity.

The distinction matters: OAuth alone tells you the user granted access; it does not tell you who the user is. Using OAuth for authentication without OpenID Connect is a known source of vulnerabilities.

Choosing

ApproachIdentifiesRevocableBest for
API keyApplicationYesServer-to-server
SessionUserYesServer-rendered app
JWTUser or serviceHardStateless API
OAuthDelegated accessYesThird-party access

A server-rendered application should use a session with an httpOnly cookie. It is the simplest and the most revocable.

A stateless API can use a JWT, with a short expiry and a refresh token.

A server-to-server call should use an API key or a client credentials grant.

A third-party integration should use OAuth with PKCE.

Common mistakes

A token in the query string. It ends up in logs, in browser history, and in a referrer header. Use the Authorization header.

A token in localStorage. Any script on the page can read it, so an XSS bug leaks it. Use an httpOnly cookie.

A JWT with a long expiry. A stolen token is valid until it expires, and there is no revocation. Keep it short.

Trusting the JWT algorithm from the header. Pin the expected algorithm, or an attacker can use alg: none.

A secret in the JWT payload. The payload is readable. Do not put anything sensitive in it.

No TLS. A credential over plain HTTP is a credential given away.

A password stored in plain text. Use a slow hash such as bcrypt, scrypt, or Argon2.

No rate limit on login. A login endpoint without a rate limit is a brute-force target.

Practical guidance

  • Use a session with an httpOnly cookie for a server-rendered app.
  • Use a short-lived JWT for a stateless API.
  • Use an API key for server-to-server.
  • Use OAuth with PKCE for third-party access.
  • Never put a token in a query string.
  • Never store a token in localStorage.
  • Pin the JWT algorithm.
  • Hash passwords with a slow hash.
  • Rate-limit the login endpoint.
  • Always use TLS.

For the related guides, see rest-api-design-principles and rest-api-rate-limiting. For the broader context, encryption-at-rest-vs-transit and zero-trust-for-small-teams.

Quotable summary

An API key identifies an application and a token identifies a user, so they solve different problems. A JWT is a signed claim rather than an encrypted one, so anyone can read its contents, and it cannot be revoked before it expires, which is why a short expiry matters. OAuth is an authorization framework for delegated access rather than an authentication protocol, and using it for authentication without OpenID Connect is a known source of vulnerabilities. Store a session token in an httpOnly, Secure, SameSite cookie rather than localStorage, because localStorage is readable by any script on the page and an XSS bug leaks the token. Always send credentials over TLS and never in a query string, where they end up in logs, browser history, and referrer headers.

Frequently asked questions

What is the difference between authentication and authorization?
Authentication establishes who you are. Authorization determines what you are allowed to do. A 401 means authentication failed; a 403 means authorization failed.
Is a JWT encrypted?
No. A JWT is base64-encoded and signed, so anyone can read the payload. The signature proves it was not tampered with, but it does not hide the contents.
Should I use JWT or sessions?
Sessions are simpler and revocable, and they suit a server-rendered application. A JWT suits a stateless API or a service-to-service call. A JWT is hard to revoke before it expires.
What is the difference between an API key and a bearer token?
An API key is a long-lived credential that identifies an application. A bearer token is usually short-lived and identifies a user or a session. An API key is for a server-to-server call, a token for a user request.
Where should I store a token in a browser?
In an httpOnly, Secure, SameSite cookie. localStorage is readable by any script on the page, so an XSS bug leaks the token.
What is OAuth for?
Delegated authorization: letting an application act on a user's behalf at another service without the user sharing their password. It is not an authentication protocol, though it is often used to build one.