API ownership and boundaries
What you need to know
Choose an integration boundary according to who consumes it, what it owns and how it must operate. A Server Component, a Server Action, a Route Handler and an independent API solve overlapping but different problems.
By the end, you can: compare BFF versus independent API; define authentication responsibility.
Boundary decision for River Books
Page rendering -> direct server-only catalog readBrowser autocomplete -> GET /api/v1/suggestionsEdit own reading note -> Server Action + ownership checkShared fulfillment -> separately owned service APILong-running job -> approved worker/queueExpected behavior (not framework-tested): Each choice is motivated by consumer and lifetime; these are proposed contracts, not deployed services.
How it works
A Server Component can read directly through a server-only data access layer to render a page. A Server Action is suitable for UI-driven mutations coordinated with framework rendering. A Route Handler exposes an explicit HTTP contract to a browser client or other consumer. A separate service can own a domain shared by multiple clients, long-lived processing, independent deployment and its own security/operational contract. Adding a separate service purely for fashion adds latency, failure modes and deployment effort.
Every externally reachable entry point must validate input and enforce authorization. Authentication establishes identity; authorization decides access to an action and specific resource. A proxy or CORS configuration does not replace authorization. CORS controls certain browser cross-origin reads and requests; it does not stop non-browser clients from calling a public endpoint. A downstream service should receive authenticated workload identity and enforce the contract at its own trust boundary. For writes, an idempotency key and atomic deduplication can prevent duplicate operations; a UI-disabled button is not a durable deduplication mechanism.
Define an explicit versioned endpoint
// app/api/v1/suggestions/route.ts — fixed public fixtureexport function GET(request: Request) { const values = new URL(request.url).searchParams.getAll('q'); if (values.length > 1) return Response.json({ error: 'INVALID_QUERY' }, { status: 400 }); const q = values[0] ?? ''; if (q.length > 40) return Response.json({ error: 'INVALID_QUERY' }, { status: 400 }); const items = ['Moss Atlas', 'Fern Journal'].filter(title => title.toLowerCase().includes(q.toLowerCase())); return Response.json({ items });}Expected behavior (not framework-tested): q=moss returns one result. Versioning the URL defines a place to evolve the external contract; it does not guarantee compatibility by itself.
Common mistake
Do not build a public endpoint merely so a Server Component can call its own code over HTTP. That adds network overhead and creates a new trust boundary. Conversely, do not expose Server Action transport as a stable public REST contract for unrelated clients.
Practice and verification
Write a small architecture decision record: consumers, data owner, authentication/authorization boundary, caching and failure policy, deployment lifetime, and one alternative. Define how repeated requests are deduplicated if the endpoint creates resources. Keep the actual authorization implementation for the next lesson.
Scope: App Router, proposed Next.js 16.x, Cache Components disabled for these labs. Snippets assume a reviewed TypeScript app and are static examples; framework behavior has not been executed. Exact supported patch and dependency set must be verified before running.
Further reading: Bff · Data Security · Auth