0%
API Design Interviews: REST, gRPC, GraphQL, Pagination, Idempotency, Versioning and Webhooks
Authentication, Authorization and Rate Limits
Every API that is not fully public must answer two questions on every request: who is calling, and what are they allowed to do. The first is authentication, the second is authorization, and confusing them is one of the most common mistakes in an API design interview. A third concern, rate limiting, protects the service from callers who are allowed to call but call too much. This lesson covers all three, because together they form most of the security and fairness story of an API, and because interviewers almost always ask about at least one of them in step five of the framework.
Authentication versus authorization
Authentication establishes identity. The caller presents a credential, such as an API key, a token or a certificate, and the server verifies it and concludes that this request comes from a particular user, application or service. Authorization uses that identity to decide whether a particular action on a particular resource is allowed. A request can be perfectly authenticated and still be forbidden, such as a signed in user trying to read another user's invoice. Lesson six mapped these to status codes: 401 when authentication is missing or invalid, 403 when the identity is known but the action is not permitted, and 404 when you prefer not to reveal that the resource exists.
In the interview, keep the two separate in your design. Say where authentication happens, usually once at the gateway or in shared middleware, and say where authorization happens, usually in the service that owns the resource, because only it knows who owns what. A common good answer is that the gateway authenticates and attaches a verified identity to the request, and each service authorizes against its own data.
Who are the callers
Before picking a mechanism, identify the kinds of callers, because each wants something different. Server to server integrations from customers, such as a merchant's backend calling a payments API, usually use API keys or the OAuth client credentials flow. Third party applications acting on behalf of a user, such as a scheduling app reading a user's calendar, need delegated authorization, which is OAuth 2.0. Your own web and mobile apps acting for a signed in user usually use session cookies or OAuth tokens issued by your own identity provider. Internal services calling each other use mutual TLS, service tokens or both. Naming these groups in one sentence during step one sets up the authentication discussion later and shows that the choice follows from the consumers.
API keys
An API key is a long random secret issued to an account or application, sent on every request, usually in the Authorization header as a bearer value or in a custom header such as X-API-Key. Keys are simple to use, simple to implement and ideal for server to server integrations where the caller is a program, not a person. Their weaknesses are equally clear: a key identifies an application, not a user; it is usually long lived; and anyone who copies it has full access until it is revoked.
Designing keys well
Several details separate a careful key design from a naive one. Generate keys with a cryptographically secure random source and enough length, at least 128 bits of entropy. Give them a recognisable prefix that encodes the type and environment, such as sk_live_ for a secret production key and pk_test_ for a publishable test key. Prefixes make keys easy to identify in logs and support tickets, and they let secret scanning services detect keys committed to public repositories and alert you automatically. Never put keys in URLs, because URLs end up in logs, browser history and referrer headers.
On the server, store only a hash of each key, never the key itself, exactly as you would a password. Because keys are long and random, a fast hash such as SHA-256 is acceptable, unlike for human passwords, which need slow hashes. Show the full key to the user only once, at creation, and afterwards display only the prefix and the last four characters. Store alongside the hash the owning account, the scopes or permissions, the creation time, the last used time and an optional expiry.
Scoping and rotating keys
A single all powerful key per account is convenient and dangerous. Better designs let customers create several keys, each with restricted permissions, such as read only access to orders, or write access limited to one resource type, and label them by purpose. If a key used only by a reporting script leaks, the damage is limited to reads. Restricted keys also make audit logs more meaningful, because each key maps to one integration.
Rotation must be possible without downtime. Allow at least two active keys at once, so a customer can create a new key, deploy it, confirm traffic has moved using the last used timestamp, then revoke the old one. Some providers also support rolling a key with an overlap window, where the old key keeps working for a set period. Revocation should take effect quickly everywhere, which matters when keys are cached at the gateway for performance: cache entries need a short lifetime or an invalidation message.
Publishable keys and browser clients
Secret keys must never ship to a browser or mobile app, because anything in client code can be extracted. When a client side component needs to call the API directly, for example to tokenise a card number so it never touches the merchant's server, providers issue a publishable key with very narrow permissions, safe to expose. Anything more sensitive goes through the customer's backend, which holds the secret key. In an interview about a public API, mentioning the split between secret and publishable keys, and why secrets never reach the client, is a strong detail.
Sessions for your own first party apps
For your own web application, where a user signs in through your login page, a server side session with a secure, HttpOnly, SameSite cookie remains a solid choice. The cookie holds an opaque session id, the server stores the session, and logout or revocation is immediate. The cost is the need for CSRF protection on state changing requests and a shared session store across servers. Many teams instead use OAuth tokens even for first party apps, which unifies the model across web, mobile and third parties. Either is defensible if you can explain the trade off.
What to say in the interview
A compact answer for a typical public API goes like this. Customer servers authenticate with prefixed, hashed, scoped API keys sent in the Authorization header, with support for multiple keys and rotation. Third party apps acting for users use OAuth 2.0 with scopes. Our own apps use our identity provider. The gateway authenticates every request once and passes a verified identity, tenant and scopes to services, which perform authorization against their own data. All traffic is TLS. That takes thirty seconds and frames the deeper topics the interviewer may choose to probe next.
Auditing and detecting misuse
Authentication data is also security telemetry. Record for every key its last used time, source IP ranges and request counts, and show them to the customer in the dashboard so they can spot a key used from somewhere unexpected. Alert on sudden changes, such as a key that normally makes a few hundred calls a day suddenly making a hundred thousand from a new region. Keep an audit log of key creation, scope changes and revocation, with the person who made each change. These features cost little, and in an interview they show that you think of credentials as something to operate over time, not only to check on each request.
Where authentication sits in the stack
Doing authentication once, at a shared layer, avoids every service reimplementing key parsing and token validation slightly differently. The gateway or an authentication middleware verifies the credential, rejects bad ones with 401 before any business code runs, and attaches a small, trusted identity context to the request: the principal, the tenant, the scopes and the authentication method. Internal services then trust that context only when it arrives over an authenticated internal channel, never from a header a client could set. Stripping any client supplied identity headers at the edge is an easy detail to forget and a serious hole when missed.
Verifying credentials belongs at the edge, in one shared layer. Deciding whether this caller may touch this object belongs in the service that owns the object, because only it knows ownership. Saying this split out loud answers half the security questions an interviewer might ask.