How do I run two APIs on button click?

On Appsmith, can we run two API on a button click?

You can add the following JS snippet on the onClick of the button:

{{ (function() {
  Api1.run();
  Api12run()
})() }}

More info can be found on - Writing Code - Appsmith

1 Like

As a little addition to @pawankumar’s comment (which would make Api1 and Api2 execute at the same time, i.e. in parallel), you can make the Apis run one after the other (in Series) with the following code:

{{ 
     Api1.run(  
         () => Api2.run()
     );
}}

NOTE: This approach ignores the error scenario (i.e. when Api1 fails). To include the error case, the code will be:

{{ 
   Api1.run( 
        () => Api2.run(), 
        () => { /* error logic goes here */ } 
   );
})

Which means Api2 will only run when Api1 succeeded. To run Api2 whether Api1 succeeds or fails, the code will be:

{{ 
   Api1.run( 
        () => Api2.run(), 
        () => Api2.run() 
   );
})

More info on this page.

Hope this helps.

6 Likes

This is very helpful with the different examples. Thank you @adebisi-fa !