{"success":true,"name":"pick-server-actions-or-route-handlers-deliberately-in-next-j","description":"In Next.js and about to create an endpoint for a form or mutation","category":"ai-brimind","path":"ai-brimind/pick-server-actions-or-route-handlers-deliberately-in-next-j/SKILL.md","file":"SKILL.md","content":"---\nname: pick-server-actions-or-route-handlers-deliberately-in-next-j\ndescription: \"In Next.js and about to create an endpoint for a form or mutation\"\n---\n\n# Pick Server Actions or Route Handlers Deliberately in Next.js\n\n## When to use\nProject is Next.js App Router, and you are about to write a route handler (or an old pages/api file) for something your own app submits.\n\n## Example request\ncreating /api/create-post plus a fetch call just for your own form → a Server Action does the same with no manual endpoint, JSON, or wiring. — Server Actions for own-app mutations. | an external service or mobile app must call your backend → outsiders cannot call Server Actions; that is a real route handler with a public URL. — Route handlers face the outside. | a webhook from a payment provider needs a POST URL → route handler, plus signature verification. — Public callback surface. | building fetch('/api/...') from a Server Component to your own app → the server calling itself over HTTP; call the shared function directly instead. — Self-fetch anti-pattern.\n\n## Instructions\nAct like an architect who asks 'who calls this?' before creating any endpoint in Next.js. When a mutation or endpoint is needed: 1) Name the caller: (a) your own app's forms and buttons → use a Server Action: a plain async server function wired to the form, with no URL to design, no JSON parsing, and built-in integration with revalidatePath. (b) External callers — mobile apps, other services, webhooks, third parties → use a route handler, because they need a real public URL. 2) Never write fetch to your own /api routes from Server Components or Server Actions — the server would be making an HTTP call to itself. Extract the logic into a normal function and call it directly from both places. 3) Whatever you pick, remember both are public entry points: validate inputs and check auth inside the Server Action or handler itself, never trusting that 'only my form calls this'. 4) Prefer the built-in form integration (action={...}, useFormStatus, redirect, revalidatePath) over custom fetch + loading-state plumbing when it is your own UI. 5) If the caller is unclear ('might be external someday'), ask or decide for today's real caller instead of building a speculative public API. Only create the endpoint once the caller is named, and explain why that caller dictates this mechanism — not just that a POST request succeeds.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #498\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}