Dev Overflow Logo

Dev Overflow

Global search

Search across questions, answers, users and tags.

Loading...
save

Server Actions vs Route Handlers — when should I use which?

clock icon

asked 2 months ago

message icon

2

eye icon

1.1K

I have both app/api/*/route.ts files and "use server" action files in the same project, and I am no longer sure which belongs where. Is there a rule of thumb?

2 Answers

The dividing line is who calls it.

Server Actions — your own UI calls it. Mutations from forms and client components, where you want the closure over server code, automatic revalidation, and no hand-written fetch.

Route Handlers — something that is not your React tree calls it. Third-party webhooks, OAuth callbacks, mobile clients, cron jobs, anything needing a stable public URL, custom headers, or a non-JSON response.

A useful check: if you would ever want to curl it, make it a route handler.

Worth adding that server actions are POST-only under the hood, so anything that needs to be cacheable or GET-addressable has to be a route handler regardless of who calls it.

1

of 1

Write your answer here

Introduce the problem and expand on what you've put in the title.

Top Questions