Adrian Lysakowski
Adrian Lysakowskiβ€’12mo ago

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
Faizan
Faizanβ€’11mo ago
@aidean0406 were you able to get answers somewhere ?
Adrian Lysakowski
Adrian Lysakowskiβ€’11mo ago
nah but i figured it out myself
Faizan
Faizanβ€’11mo ago
what was your conclusion ?
Adrian Lysakowski
Adrian Lysakowskiβ€’11mo ago
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 executed
Faizan
Faizanβ€’11mo ago
i 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.
No description
Adrian Lysakowski
Adrian Lysakowskiβ€’11mo ago
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 it
Faizan
Faizanβ€’11mo ago
ahh got it. thanks for discussion.
Adrian Lysakowski
Adrian Lysakowskiβ€’11mo ago
cuz i will see it simply in network tab in request, of the headers that will include ur private tokens
Adrian Lysakowski
Adrian Lysakowskiβ€’11mo ago
No description
Faizan
Faizanβ€’11mo ago
got it. that's a really good point.
Adrian Lysakowski
Adrian Lysakowskiβ€’11mo ago
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