Hi i am new to this retool app and I am having a doubt regarding one restAPI calls another one.
In the submit button of my form, i added one of the eventHandler for the Submit is to trigger the RestApI listUser. Before running this RestQuery, i want to trigger another RestApi "generate_OTP", and the output of this restAPi (generate_OTP) will be a token(say Token A) and this Token A should be added as the header of the first RestQuery APi (listUser) and then only I need to execute the listUser API.
I tried to trigger generate_OTP first in eventHandler for the Submit button in my form, then I trigger listUser API, but it is not working as expected. Is there any option to do this. Everytime, the listuser API is triggered, it should generate a new token and then it should be added to the header
Thanks in Advance.
All of the events on a query are run at the same time. If you need them to be sequential, have the Submit event trigger the "generate_OTP" API, add then add a success event to "generate_OTP" to trigger the "listUser" API.
Thanks for addressing the query John @jg80 . But If I am having multiple apis (just like listuser , say createUSER, updateUser) and for all these apis i need the output of generate_OTP as the header, is there any way to address the requirement.
Depending on how fine-grained you need the control to be, I think you have three main approaches:
- One API needs to run first, then all bunch of other APIs dependent on that initial API can all run at once: In this case, trigger the initial API from some event in the UI, and then add a trigger for each of the dependent APIs on the success of the initial API. The order you add them doesn't matter and they will all run right away, probably dependent on the result of the initial API.
- There are a series of APIs that need to run in a simple logical sequence: In this case, each API in the sequence should have a trigger for the next API added to the success events. That way, each API will wait for the one before it to finish before executing, which is probably what you want because each of them in the chain relies on one (or more) of the upstream API(s).
- There is a branching and conditionals and sequence isn't linear: you can write a JS query that controls the process (including async/await/etc.) to make sure jobs are all done and promises returned at the appropriate times.
I find it helps me to clarify what I am trying to accomplish by explain the series of calls with a process diagram or dependency chain diagram of some sort.
p.s. there is a fourth possibility, which is "you should create a workflow" and control it from there, returning all of the information back to the app when it is done. If it is getting too complex in the app, it might help to break that piece of logic out.
Thanks John , I will try to do the changes according to your suggestions.
Hi @Arathy Hope you were able to solve this already!
I was also wondering if the generate OTP query is something you could add as a custom authentication step? Docs here: Custom API authentication | Retool Docs Typically, you can have a custom auth step that gets a token & then you can pass that dynamic token as a variable to your headers on your resource. Custom auth is set up on the resource page (in this case, the resource for the api that has the user info) and then automatically applied to queries on that resource.
Thank you @Tess . I will try to add as a custom authentication step.