Skip to main content

Guide: call the API from Next.js

Official monorepo pattern: server actions, session, and Cache Components.
3 min read

The browser never talks to Go with the internal secret. Next is the BFF.

Official pattern#

Steps in code#

1. Auth and section gate#

2. Go transport#

3. Errors#

Propagate structured errors so the client can use isStructuredApiErr and upsell.

4. Cache (Cache Components)#

The shared fetch does not decide cache. Each cacheable read declares it with 'use cache: remote'.

For reads shareable across members of the same profile:

Rules:

  • Auth (cookies, headers, Better Auth) and membership/section outside the 'use cache: remote' scope.
  • Never pass the session token as an argument of a shareable cached function: it becomes part of the cache key and blocks sharing + fragments Runtime Cache.
  • Inside the cached scope: only baseFetchInternal / baseFetchInternalMeta (server-to-server). Writes and user-scoped reads still use baseFetch / baseFetchMeta + session Bearer.
  • Tags per surface in every domain (list / trash / detail / resource). Helpers in services/shared/server/cache-tags.ts.
  • Do not use a single profile-wide cache tag in product queries: that purges trash and unrelated surfaces on every write.
  • Do not put filters/cursors in tags — the function args form the cache key.
  • Do not put in Data Cache: session/identity, permission checks, usage/audit, editor documents, streaming, writes, exports, assignedToMe/createdByMe filters (they use session or a per-userId key in phase 2).
  • Writes: invalidateCache.* → updateTag (immediate, read-your-own-writes) + a single refresh(). Prefer domain helpers or tags([...]). Use { refresh: false } only in autosave/editor/blob. Do not use revalidateTag in product mutations.
  • Materialization with side effects (e.g. finance ensure-month) goes in a Server Action fired from the client, not in the RSC render.

4b. App init and dynamic reads (react.cache)#

Three distinct layers:

| Layer | What for | Where | | --- | --- | | Upstash Redis (Go) | Hot payload of GET /users/me/session (me:session:v2:*), identity and access | Go API | | react.cache (request) | Per-request dedupe of identity, session, access, usage, canView | Next BFF / RSC | | 'use cache: remote' (Data Cache) | Cacheable product reads with cacheTag | Domain queries |

Init warm path (one authenticated document):

  • Do not call GET /users/me proactively on every request: the Redis session already brings user + profiles + permissions (+ canViewAuditLogs, settings).
  • Do not preload usage/invitations/library pickers in the global profile layout.
  • Shell/gate access: profileAccessFromSession / resolveProfileAccess (session first; Go /access only as fallback). Audit: canViewAuditLogsFromSession — no hop to /audit-logs/access on the cold path.
  • Client bootstrap (GoUserBootstrapBoundary) only if BA says signed-in and there is no SSR initialSession.

Helpers: shared/lib/server/app-session.ts, platform-bootstrap.ts, require-go-domain-auth.ts.

5. Invalidation#

Security checklist#

  • Action marked 'use server'
  • Do not import secrets in client components
  • studioId validated on the server, not only from a blind client
  • Correct app section (work, daily, finance…)

Where to look#

NeedPath
Better Auth identityshared/lib/server/better-auth-session.ts
App session (Redis init)shared/lib/server/app-session.ts, platform-bootstrap.ts
Domain access gatesshared/lib/server/require-go-domain-auth.ts
Domain helpersshared/lib/server/go-domain-action-helpers.ts
Tags / invalidationservices/shared/server/cache-tags.ts, cache-invalidation.ts
Go fetchservices/shared/server/base-fetch.ts
Cached query exampleservices/tasks/queries/list-tasks.ts

Next#