Resources / Guides
API authentication methods: API keys vs OAuth 2.0 vs JWT vs HMAC
The main ways to authenticate API requests — API keys, OAuth 2.0, JWTs, HMAC signatures and mTLS — how each works, and which to choose.
Every API needs to answer two questions on each request: who is calling (authentication) and are they allowed to do this (authorization). These are the common ways to answer the first one.
API keys
A long random string the client sends with every request, usually in a header:
Authorization: Bearer sk_live_51Hx...
x-api-key: sk_live_51Hx...
- Good for: server-to-server calls, developer APIs, internal services, AI agents calling tools.
- Strengths: simple to issue, use and revoke; one key per customer makes usage and limits easy to track.
- Watch out: a key is a password. Never put it in browser or mobile code; store only a hash server-side; support expiry and rotation; scope keys to what they need.
OAuth 2.0
A framework where a user delegates access to an app without giving it their password. The app gets an access token (short-lived) and often a refresh token.
- Good for: "Sign in with…", apps acting on behalf of users, third-party integrations (Slack, GitHub, Google).
- Strengths: users consent to scopes; tokens expire; access can be revoked per app.
- Watch out: more moving parts — redirect URIs, PKCE for public clients, token storage and refresh. Use a well-tested library or identity provider.
JWT (JSON Web Tokens)
A signed token format: header.payload.signature. The server can verify it without a database lookup, because the signature proves who issued it.
- Good for: access tokens between your own services, short-lived session tokens, tokens issued by an identity provider.
- Strengths: stateless verification; carries claims (user id, scopes, expiry).
- Watch out: you can't easily revoke a JWT before it expires — keep lifetimes short. Always verify the signature and algorithm (never accept
alg: none), and checkexp,issandaud.
JWT is a format, not a method — OAuth often uses JWTs as access tokens.
HMAC request signatures
The client signs each request (usually timestamp + body) with a shared secret, and sends the signature. The server recomputes it.
- Good for: webhooks (Stripe, GitHub and Shopify all sign this way) and high-security APIs.
- Strengths: the secret never travels over the wire; tampering and replays are detectable.
- Watch out: you must verify against the raw body and check the timestamp. See Stripe webhook signature verification.
Mutual TLS (mTLS)
Both sides present certificates. Strong, but heavy to operate — mostly for regulated industries and service meshes.
Which one to choose
| Scenario | Use |
|---|---|
| Customers' servers call your API | API keys (per customer, scoped, revocable) |
| Your app acts on behalf of users of another service | OAuth 2.0 |
| Users sign in to your own app | Sessions or OAuth/OIDC via an identity provider |
| Services you own call each other | Short-lived JWTs or mTLS |
| Receiving events from another service | HMAC signature verification |
| AI agents calling your tools | API keys or OAuth, scoped per tool |
Whatever you pick: always use HTTPS, rate-limit per credential (see API rate limit best practices), log which credential made each call, and make revocation instant.
Authentication on Wisp
Each Wisp project can require API keys — one per customer or app, optionally limited to some endpoints and with an expiry. Callers send Authorization: Bearer … or x-api-key; requests without a valid key get 401, and your code sees which key called in req.apiKey. For anything else — sessions, JWTs from your identity provider, HMAC-signed webhooks — describe it and Wisp writes it as a before function that runs ahead of every HTTP function.
Try it: curl to fetch / Python converter
Build it with Wisp — describe an API, get it live.