External calls
Hi! I am using T3 stack, tRPC together with clerk, and i need to create users within the app, being already logged in, so i need to use external API, quiestion is, what are best practices, doing external API calls inside trpc route? or do it on component side
11 Replies
@aidean0406 were you able to get answers somewhere ?
nah but i figured it out myself
what was your conclusion ?
call should be inside trpc, or anywhere on "server" side, as u will need to send them token inside request
my component is client side component using trpc so there is no othey way than having it in one of trpc route
it works super fine, in case of problem with request i am throwing simply
5XX
error that users couldn't be created so further part of logic which then do relation in db is not being executedi was using exactly same approach as yours, but it didn't sound right. and then there is this message from a contributor.
the solution where you create trpc server function just to call an external api sounds really hacky and not natural.
so the logic is
on UI
-> fill in user information
-> call trpc users route
create
method
on Server
-> method inside first try to create users in clerk using fetch
-> await response, check if all good
-> save created user in my db using with additional property clerkUserId
from the response of fetch
@mfaizan1 it depends, if u call public extarnal API then it's fine, u can use react query, but if that's the request where u need to pass some "tokens" which are private ones, u can't use react query, as then me using ur app can steal itahh got it. thanks for discussion.
cuz i will see it simply in network tab in request, of the headers that will include ur private tokens
got it. that's a really good point.
you could do a fetch, inside "server" component, instead of trpc, but then if ur logic later on in trpc actually depends on response, then it doesn't make sense to have call in component await and then pass it to trpc route 😄 as in my case once i got response i need to save those users in my db as well 😄 so it very valid to have it all together inside the method of trpc route