Calling server functions from API route?
Am I doing something wrong or is this not supported?
search
is a server function defined in another file. Nothing crazy in it, just a db call.
16 Replies
fair-rose•11mo ago
why don’t you make the db call here?
deep-jadeOP•11mo ago
In this very specific instance: so I can test it via Insomnia. In reality it will still be a server fn.
I just noticed what....appears? to be a functionality gap here and was wondering if it's not supported?
I feel like executing server fn's from an API route is very sensible
since they are.....just server code
fair-rose•11mo ago
Just thinking about it, you could just make search a simple function instead of server fn.
I believe server fn's should be called from client side only.
fair-rose•11mo ago
react docs

deep-jadeOP•11mo ago
I didn’t think they had much to do with RSC
Atleast the server fn’s in start
fair-rose•11mo ago
focus on the last text
deep-jadeOP•11mo ago
?
Like meaning, I didn’t think server fn in start = rsc server fn
Obviously in Start they are replaced on the client w a fn that makes a request
relaxed-coral•11mo ago
React server functions and tanstack serverFn are two separate concepts it's an unfortunate confusing rename made recently by React, as they used to call them server actions
deep-jadeOP•11mo ago
But I don’t see why they shouldn’t be able to still be called in server code from one another or from an api route.
That was my understanding also @Jaga Santagostino
relaxed-coral•11mo ago
I'm not sure this is true, but AFAIK tanstack server fn are expected to handle middlewares, so it would be like calling another endpoint running in the same app passing from the networ I don't think is a supported case
deep-jadeOP•11mo ago
I suppose how they are built now, with the idea that they will always be tied to a request, might make that hard?
relaxed-coral•11mo ago
mind sharing the error you are facing? if any
deep-jadeOP•11mo ago
Yeah. It’s likely not a common use case either. I just ran into it when quickly trying to use Insomnia on an existing server fn I had
@Jaga Santagostino ^^^ is my API route, “search” is a server fn. in a real use case I wouldn’t do this, I’d have one or the other. For now though I just wanted a quick route to test and found that it didn’t work and was curious if it was supposed to.
fair-rose•11mo ago
totally, I was just saying that it doesn't make much sense to call a server fn inside an api handler
deep-jadeOP•11mo ago
Probably not. Since they both achieve the same purpose
Sounds like not supported and likely doesn’t make sense to , which is ok.
fair-rose•11mo ago
Yeah, in this case, directly call a function called search or as you mentioned bring the db call here