Calling procedure from api endpoint

How can I call updatePlaylists from a function handler in pages/api/updatePlaylists.ts
7 Replies
del.hydraz
del.hydraz2y ago
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 helps
Server 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.
DunnO
DunnOOP2y ago
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?
del.hydraz
del.hydraz2y ago
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
del.hydraz
del.hydraz2y ago
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
Create T3 App
tRPC 🚀 Create T3 App
The best way to start a full-stack, typesafe Next.js app.
del.hydraz
del.hydraz2y ago
import { type NextApiRequest, type NextApiResponse } from "next";
import { createNextApiHandler } from "@trpc/server/adapters/next";
import { appRouter } from "~/server/api/root";
import { createTRPCContext } from "~/server/api/trpc";
import cors from "nextjs-cors";

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
// Enable cors
await cors(req, res);

// Create and call the tRPC handler
return createNextApiHandler({
router: appRouter,
createContext: createTRPCContext,
})(req, res);
};

export default handler;
import { type NextApiRequest, type NextApiResponse } from "next";
import { createNextApiHandler } from "@trpc/server/adapters/next";
import { appRouter } from "~/server/api/root";
import { createTRPCContext } from "~/server/api/trpc";
import cors from "nextjs-cors";

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
// Enable cors
await cors(req, res);

// Create and call the tRPC handler
return createNextApiHandler({
router: appRouter,
createContext: createTRPCContext,
})(req, res);
};

export default handler;
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-cors
Create T3 App
tRPC 🚀 Create T3 App
The best way to start a full-stack, typesafe Next.js app.
DunnO
DunnOOP2y ago
I will give this a read and a shot and let you know. Thank you for your help
del.hydraz
del.hydraz2y ago
Yeah happy to
Want results from more Discord servers?
Add your server