DM
Technical reference

API Security Cheatsheet

Protect APIs from common attacks and misuse

Must Know

typescript

Validate at the Boundary

Validate type, format, length, range, and unexpected fields.

const Input = z.object({
  email: z.string().email(),
  limit: z.coerce.number().int().min(1).max(100),
}).strict();
javascript

Parameterized Query

Never concatenate untrusted values into SQL.

db.query('SELECT * FROM users WHERE id = $1', [userId]);

Important Patterns

javascript

Object-Level Authorization

Possessing an object ID does not grant access.

const invoice = await loadInvoice(id);
if (invoice.tenantId !== user.tenantId) throw forbidden();

Rate Limits

key: user ID, API key, and/or trusted IP
limit by endpoint cost
return 429 + Retry-After
protect login and recovery separately

Useful Recipes

Security Headers

Strict-Transport-Security: max-age=31536000
Content-Security-Policy: default-src 'self'
X-Content-Type-Options: nosniff
Referrer-Policy: no-referrer

Safe Outbound Requests

Protect server-side fetches from SSRF.

allow-list schemes and hosts
resolve and reject private IP ranges
disable unsafe redirects
set timeouts and size limits

Pitfalls & Production

Sensitive Data

do not log tokens, passwords, card data, or health data
redact structured fields
encrypt in transit and at rest
minimize retention

Security Operations

Security must remain operable after deployment.

dependency scanning
secret scanning
key rotation
audit logs
incident playbook
regular access review