Architecture Reference // 2026

Public API
Security Principles

Cloud-native, security-first rules for any API exposed on the public internet.
8 domains · 23 rules.

01 // Identity & Access

Who is calling, and what are they allowed to do?

Authenticate at the edge before requests reach your service. Keep tokens short-lived, scoped minimally, and out of URLs. Every downstream failure in this domain is a full account compromise.

01
Verify at the edge, never inside
JWT / OAuth2 validation at the gateway. Never trust client-supplied claims without server-side verification.
02
Short-lived tokens with refresh rotation
Access tokens at 15 min or less. Refresh token rotation invalidates stolen tokens on the next use — the breach window is bounded.
03
Least-privilege scopes
Each token carries only the scopes its use case requires. Overly broad tokens turn a credential leak into a full account compromise.
04
Secrets never in URLs
API keys in query strings land in server logs, browser history, and CDN access logs. Use Authorization headers exclusively.
02 // Transport & Protocol

How does data move between caller and service?

All traffic must be encrypted. Every default that is too permissive is an attack surface — CORS wildcards, missing HSTS, and unvalidated content types are all exploitable.

TLS 1.2+ only — enforce HSTS
Terminate TLS at the edge. Add Strict-Transport-Security: max-age=31536000; includeSubDomains so browsers never downgrade to HTTP.
Strict CORS policy
Access-Control-Allow-Origin: * is only safe for fully public, read-only, unauthenticated endpoints. Everything with auth requires an explicit allowlist.
Validate Content-Type on inbound
Reject requests with unexpected content types before parsing. Parser confusion attacks exploit the gap between what you expect and what you receive.
03 // Rate Limiting & Abuse

How does the system defend itself under load?

Limiting is not a single dial. Granularity matters — a global cap does nothing to stop a single actor hammering one expensive endpoint. Defense must happen at multiple layers.

1

Rate-limit at multiple granularities

Per-IP, per-user, per-endpoint, per-tenant. Each granularity catches a different class of abuse that the others miss.

2

Progressive backoff on auth failures

After N consecutive failed auth attempts, introduce forced delays or lockouts. This stops credential-stuffing without blocking legitimate users.

3

WAF + DDoS absorption at the edge

Application-layer limiting is too late if volumetric floods hit your origin. Cloudflare, AWS Shield, or equivalent must absorb attacks before your code runs.

04 // Input & Output

What enters and leaves the service boundary?

Invalid input must be rejected at the edge of your service, not propagated inward. Output discipline is equally important — what you return becomes another party's attack surface.

Schema-validate all input at the boundary

JSON Schema, Zod, Pydantic — pick the stack equivalent. Reject with a 400 immediately; never let malformed data reach your domain logic.

Never reflect unsanitized input

Even JSON APIs can carry stored XSS if responses are rendered. Encode output; don't assume consumers are safe.

Paginate and cap all list endpoints

Unbounded queries are both a DoS vector and a data-exfiltration path. Server-side page size ceiling — never caller-controlled without a hard limit.

Strip internal fields from responses

Return only what the contract defines. Serializers that auto-expose model fields — password_hash, internal IDs — are a common accidental leak.

05 // API Design

How is the contract structured for safety?

API design decisions made early become hard to reverse once clients depend on them. Three choices — versioning, ID scheme, and error shape — have outsized security implications.

Version from day one — /v1/
Breaking changes without versioning force all clients to update simultaneously. A version prefix gives you a controlled deprecation runway.
Opaque IDs externally
UUIDs or hashed IDs prevent enumeration attacks. Sequential integers expose record counts and make bulk scraping trivial.
Consistent, minimal error envelopes
A stable schema like {"error":{"code":"...","message":"..."}} lets clients handle errors programmatically. Never expose stack traces or internal paths.
06 // Secrets & Config

Where do credentials and configuration live?

Secrets committed to code or stored in plaintext env vars are already exposed — it is only a matter of when they are found. Separation of config from code is both a security and operational requirement.

Where secrets belong
Vault
AWS Secrets Manager
GCP Secret Manager
Injected at runtime
Rotated on schedule
Rotated on compromise
Where secrets never belong
Source code
Plaintext .env files
Container image layers
URLs and query strings
Logs
07 // Observability

How do you know when something is wrong?

Logs and metrics are not just operational tooling — they are your only way to detect an active attack. What you cannot see, you cannot defend. Build this in from day one.

01
Correlation ID on every request
A request ID traces a transaction end-to-end across services. Log it everywhere. But never log Authorization tokens, passwords, or PII — logs are a secondary breach surface.
02
Alert on anomalies, not just errors
A spike in 401s, latency surge on one endpoint, or unusual geographic distribution are security signals. Metrics and alerting are part of your security posture, not just ops.
03
Circuit breakers + defined SLOs
Downstream failures must fail fast, not cascade. A public API that hangs under load becomes an amplifier for outages and a target for resource-exhaustion attacks.
08 // Infrastructure Posture

What is exposed, and to whom?

The network perimeter is part of the API's security surface. A well-secured application running on a misconfigured host is still vulnerable. Minimal exposure is a first-class requirement.

Origin must not be publicly reachable

If you are behind a CDN or proxy, your origin IP must be firewalled to accept traffic only from the CDN's IP ranges. An exposed origin bypasses all edge protections.

Least network exposure

Internal services stay on private subnets. Only the API gateway or load balancer faces the internet — everything behind it is unreachable directly.

Automate certificate rotation

Manual cert renewal is an operational risk with a predictable failure mode. ACM, Let's Encrypt with certbot, or CDN-managed certs with auto-renewal eliminate expiry incidents.

Dependency scanning in CI

npm audit, pip-audit, trivy — whichever fits the stack. Known CVEs in dependencies are the most common attack vector for otherwise well-designed APIs.

The mental model

The edge authenticates.
The gateway rate-limits.
The service validates.
The data layer least-privileges.
Observability catches what everything else missed.

Daniel Brasileiro