tRPC

T

tRPC

Move Fast & Break Nothing. End-to-end typesafe APIs made easy.

Join

Support @tanstack/react-query 5.4.3

will it support 5.4.3 @tanstack/react-query when i install above version with my next.js it unable to start, have to fall back to 4.36.1...
Solution:
sorry just found the post mentioning it https://github.com/trpc/trpc/issues/4218

Help with inferring output

```ts return ctx.prisma.product.findMany({ include: { price: { select: {...
No description

Would you recommend tRPC’s usage in this case?

So we have two applications, one existing, one about to be started. These two applications will talk to an external API in the exact same way. The current application uses React Query to do so. I’m thinking about creating some shared library/SDK/service (whatever you want to call it) so these two applications can consume the API in the same way. I guess tRPC solves this quite well in some respects, have the tRPC client be it’s own package, have the two applications consume it....
Solution:
I think there's nothing wrong with creating a reusable piece of logic, but you say make the client its own package. Why the client? Personally I'd create a private package with only the common logic in it as an async function. No reference to even trpc. Then I'd install this package on both applications, and invoke the library function in a procedure somewhere in a router. This way you can easily: - Transform the results further. - Have completely unique trpc configurations. ...

How to limit payload size?

Is there a way to limit the payload size? E.g., if a particularly large JSON payload is sent to the (standalone) server, the server should stop after having read a set number of bytes and fail the request.

Next.js app directory

Hi. I have been able to set up tRPC with the app directory in Next.js. It was a frustrating experience at first. You can find a simple setup here. I'm not sure if this is still the best way to do so, or that it has ever been for that matter. Could someone confirm if this is the way to do it right now, or if I'm missing out on some features this way? I found [this](https://github.com/trpc/trpc/tree/main/examples/.experimental/next-app-dir...

trpc error

Trpc not working in next js 14 app router. Please help me to resolve

Feature or happy accident: error message from `cause` being used as TRPC's error message

When a procedure throws a TRPC error with no message, it turns out that if the thrown error has a cause, and the cause has a message, the cause's message will be used as the TRPC error message returned to the client. Is this on purpose or an accident? Couldn't find this behavior documented.

When error in tRPC route, the error message is vauge

There are no line numbers when a runtime error happens in a tRPC route. Surely I am missing something? Would be impossible to debug larger apps.
No description

How to have 2 separate trpc clients for http and websockets?

I'm trying to have a setup where I can use trpc with 2 separate clients, one for normal HTTP requests and one for websockets since most of our site pages do not need a websocket connection. I have tried to create 2 files with 2 different createTRPCNext clients, it works fine in pages that don't use useSubscription but when using it, it looks like only the first trpc client that was initialized will be used, because even if I'm using the websocket client for useSubscription, I'm getting this error TRPCClientError: Subscriptions should use wsLink...

How to configure context for a standalone server?

Are there any examples out there on how to set up context for a standalone server? The docs do include an example for next , although it doesn't help much in setting one up for a standalone server. How is context set up for a standalone server?...
Solution:
Thanks, in the end I found it here, https://trpc.io/docs/server/adapters/standalone#2-configure-the-standalone-server The standalone config docs use the createContext function but don't define it, and the createContext docs define it but don't use it with createHTTPServer, so had to put both doc pages together to figure out how to do it for the standalone server...

Docs hard to follow with so much `next` code

Not having a great experience with the docs. There are many examples that use next, making it hard to understand what should be done in cases where next isn't being used. Take the docs on setting up the context: https://trpc.io/docs/server/context. They're entirely next focused. To what extent is Vercel's sponsorship affecting the docs?

adding information to the QueryKey that is not part of the procedure input

we have the situation that we have our trpc routes read some specific information from the browsesr url. In our case, it's the ID of the workspace the user is currently on. We use that information on the server to send API requests to other services. Now we'd want to automatically add this information to the cache key, because the same route should return different information for different workspace IDs, and since users can switch workspaces, showing stale data from an old workspace is not good. What we can do right now is add the workspaceId as a required input to each route, read it on the frontend with a custom hook and provide it as input. That works but is a bit cumbersome: We know that the url will always contain the id, and we know how to read from it on the server....
Solution:
Yeah this is suggested periodically, and I agree there's something a little awkward for type-safe cross-cutting concerns in tRPC, since headers are global and inputs can get repetitive, but adding to the input is the canonical way to achieve this because that way the input on the server has access to this. tRPC just isn't really a HTTP server so reading the URIs which I guess NextJS might have access to isn't really a 1st class concern Maybe there's something we could do around router-level inputs that are shared and can be set in a scoped way on the client, like orgScopedProjects = trpc.projects.setInputs({ organisationId })...

How to make typed error responses in the context of a specific query?

Each query has a typed (success) response, although it seems all queries have a shared type for the error response. Is there a way of having per-query typed error responses? Is there a better alternative to having an error within a successful response, as below? ```ts const appRouter = router({ foo: publicProcedure.query(() => {...

What's the negative code returned in error responses?

What's the negative error code returned in errors responses and how is it meant to be used by the client?
Solution:
tRPC is compliant with JSON-RPC and has specific error codes. You can find them here; The docs have a great explanation on handling errors....

`refetchOnWindowFocus` with RSC?

Hi. Is there any way to have refetchOnWindowFocus work with server components? Seems like it only available for client side. Perhaps I can use revalidatePath() to handle this? Not 100% sure what the best/optimal solution is....

Multi useSubscription with trpc react -query integration failed to find second sub

using trpc in nx monorepo, node version is 20 with yarn. backend is using trpc with express integration (both http and ws); this is the react-query trpc integration: import type { AppRouter } from '@home-web-bff';...

Is there any easy way to create a Type for a trpc router

I want to publish a set of trpc api client to npm, which will be used by my customer directly. But if I use typeof MyRouter to infer the Router type, my bundler (which is tsup) will bundle db schema types into the d.ts file. If I want my customer to gain the typesafe ability I have to make my db schema info public. Is there any way to define a Router type without typeof MyRouter type infer? So that I can expose limited info to public.

TRPC with Nextjs 13 App Router

Im trying to implement TRPC with my already existing Nextjs 13 App to get typesafe API's. I've managed to get it to work in Client Components, meaning i can make the API Calls from them but whenever I try to use them on a server rendered Page, I keep getting an error. In the tutorials I followed, it worked perfectly fine in the Server Rendered Page so I'm wondering how to fix this. Related Files: ```ts...

Security: DDOS attack prevention for open endpoints tRPC

So we are building an application and got multiple trpc endpoints associated with it, some of them are private and a few public ones (like login etc). While we can rate limit on the number of requests to prevent DDOS and similar attacks, the batch calls still stays vulnerable, what are the general methods to prevent these, and are there some precautions to take to limit the number of calls or access to these open endpoints...