#Full differences between server functions and api routes

10 messages · Page 1 of 1 (latest)

north canopy
#

Looking at the current examples it looks like they are created the same way and used the same way. One difference is api routes have routing, are there others?

worn bluff
#

You can’t use api routes like a function

rancid coyote
#

I think most of the time I would just use server functions

#

Except for webhooks and things like that

drifting isle
#

API routes are for when you need static routes that behave similar to Express and Fastify. Think something like a "public" API or an endpoint to receive webhooks (like from Stripe).

The server functions are more for creating dynamic endpoints, which when accessed to executes code on the server. Think of these endpoints as private as in their URLs are not static and can change between deployments.

#

Just a disclaimer, by private I do not mean "secure". You'd still have to appl security practices to both API routes and Server functions (rate-limiting, authorization, etc).

north canopy
#

thanks

north canopy
#

what are the differences between server functions in tanstack start and 'use server' directives in react 19? functionally its quite the same, its just slightly different way of creating them?

drifting isle
#

Server functions are the "concept".
The createServerFn util is an abstraction the automatically appends 'use server'

north canopy
#

cool, I thought that might be the case but have not checked in the source -_-