How would you secure a public REST API? Walk through the controls you would put in place.
Answer
Securing a public REST API means layering controls so that authentication proves who is calling, authorization decides what they may do, and the rest of the stack limits abuse and protects data in transit. A good answer names several specific controls and explains the reasoning, not just 'use API keys and rate limiting'.
Authentication: prove who is calling
API keys suit server-to-server traffic but are long-lived secrets, so rotate them, scope them and never put them in frontend code or URLs.
OAuth 2.0 with short-lived JWT access tokens suits user-facing APIs. Validate the signature, issuer, audience and expiry on every request, and keep access tokens short so a leaked one expires quickly.
Always send credentials over TLS, never in query strings, which end up in logs and browser history.
Authorization: enforce what they may do
Authentication is not authorization. The most common and damaging API bug is broken object level authorization (BOLA), number one on the OWASP API Security Top 10. It happens when GET /orders/1043 returns any order because the server never checks that the order belongs to the caller. Enforce ownership and role checks on the server for every object and every action. Never trust an ID, role or price sent by the client.
Limit abuse and protect the data
Rate limiting and throttling per key or user to blunt brute force, scraping and denial of service. Return 429 with a Retry-After header.
Input validation against a strict schema, rejecting unknown fields to prevent mass assignment where a client sets isAdmin=true.
Minimal responses: return only the fields the client needs, so you do not leak internal data through excessive exposure.
CORS configured with a specific allowlist, never a reflected Access-Control-Allow-Origin with credentials.
Security headers and an API gateway for TLS termination, auth, logging and WAF rules in one place.
Observability
Log authentication failures, authorization denials and rate-limit hits, and alert on spikes. Many API breaches are discovered only because someone noticed one key pulling millions of records.
Common mistakes
Checking authentication but forgetting per-object authorization (BOLA).
Putting API keys or tokens in the URL or in client-side JavaScript.
Relying on an API being 'hidden' or undocumented as if that were security.
Validating input on the client only, which an attacker bypasses by calling the API directly.
Verbose error messages that leak stack traces, SQL or internal hostnames.
Request through an API security pipeline. Each stage is a gate; a request must pass all of them before it touches your data.
TLS termination: Encrypted transport at the gateway.
Authentication: Validate the API key or JWT signature and expiry.
Rate limiting: Throttle per key or user, return 429 when exceeded.
Input validation: Reject bad schemas and unknown fields.
Authorization: Check the caller owns the object and has the role.
Business logic: Runs only after every gate passes.
Code examples
Per-object authorization and rate limiting (Express)
import rateLimit from 'express-rate-limit';
// 100 requests per minute per API key
const limiter = rateLimit({
windowMs: 60_000,
max: 100,
keyGenerator: (req) => req.apiKey,
standardHeaders: true, // sends RateLimit-* and Retry-After
});
app.get('/orders/:id', authenticate, limiter, async (req, res) => {
const order = await db.orders.findById(req.params.id);
// BOLA fix: confirm the order belongs to the caller
if (!order || order.userId !== req.user.id) {
return res.status(404).json({ error: 'Not found' });
}
res.json(order);
});
The ownership check is what stops broken object level authorization. Returning 404 rather than 403 also avoids confirming that another user's order exists.
Key points
Authentication proves identity; authorization decides access. They are separate controls.
BOLA (missing per-object authorization) is the top API vulnerability; check ownership every time.
Use short-lived tokens, TLS everywhere, and never put secrets in URLs or client code.
Rate limiting, strict schema validation and minimal responses blunt abuse and leakage.
Log and alert on auth failures and rate-limit hits to detect attacks early.
Common follow-up questions
What is BOLA and why is it the top API security risk?
How do you validate a JWT, and what claims must you check?
How would you design API key rotation without downtime?
What is mass assignment and how does schema validation prevent it?
Interview tips
Go beyond 'keys and rate limiting'; naming BOLA and least privilege signals real experience.
Stress that authentication is not authorization; many candidates conflate them.
Give one concrete mitigation per risk rather than listing buzzwords.