# Guide: call the API from Next.js

> Official monorepo pattern: server actions, session, and Cache Components.

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

## Official pattern

```txt
'use client' UI
   │  calls action
   ▼
'use server' action  (features/... or services/...)
   │  requireGoDomainServerAuth / requireGoWriteAccess
   │  baseFetch / baseFetchMeta
   ▼
Go  /api/{domain}/profiles/{id}/...
```

## Steps in code

### 1. Auth and section gate

```ts
const auth = await requireGoWriteAccess('tasks', studioId, 'work');
// or requireGoDomainServerAuth(studioId, 'daily') for notebook
```

### 2. Go transport

```ts
const response = await baseFetchMeta<Task>(
  `/api/tasks/profiles/${studioId}`,
  auth.token,
  { method: 'POST', body: JSON.stringify(input) }
);
return response.data;
```

### 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**:

```txt
RSC / public query
   │  requireGoDomainServerAccess(studioId, section?)  (dynamic: cookies + membership)
   ▼
private function with 'use cache: remote'
   │  cacheLife('hours')
   │  cacheTag(tasksListTag(studioId))  // domain surface tag
   │  baseFetchInternalMeta(path)          // Bearer CRON_SECRET — no user token
   ▼
Go API (GET profile-scoped; trusts membership to the BFF)
```

```ts
import { baseFetchInternalMeta } from '#/services/shared/server/base-fetch';
import { tasksListTag } from '#/services/shared/server/cache-tags';
import { requireGoDomainServerAccess } from '#/shared/lib/server/require-go-domain-auth';

export async function listTasksForProfile(
  studioId: string,
  params: ListTasksParams = {}
) {
  await requireGoDomainServerAccess(studioId, 'work');
  // assignedToMe / createdByMe → path with user session (not shareable)
  return listTasksQueryCached(studioId, params);
}

async function listTasksQueryCached(
  studioId: string,
  params: ListTasksParams
) {
  'use cache: remote';
  cacheLife('hours');
  cacheTag(tasksListTag(studioId));
  return baseFetchInternalMeta(/* ... */);
}
```

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):

```txt
requireBetterAuthIdentity()          // Better Auth cookie cache → token (react.cache)
        │                            // (DB session only on cold bootstrap / Eve / token mint)
getAppSession()                      // GET /users/me/session (Redis in Go; react.cache)
        │  cold only: fresh BA session → POST bootstrap → retry session
        ▼
derive: user / prefs / profiles / access / canViewAuditLogs / settings
        │
profile layout Suspense
        │  work-bootstrap?projection=shell  (sidebar counts + project/tag pickers)
        ▼
on demand (not every soft-nav):
  · Profile Settings → loadProfileConfigDialogData (details+usage+invitations)
  · Create Library Item / library routes → library picker tags+collections
```

- **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

```ts
import { invalidateCache } from '#/services/shared/server/cache-invalidation';
import {
  projectsListTag,
  tagsListTag,
} from '#/services/shared/server/cache-tags';

// Tasks: list + inbox calendar + search + profile Calendar agenda
invalidateCache.taskWrite(studioId, taskId); // update due/status/reminder…
invalidateCache.tasksSurfaces(studioId); // create / duplicate
invalidateCache.tasksWrite(studioId, taskIds, { trash: true }); // bulk soft-delete
invalidateCache.tasksAll(studioId); // list+trash+calendar+search+agenda

// Helpers by domain (options → granular tags)
// Several domains also invalidate calendarAgendaTag (tasks, projects,
// activities, goals, library items, nutrition shopping, finance debts/savings).
invalidateCache.projects(studioId, { projectId });
invalidateCache.notebook(studioId, { noteId, groups: true });
invalidateCache.finance(studioId, 'transaction'); // accounts+tx+search+summary+budgets+agenda
invalidateCache.finance(studioId, 'recurring'); // rules+tx+…+agenda
invalidateCache.finance(studioId, 'debts'); // +agenda
invalidateCache.nutrition(studioId, 'recipes', { recipeId });
invalidateCache.nutrition(studioId, 'shopping'); // +agenda
invalidateCache.library(studioId, { itemId, trash: true }); // soft-delete/restore +agenda
invalidateCache.health(studioId, { checkins: true });
invalidateCache.tagList(studioId, { tagId });
invalidateCache.calendar(studioId, { reminderId }); // reminders list+agenda

// Multi-domain in a single refresh
invalidateCache.tags([
  projectsListTag(studioId),
  tagsListTag(studioId),
]);
```

## 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

| Need | Path |
| --- | --- |
| Better Auth identity | `shared/lib/server/better-auth-session.ts` |
| App session (Redis init) | `shared/lib/server/app-session.ts`, `platform-bootstrap.ts` |
| Domain access gates | `shared/lib/server/require-go-domain-auth.ts` |
| Domain helpers | `shared/lib/server/go-domain-action-helpers.ts` |
| Tags / invalidation | `services/shared/server/cache-tags.ts`, `cache-invalidation.ts` |
| Go fetch | `services/shared/server/base-fetch.ts` |
| Cached query example | `services/tasks/queries/list-tasks.ts` |

## Next

- [Create a task](/api/guides/create-a-task)
- [Rich text](/api/guides/edit-rich-text)
- [Local development](/getting-started/local-development)
