Difference between Route Handlers/API Routes and Server Actions
Unanswered
Northern Shoveler posted this in #help-forum
Northern ShovelerOP
Hi guys, I've a little bit of mess in my mind. Can someone briefly explain the differences between these entities in Next.JS?
- Route Handlers/API Routes
- Server Actions
- Route Handlers/API Routes
- Server Actions
5 Replies
Siberian
Server actions basically create an API endpoint under the hood, with the added benefit that you can call the function directly, you get type safety etc.
Manually defining an API endpoint gives you more control over stuff like what verb you use (PATCH, POST, ...) etc and also allow you to define what the actual route of the endpoint is, which you'd need if you wanted to call it from outside your next js application
Manually defining an API endpoint gives you more control over stuff like what verb you use (PATCH, POST, ...) etc and also allow you to define what the actual route of the endpoint is, which you'd need if you wanted to call it from outside your next js application
@Siberian Server actions basically create an API endpoint under the hood, with the added benefit that you can call the function directly, you get type safety etc.
Manually defining an API endpoint gives you more control over stuff like what verb you use (PATCH, POST, ...) etc and also allow you to define what the actual route of the endpoint is, which you'd need if you wanted to call it from outside your next js application
Northern ShovelerOP
Thank you bro, so if I don't have the need to expose anything outside of my app, I actually don't need Route Handlers (which are the evolution of API Routes, correct me if I'm wrong)
Yes that’s correct. There are fewer times where you might need a route handler now
Siberian
FWIW I am a fan of using them when learning Next cause it makes the boundary between client/server more obvious
not knowing that leads to a lot of confusion