T
TanStack13mo ago
rare-sapphire

Full differences between server functions and api routes

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?
6 Replies
fair-rose
fair-rose13mo ago
You can’t use api routes like a function
rival-black
rival-black13mo ago
I think most of the time I would just use server functions Except for webhooks and things like that
fair-rose
fair-rose13mo ago
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).
rare-sapphire
rare-sapphireOP13mo ago
thanks 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?
fair-rose
fair-rose13mo ago
Server functions are the "concept". The createServerFn util is an abstraction the automatically appends 'use server'
rare-sapphire
rare-sapphireOP13mo ago
cool, I thought that might be the case but have not checked in the source -_- looking forward to the release!

Did you find this page helpful?