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
trpc has one endpoint. What is the goal?
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
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).
just so I can test the response
I've never needed to test that way.
it's pretty common to be able to want to test your api via http request
I think it's a valid question
HTTP RPC Specification | tRPC
Methods Type mapping
@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
Go to your browser network tab, and see the request its making. Imitate that request
oh good idea
ah
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.
http://localhost:3000/api/trpc/tasks.create
its using the dot notationI hadn't stumbled on that. Interesting to know, but definitely not something I've needed yet. Thanks @iDarkLightning
so it would have been http://localhost:3000/api/trpc/example.hello
np
thanks @iDarkLightning!
np