Front End System Design Playbook · Chapter 7 of 11

API design for the front end

The I in RADIO is where your design becomes concrete. Interviewers want to see that you can define the contract between the client and server, and between components, in enough detail that another engineer could build against it. Front end engineers are API consumers every day, and senior ones increasingly shape those APIs through BFFs, GraphQL schemas, and design-system component APIs.

Two kinds of interfaces

InterfaceBetweenExamples
Network APIClient and serverGET /feed, GraphQL query, WebSocket events
Component APIComponents and their consumersProps, callbacks, slots, imperative handles

In application questions, spend more time on the network API. In component questions (autocomplete, modal, data table), the component API is the main event.

Choosing an API style

StyleStrengthsWeaknessesChoose when
REST (JSON)Simple, cacheable with HTTP, universal toolingOver- and under-fetching, many round trips for nested dataResource-oriented CRUD, public APIs, CDN caching matters
GraphQLClient picks fields, one round trip, typed schemaHarder HTTP caching, query cost control, more infrastructureMany clients with different needs, deeply nested data
tRPC / typed RPCEnd-to-end types with no codegenCouples client and server (TypeScript monorepo)Full-stack TS apps owned by one team
BFF (any style)Shapes data per client, hides microservice sprawlAnother service to runMany backend services, mobile and web diverge

In an interview, pick one, justify it in one sentence, and move on. "REST with a BFF that returns view-shaped responses, because the feed aggregates four services and we want one round trip on mobile."

Designing endpoints

Show realistic request and response shapes:

GET /api/v1/feed?cursor=eyJ0IjoxNzA...&limit=20
Accept: application/json

200 OK
Cache-Control: private, max-age=0
{
  "items": [
    {
      "id": "p_123",
      "author": { "id": "u_9", "name": "Asha", "avatarUrl": "https://cdn.../a.webp" },
      "text": "Shipping day!",
      "media": [{ "url": "https://cdn.../1.avif", "width": 1080, "height": 1350, "alt": "Team photo" }],
      "likeCount": 41,
      "viewerHasLiked": false,
      "createdAt": "2025-03-02T10:15:00Z"
    }
  ],
  "nextCursor": "eyJ0IjoxNzA...",
  "hasMore": true
}

Details that score points:

  • Media dimensions in the payload, so the client can reserve space (no CLS).
  • Viewer-specific fields (viewerHasLiked) so the client doesn't need a second request.
  • Opaque cursors, so the server can change its pagination strategy.
  • ISO timestamps in UTC; format on the client per locale.

Pagination

TypeHowProsConsUse for
Offset?page=3&limit=20Jump to page N, simpleDuplicates or skips when data changes; slow deep offsetsAdmin tables, search results with page numbers
Cursor?cursor=abc&limit=20Stable under inserts, efficientNo random accessFeeds, chats, infinite lists
Keyset / time-based?before=2025-03-01T...Transparent, efficientNeeds a unique sortable keyMessage history, logs

For chat, mention bidirectional pagination: load older messages upward, and catch up on newer ones after reconnecting.

Mutations

POST /api/v1/posts/p_123/likes
Idempotency-Key: 7f9c2e1a-...

200 OK
{ "likeCount": 42, "viewerHasLiked": true }
  • Return the updated resource (or the fields that changed) so the client can reconcile its optimistic state.
  • Idempotency keys for anything that may be retried (payments, message sends).
  • Client-generated ids for optimistic creates, mapped to server ids in the response.

Error contract

Agree on one error shape across endpoints:

{
  "error": {
    "code": "RATE_LIMITED",
    "message": "Too many requests",
    "retryAfterMs": 3000,
    "fields": { "email": "Already in use" }
  }
}
HTTP statusClient behavior
400 / 422Show field-level validation errors; don't retry
401Refresh token once, then redirect to login
403Show a permission message; don't retry
404Show not-found UI; remove from cache
409Conflict: refetch and ask the user to resolve
429Back off using Retry-After
5xx / networkRetry with exponential backoff and jitter; show offline or retry UI

Real-time transports

TransportDirectionProsConsUse for
Short pollingClient pullsSimplest, works everywhereWasteful, adds latencyLow-frequency updates (every 30–60s)
Long pollingClient pulls, server holdsWorks through most proxiesConnection churnFallback
Server-Sent EventsServer to clientHTTP-based, auto-reconnect, simpleOne-way; per-domain connection limits on HTTP/1.1Notifications, live feeds, AI token streaming
WebSocketBidirectionalLow latency, full duplexStateful servers, custom reconnect and heartbeat logicChat, collaboration, games
WebTransportBidirectional (HTTP/3)Multiple streams, unreliable datagramsNewer; check current browser supportMedia, gaming (niche)

Describe the event schema, not just the transport:

type ServerEvent =
  | { type: 'message.created'; message: Message }
  | { type: 'message.read'; conversationId: string; readerId: string; upTo: string }
  | { type: 'typing'; conversationId: string; userId: string };

And the reconnect story: exponential backoff, resume from a lastEventId or cursor, and refetch whatever was missed.

Component API design

For component questions, design props like a public library:

type AutocompleteProps<T> = {
  // data
  fetchSuggestions: (query: string, signal: AbortSignal) => Promise<T[]>;
  getItemKey: (item: T) => string;
  // rendering
  renderItem?: (item: T, state: { active: boolean }) => React.ReactNode;
  renderEmpty?: (query: string) => React.ReactNode;
  // behavior
  debounceMs?: number;          // default 200
  minQueryLength?: number;      // default 1
  // controlled or uncontrolled value
  value?: string;
  defaultValue?: string;
  onChange?: (value: string) => void;
  onSelect: (item: T) => void;
  // a11y
  label: string;
};

Principles to say out loud:

  • Sensible defaults, escape hatches for power users (render props or slots).
  • Support both controlled and uncontrolled usage, following the value/defaultValue convention.
  • Pass AbortSignal so the component can cancel stale requests.
  • Require accessibility-critical props (such as label) at the type level.
  • Generic types so consumers keep type safety for their item shape.
  • Composition over configuration when the option list keeps growing (<Select.Trigger>, <Select.Item>).

Caching and HTTP semantics

  • ETag / If-None-Match for cheap revalidation.
  • Cache-Control: public, max-age=31536000, immutable for hashed static assets.
  • stale-while-revalidate for semi-dynamic JSON at the edge.
  • Personalized responses: private and Vary on auth.
  • Client cache keys must include every input that changes the result (['feed', userId, filters]).

Versioning and evolution

  • Additive changes only. Never repurpose a field.
  • Version via URL (/v2) or header for breaking changes. GraphQL: deprecate fields and track usage.
  • Contract tests or schema validation (OpenAPI, Zod at the boundary) to catch drift.
  • Generate TypeScript types from the schema. Don't hand-maintain both sides.

What lead candidates add

  • Ownership: who owns the BFF? If front end teams own it, they can move fast, but they now carry on-call.
  • Performance budgets on the API: p95 response time targets, payload size limits for mobile.
  • Consistency across teams: shared error format, pagination conventions, and an API style guide.
  • Security: auth via httpOnly cookies vs bearer tokens, CSRF strategy, never trusting client-calculated values (prices, permissions).

Checklist

  • Chose an API style with a one-line justification
  • Wrote at least one realistic request/response with real fields
  • Picked a pagination type that fits the data
  • Described mutations with optimistic reconciliation and idempotency
  • Defined an error shape and client behavior per status
  • Chose a real-time transport and described events and reconnects (if needed)
  • Designed a typed component API with defaults, controlled/uncontrolled support, and a11y

Spotted a mistake or a rights concern? Report an issue