bigonotes
System DesignAPI Design

API Security

Authentication vs authorization, when to use API keys or JWTs, role-based access control, and rate limiting for APIs.

5 min readLast updated

Every API has to answer two questions about each request: who is making it, and are they allowed to do this? It also has to protect itself from clients that send too much. In most interviews security isn't the focus, so a sentence or two is usually enough. But you should know what those sentences mean.

Authentication vs authorization#

These two words sound alike but answer different questions.

  • Authentication (AuthN) verifies identity: is this request from ana@example.com?
  • Authorization (AuthZ) verifies permission: may Ana cancel this reservation? She can cancel her own reservations, not everyone's.

In an interview, say which endpoints require an authenticated user and that you'd use a JWT (or a session stored server-side) to identify them. Derive the current user from the token, never from a user_id in the request body.

API keys#

To authenticate a request, the server needs a credential it can check. The simplest one is an API key: a long random string that works like a password for an application rather than a person.

  1. Generate a unique key per client, e.g. sk_live_abc123....
  2. Store it (ideally hashed) with that client's permissions and rate limits.
  3. On each request, look up the key and identify the client.

The client sends the key in a header on every call:

GET /restaurants
Authorization: Bearer sk_live_abc123...

They're a good fit for server-to-server calls where you control both sides (the reservation service calling the notification service) and for third-party developers who need programmatic access.

They're almost never right for end users. People shouldn't have to handle long secret strings, and a key has no built-in expiry and says nothing about which person is behind a request, unlike a login session.

JWT (JSON Web Token)#

API keys suit applications. For people who log in, the usual credential is a JWT: a signed JSON object that the server hands out at login and the client sends back on each request. Think of it like a passport: your identity is checked once, then you show the stamped passport, and anyone who trusts the seal accepts it without checking your birth certificate again.

Structure#

A JWT is three base64url-encoded parts joined by dots, header.payload.signature:

Part Contains Example
Header Token type and signing algorithm {"alg": "HS256", "typ": "JWT"}
Payload Claims: user id, roles, expiry {"sub": "12345", "name": "Alex", "admin": true, "exp": 1735689600}
Signature Signature over header and payload, made with the server's key Changes if any byte of the payload changes

The payload is encoded, not encrypted. Anyone can decode and read it, so never put passwords or sensitive data in it. Security comes from the signature: if an attacker flips "admin": false to true, the signature no longer matches and the server rejects the token.

Flow#

Here's how a JWT moves between client and server:

  1. Login: the client sends credentials.
  2. Issue: the server verifies them and signs a JWT. With HS256 it uses a shared secret; with RS256 it uses a private key, and services verify with the public key.
  3. Store: the client keeps the token, preferably in an HttpOnly cookie, or in app storage on mobile.
  4. Use: every request carries Authorization: Bearer <token> (or the cookie).
  5. Verify: the server checks the signature and expiry. No database lookup is needed, because the token itself carries the user's identity.

Why use JWTs#

  • Stateless: there's no session store to share. Any number of servers can verify tokens as long as they have the key, so the API tier scales horizontally.
  • Mobile-friendly: a token is a plain string, so it works the same on iOS, Android and web.
  • Cross-service: one token can be accepted by several related services, which is the basis for single sign-on.

Edge cases#

Carrying identity in the token has costs, too. These are the problems that come with JWTs, and how to handle each:

Problem Mitigation
Token theft Short-lived access tokens (e.g. 15 minutes) plus refresh tokens
Data leakage Keep payloads lean, with no secrets or personal identifiers like SSNs
XSS stealing the token Store it in an HttpOnly cookie that JavaScript can't read
Revoking access Tokens are valid until they expire, so keep a denylist (e.g. in Redis) of revoked token ids for immediate logout

Rule of thumb: use API keys for internal services and external developers, and JWTs for user sessions in web and mobile apps.

Role-based access control (RBAC)#

