Where Should I Place API Communication Files for OpenAI in a Next.js and tRPC T3 Stack?

I've recently started working with tRPC due to its integration in the T3 Stack. I'm trying to build a chat application using OpenAI's API. I'm curious about where to write the files for API communication when interfacing with OpenAI's API. Should they be placed in Next's API routes or under the server folder in tRPC's routers? According to GPT-4, it's better to write them in the API routes for more flexibility when communicating with external servers. How do you all differentiate between the two? I understand that the communication to prisma should be written to tRPC's routers because they are internal db communication and we know the type definitely.
4 Replies
peterkyle01
peterkyle0114mo ago
Just throw all api stuff under trpc server to have types,thats the beauty of trpc ,its like api routes but better
IT
ITOP14mo ago
@peterkyle01 Thanks for answering! I will try your advice. Since using the T3 Stack also involves maintaining type safety, it's better to write the code for API communication on tRPC's server rather than placing it in Next.js's API routes, correct?
IT
ITOP14mo ago
And I was looking at a public repository a little. Why do you think the person of the repository below separates the API route and the tRPC for API communication? API route: https://github.com/NathanLazo/chat-gpt-T3-stack/blob/main/src/pages/api/openai/gpt.ts tRPC: https://github.com/NathanLazo/chat-gpt-T3-stack/blob/main/src/server/api/routers/gpt.ts
GitHub
chat-gpt-T3-stack/src/pages/api/openai/gpt.ts at main · NathanLazo/...
T3 Stack with the power of OpenAI API. Contribute to NathanLazo/chat-gpt-T3-stack development by creating an account on GitHub.
GitHub
chat-gpt-T3-stack/src/server/api/routers/gpt.ts at main · NathanLaz...
T3 Stack with the power of OpenAI API. Contribute to NathanLazo/chat-gpt-T3-stack development by creating an account on GitHub.
peterkyle01
peterkyle0114mo ago
I mostly use api routers for third party auth for example if i need to pass tokens in the request or some headers,i looked for where it was used i did not find it but it still shows what i say about passing some auth or keys in the request using api routes or also using them as middlewares.rRPC are for the rest like data fetching from db and all crud stuff.
Want results from more Discord servers?
Add your server