api

Idempotency keys for “create” endpoints

Retries are inevitable: mobile clients, flaky networks, and load balancers will resend POST requests. Without idempotency you end up double-charging or double-creating records. I store an Idempotency-Key with a sha256 hash of the request body and the

Django REST Framework nested routers

Nested routers create hierarchical URL structures for related resources. I use drf-nested-routers to define parent-child relationships in URLs like /posts/1/comments/. This makes APIs more RESTful and intuitive. I filter child resources by parent ID i

Next.js Route Handler with auth guard

I like API routes that read like tiny, well-scoped controllers. In Next.js Route Handlers, I keep auth and input parsing right at the top, then return explicit status codes instead of throwing for expected failures. I also avoid leaking server-only de

Django REST Framework viewset actions

Custom actions extend viewsets beyond CRUD operations. I use @action decorator with detail=True/False for object-level or collection-level actions. This creates endpoints like /posts/1/publish/ or /posts/recent/. I specify HTTP methods, permissions, a

tRPC router pattern for type-safe APIs

Maintaining separate REST types and frontend client types can be a lot of overhead in TypeScript-heavy teams. With tRPC, the API types flow directly to the client, which reduces duplication and keeps refactors safe. Runtime validation still matters, s

Runtime validation for request bodies (Zod)

TypeScript only protects you at compile time; your API still receives untrusted JSON from the internet. I lean on Zod as the source of truth for parsing + validation so runtime and types stay aligned. The big win is that I don’t try to validate ‘every

API input coercion for query params (Zod preprocess)

Query params arrive as strings, and ad-hoc parsing logic tends to drift across endpoints. I use Zod preprocessors to coerce values like page size and booleans, then validate the result. This keeps the handler readable and makes parsing rules shareable