How do you hit trpc endpoints via http request?

I am fairly new to trpc. It's awesome when consuming the API through the client. But what if I want to test the API via postman or make a get request using the browser? For example, with a normal next api route, I could go to localhost:3000/api/hello and it would return the data. Can I do something similar with trpc endpoints? I tried going to localhost:3000/api/trpc/example/hello but that did not work. Thanks in advance!
17 Replies
royanger
royanger2y ago
trpc has one endpoint. What is the goal?
Will
WillOP2y ago
Let's say I have created a router, itemsRouter. itemsRouter has a list method. I want to be able to make an http request to retrieve that data something like localhost:3000/api/trpc/items/list
royanger
royanger2y ago
Why are you making a http request? You can already do stuff like this https://github.com/trpc/zart where you might have two apps using the TRPC stuff (next and expo in this case).
Will
WillOP2y ago
just so I can test the response
royanger
royanger2y ago
I've never needed to test that way.
Will
WillOP2y ago
it's pretty common to be able to want to test your api via http request I think it's a valid question
Will
WillOP2y ago
@iDarkLightning I actually read through that, but those docs don't really go over how to do it in the context of a next app like in t3 it mentions: Given a router like this exposed at /api/trpc: but that didnt seem to work when I tried it so I'm wondering if there's a t3 specific way to do it
iDarkLightning
Go to your browser network tab, and see the request its making. Imitate that request
Will
WillOP2y ago
oh good idea ah
iDarkLightning
It is an HTTP route, make a fetch request. T3 doesn't add anything on top of any of these libraries, it just composes them.
Will
WillOP2y ago
http://localhost:3000/api/trpc/tasks.create its using the dot notation
royanger
royanger2y ago
I hadn't stumbled on that. Interesting to know, but definitely not something I've needed yet. Thanks @iDarkLightning
Will
WillOP2y ago
iDarkLightning
np
Will
WillOP2y ago
thanks @iDarkLightning!
iDarkLightning
np
Want results from more Discord servers?
Add your server