API & Server Actions
Why mutations are Server Actions, and when to add an API route.
Every mutation in the app is a Server Action ('use server'), colocated
with the routes that use it in an _actions/ folder — for example
openBillingPortal() in src/app/[locale]/(saas)/_actions/billing.ts.
That's the standard App Router pattern, and less code than a parallel REST
API.
On the client, call actions through the useAction() hook
(src/hooks/use-action.ts). It tracks the pending state and shows a toast
when the action throws:
const { isPending, execute } = useAction()
execute(() => openBillingPortal(), { error: 'Could not open the portal.' })Inside an action, read orgId and the role from auth() rather than
trusting arguments, and check the plan if the action is gated —
Build Your First Feature shows a complete action.
When to add an API route
Only when something outside your app needs to call it, like a third-party
webhook. That's why the one API route today is the Stripe webhook receiver,
src/app/api/webhooks/stripe/route.ts — see
How Billing Works.