Authentication tells you who the user is, and role-based access control is a common way to decide what they're allowed to do. Real systems have different kinds of users. In a restaurant reservation app, diners reserve tables and see their own reservations, restaurant managers edit their menus and see the reservations for their restaurants, and admins can do everything. RBAC gives permissions to roles and roles to users, instead of granting permissions to each person.

Role Permissions
diner Reserve tables, view own reservations
restaurant_manager Edit menus, view reservations for own restaurants
admin Everything

Each user is then assigned a role:

User Role
ana@example.com diner
owner@lagoa-bistro.com restaurant_manager

Each request then checks both. For GET /reservations/{id}:

  1. Authenticated? The request carries a valid JWT.
  2. Authorized? The user made this reservation, or the user's role is admin.

In an interview it's usually enough to say which roles can call which endpoints, and often roles don't come up at all.

Rate limiting#

The last concern from the opening is clients that send too much. Rate limiting puts a ceiling on requests per client over a stretch of time. It protects against attacks, scrapers and accidental overload.

  • Per user: e.g. 1,000 requests per hour for each authenticated user.
  • Per IP: e.g. 100 requests per hour for unauthenticated traffic.
  • Per endpoint: e.g. 10 reservation attempts per minute, so bots can't grab every prime-time table.

Enforce limits at the API gateway or in middleware, and return 429 Too Many Requests when a limit is hit. Unless you're asked, don't design the algorithm (token bucket, sliding window). Saying "we'll rate limit at the gateway to prevent abuse" is usually enough.

Key takeaways#

  • Authentication answers "who are you?"; authorization answers "are you allowed?".
  • Use API keys for services and third-party developers, and JWTs for user sessions.
  • JWTs are signed, not encrypted. Keep them short-lived and stored in HttpOnly cookies, and plan for revocation.
  • RBAC grants permissions to roles, not individuals.
  • Rate limit at the gateway and return 429.
Revision notes · 2 min

Core idea#

Every API answers two questions per request: who is making it, and are they allowed? It must also protect itself from clients that send too much.

Key points#

  • Authentication (AuthN) verifies identity. Authorization (AuthZ) verifies permission.
  • Derive the current user from the token, never from a user_id in the body.
  • An API key is a long random string that acts as a password for an application.
  • Store API keys hashed, with each client's permissions and rate limits.
  • A JWT is a signed JSON object: header.payload.signature.
  • The JWT payload is encoded, not encrypted. Anyone can read it.
  • The signature stops tampering. A changed payload no longer matches its signature.

API keys vs JWTs#

API keys JWTs
Use for Internal services, third-party developers User sessions in web and mobile apps
Expiry None Built-in exp claim
Verification Look up the key Check signature and expiry, no database lookup
  • JWTs are stateless, so any server with the key can verify them. The API tier scales horizontally.
  • HS256 signs with a shared secret. RS256 signs with a private key, and services verify with the public key.

JWT edge cases#

Problem Mitigation
Token theft Short-lived access tokens (15 minutes) plus refresh tokens
Data leakage Lean payloads, no secrets or personal identifiers
XSS steals the token Store it in an HttpOnly cookie
Revoking access Denylist of revoked token ids, for example in Redis

RBAC and rate limiting#

  • RBAC grants permissions to roles, and roles to users.
  • Check both: is the request authenticated, and does the user own the resource or hold the right role?
  • Rate limit per user, per IP or per endpoint.
  • Enforce limits at the API gateway or in middleware. Return 429 Too Many Requests.

Pitfalls#

  • Putting passwords or sensitive data in a JWT payload.
  • Giving API keys to end users. Keys do not expire or carry user context.
  • Designing the rate-limit algorithm when nobody asked.

Say it in an interview#

  • "These endpoints need an authenticated user. A JWT identifies them."
  • "API keys for services, JWTs for users."
  • "We rate limit at the gateway to prevent abuse."

Found a mistake? Email ask@bigonotes.com