Calling procedure from api endpoint
How can I call updatePlaylists from a function handler in pages/api/updatePlaylists.ts
7 Replies
If you're trying to call a procedure from another router, you should break out the shared logic into its own function; calling tRPC within tRPC leads to problems. For calling a procedure from anywhere outside of tRPC on the server, you make a caller. Here's the details on callers in the tRPC docs: https://trpc.io/docs/server/server-side-calls#create-caller. For calling on the client side, you just make a single endpoint/file in
pages/api
to handle all client-side tRPC calls. Depending on the framework, you may use a NextJS handler/adapter or a vanilla handler/adapter. Hope that helpsServer Side Calls | tRPC
You may need to call your procedure(s) directly from the same server they're hosted in, router.createCaller() can be used to achieve this.
In my case I have a cron job which calls the nextjs endpoint and the endpoint is meant to call the procedure. The picture shows the file which I will be calling the procedure from.
I am still getting all sorts of errors when I follow the examples in the docs, do you have an idea on which example I should be looking at in the docs?
There's this pattern that I use in my Astro projects, though I'm not sure it'll help, just putting it here just in case: https://trpc.io/docs/client/setup
This one specifically walks through setting up NextJS with tRPC: https://trpc.io/docs/nextjs/setup
Set up a tRPC Client | tRPC
1. Install the tRPC Client library
Set up with Next.js | tRPC
Recommended file structure
This one discusses what to do in
pages/api/trpc/[trpc].ts
which is where you'd put your handler, not pages/api
like I initially suggested: https://create.t3.gg/en/usage/trpc#-pagesapitrpctrpcts
This snippet is what you'd put in
pages/api/trpc/[trpc].ts
. Because you're calling this endpoint from a different place, I think adding cors is necessary, but please correct me if I'm wrong.
Link: https://create.t3.gg/en/usage/trpc#enabling-corsI will give this a read and a shot and let you know. Thank you for your help
Yeah happy to