← Guides

Developer Guide

JWT for API Authorization

Authorize REST calls with Bearer JWTs, scopes, aud, and iss. Covers gateway middleware, why stateless tokens are hard to revoke, and local inspection.

jwt api authorizationauthorization bearer jwtjwt scopes rolesjwt audience issuerstateless jwt revocation

An API gateway does not log anyone in. It receives a request that already claims to be someone, usually via Authorization: Bearer <jwt>, and decides whether that caller may hit GET /v1/invoices. The token is a set of claims — sub, scope, aud, iss — plus a signature the gateway can check without calling the user database on every hop.

That is authorization, not the login cookie discussion. Here the client is often another service, a mobile app, or a SPA sending a header. The failure mode is a 401 or 403, not a redirect to /login.

Authorization: Bearer

The header is Authorization: Bearer plus the compact JWT, one space, no quotes. Some callers percent-encode the token because they built the header like a query string; do not. If a token must travel in a query parameter (websocket handshake, signed download), URL-encode it with /tools/url-encoder (encodeURIComponent) and still prefer the header when you can.

Gateways should reject missing headers, extra commas, and Bearer with nothing after it before they touch Base64url parsing. Decode a captured token on /tools/jwt-debugger when a client swears it sent the same string your logs truncated.

Scopes and roles in claims

OAuth-style scope is a space-delimited string ("invoices:read invoices:write"). Role-style claims are often an array ("roles": ["billing_admin"]). Pick one convention per token audience and document it. Middleware maps the route to a required scope, then checks membership — it should not parse English out of sub.

Keep scopes coarse enough that you are not shipping a novel, and fine enough that invoices:write is not implied by invoices:read. Changing a user’s scopes mid-flight will not affect JWTs already issued; that is the revoke problem below. Pretty-print claim JSON in /tools/json-formatter when you design the contract.

Audience and issuer

iss names the auth server. aud names the API (a URI or an application id). A token minted for the billing API must not open the admin API just because the signature is valid. Verify both claims on every request, using an allow-list, not a boolean “signature OK”.

azp / client_id matter when one issuer serves many OAuth clients. Clock skew of a minute or two is common; nbf in the future shows a not-yet-valid badge in /tools/jwt-debugger after mount, which is how you tell skew from a bad secret.

Stateless JWTs are hard to revoke

Once signed, a JWT is valid until exp unless every gateway consults a denylist or you rotate signing keys. That is the trade you made for skipping a session lookup. For “log out everywhere” and “contractor offboarded”, keep access tokens short and store refresh tokens (or a session row) where you can delete them.

A denylist of jti values works if you issue jti and if every replica checks it. At that point you have a small database again — which is fine, just be honest that the design is no longer purely stateless.

Gateway middleware pattern

Typical pipeline: require Authorization, parse Bearer, verify signature (HS256 or RS256 JWKS), assert iss / aud / exp / nbf, attach sub and scopes, then enforce the route’s scope. Fail closed: 401 for bad tokens, 403 for missing scope.

Do not implement crypto in the handler, and do not log full tokens. Decode a 401 on /tools/jwt-debugger (HS256 verify only; RS256 is decode-only) and format claims in /tools/json-formatter.

Common pitfalls

Checking only the signature and ignoring aud is how a token from the staging issuer opens production if they share a key, or how a token meant for one API opens another. Treating scope as a single string equality check fails when the caller sends two scopes.

Putting JWTs in query strings leaks them to access logs. If you must, encode with /tools/url-encoder and expire them in minutes. A decoded payload is not a verified one.

  • 401 = not authenticated; 403 = authenticated but not allowed.
  • aud and iss are part of authorization, not optional metadata.
  • /tools/jwt-debugger does not verify RS256 / ES256.

Worked examples

HTTP request to a protected route

GET /v1/invoices HTTP/1.1
Host: api.example.com
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VyXzQyIiwic2NvcGUiOiJpbnZvaWNlczpyZWFkIiwiaXNzIjoiaHR0cHM6Ly9hdXRoLmV4YW1wbGUuY29tIiwiYXVkIjoiaHR0cHM6Ly9hcGkuZXhhbXBsZS5jb20iLCJleHAiOjE3MDAwMDM2MDB9.signature

The third part is a placeholder. Paste a real token into /tools/jwt-debugger rather than trusting a blog signature.

Authorization claims

{
  "sub": "user_42",
  "scope": "invoices:read invoices:write",
  "iss": "https://auth.example.com",
  "aud": "https://api.example.com",
  "exp": 1700003600
}

Format this in /tools/json-formatter. exp 1700003600 is 2023-11-14 23:13:20 UTC.

Frequently asked questions

Should APIs look up the user on every request?

Not for identity if the JWT is verified and exp is short. Do look up anything that must be current — billing holds, feature kills, or scopes you refuse to freeze in the token.

How do I revoke a Bearer JWT?

You cannot undo a signature. Shorten exp, revoke refresh tokens, rotate keys, or check a jti denylist on the gateway.

Can I put the JWT in a query parameter?

Avoid it. If a client cannot set headers, percent-encode with /tools/url-encoder and keep exp measured in minutes.

Does the debugger upload API tokens?

No. /tools/jwt-debugger decodes in the browser and verifies HS256 with a secret you type locally. Other algorithms are not verified.

Summary

Protect routes with Authorization: Bearer, require iss / aud / exp, and map scope in gateway middleware. Stateless tokens will not vanish on logout unless you planned for that. Inspect claims on /tools/jwt-debugger and /tools/json-formatter; encode a query-string token on /tools/url-encoder only as a last resort.

Try it yourself

Put what you've learned into practice with our free browser-native tool.

Open Tool →