Suggest an editImprove this articleRefine the answer for “How are Server Actions different from API Routes?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**Server Actions** and **API Routes** solve a similar task - running server-side code, but they do it **differently and for different scenarios**. **Key point:** the main difference is how and why they are called.Shown above the full answer for quick recall.Answer (EN)ImageServer Actions and API Routes solve a similar task - running server-side code, but they do it **differently and for different scenarios**. The main difference is **how and why they are called**. --- ## How they work conceptually ### API Routes An API Route is a **regular HTTP entry point**: - it has a URL (`/api/users`) - it accepts an HTTP request - it returns an HTTP response - it can be called by anyone, if access is open The frontend and server talk to each other **as two separate worlds**. --- ### Server Actions A Server Action is a **server-side function**, not an endpoint: - it has no URL - it can't be called directly over HTTP - it's only called from within the Next.js application - Next.js itself manages the transport The UI doesn't do `fetch` - it **calls a function**. --- ## The main difference: the interaction layer | Criterion | Server Actions | API Routes | |---|---|---| | Call method | Function call | HTTP request | | URL | No | Yes | | `fetch` | Not needed | Needed | | Suited for a public API | No | Yes | | Code ends up in the client | No | No | | Typing | Shared types | Needs syncing | --- ## Differences by purpose ### When API Routes are a good fit - you need a **public API** - access is needed by: - mobile apps - other services - external clients - you need explicit HTTP handling: - headers - statuses - cookies - middleware API Routes are a full backend endpoint. --- ### When Server Actions are a good fit - actions are initiated **from the UI** - the logic is tightly coupled to the UI - CRUD operations - form submissions - working with a DB and secrets - no external access is needed Server Actions are an "internal server layer" of the application. --- ## An example of the difference in practice **API Routes**: - frontend -> `fetch('/api/create-user')` - server -> `req`, `res`, HTTP statuses **Server Actions**: - form -> `action={createUser}` - server -> a regular function The logic is the same, but the path is shorter. --- ## Security and access - an API Route can be: - accidentally exposed externally - protected incorrectly - a Server Action: - has no public address - can't be called directly - is only accessible from within the application This lowers the risk of access mistakes. --- ## An important limitation of Server Actions Server Actions **don't fully replace API Routes**. If you need to: - serve data to external clients - build microservices - act as a backend for several applications - API Routes remain the right choice. --- ## In short **API Routes** are HTTP contracts for the outside world. **Server Actions** are server-side functions for the UI's internal logic.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.