How would you make requests to an Open API that follows traditional REST using t3 stack
Hello! I was wondering how I would go about fetching data from an open API that follows the traditional RESTful format using trpc. I would normally go about this with simple axios calls on the client, but I figured that might not be standard practice and wanted to get some insight on what would be the best way to make calls to an external api in the T3 stack.
9 Replies
OpenAPI is a spec
Not a hard contract
You can generate types from a spec
But that doesn't mean that server that you are requesting is respecting the format
You can generate a OpenAPI spec from trpc
But the opposite isn't that type safe
I think I might be using the incorrect term. I just meant a any publicly accessible API. and in my case, uses REST routes, but I was confused to utilize those in the t3 stack
you could call it in the front end with react-query, but more likely you would want to call it in the backend from a tRPC query
so call the tRPC endpoint in the frontend, and then call the API with fetch within the tRPC end point, return the result of the fetch with tRPC
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
If you want to generate OpenAPI spec that's a great project:https://github.com/jlalmes/trpc-openapi
GitHub
GitHub - jlalmes/trpc-openapi: OpenAPI support for tRPC 🧩
OpenAPI support for tRPC 🧩. Contribute to jlalmes/trpc-openapi development by creating an account on GitHub.
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
do you want to fetch the data from the frontend or the backend?
in both cases you do it exactly the same way as you always would
frontend: react query if you want loading state, caching, etc or fetch if you just want to get the data, axios works as well of course
backend: fetch or axios or whatever
tRPC allows your own frontend and your own api to talk to each other in a convenient way
it has no opinions about how you talk to other external stuff
I apologize for the late response, but thank you for all the answers. seems like theres some mixed opinions about how I should approach it, which probably just means I can go about it in any way and ultimately be fine LOL i think my biggest concern was whether or not there was a standard practice for this and i think that question was properly addressed. thank you everyone!
The best way heavily depends on your use cases, no norm exists (or SHOULD exist)
What service are you calling?
Does the GDPR apply to you?
In the latter case you will probably prefer to proxy the request via ur own backend to not expose your users ip to the third party
Is CORS an issue with the third party? … i could go on, but this wont help you