How to loop "n" times, request API value and sum the value?

Hello!

I am trying to figure out if there is a way to make calls to an API endpoint n times, changing the request param each time and summing up a return value from the API request to arrive at a total. Any help or pointers will be appreciated. Specifics below.

My app hits a set of public APIs that are exposed by a fitness device. I am trying to figure out the total calories burned for a given user, which is the sum of the individual calories burned per workout. The challenge is that the way the API is architected, there isn’t one call that gives me this number. It takes 4 calls (one of which has a pagination limit of 100 and many users have more than 100 workouts). Here is the sequence of calls. I have 1 & 2 complete and am unsure how to accomplish 3 &4 in Adalo.

  1. Hit API endpoint #1 to get UUID of the user (pass user input username + password). This is straightforward and I hit the first API endpoint and store this in the “User” table when a new user account is created

  2. Use UUID to get the total number of workouts (You’ll see why I need this when we get to the next step). This part is also straightforward. I pass the UUID i stored for the logged in user to endpoint #2 and get back a single value for total workouts (total_workout_count)

  3. Next up is an API endpoint that returns a list of all the workouts a user has completed. Unfortunately, while this returns a workout_id (UUID equivalent for each workout completed by the user) per workout, it does not return the calories expended. To complicate things further, this has a pagination limit of 100 results per page, with the page # being passed as an input param. My plan here, is to take the total_workout_count value I stored in 2 and divide that by 100 and round up to the next whole number. This is the number of pages of workout data this particular user has. I then have to make n calls to this endpoint #3, each time incrementing the page # which would match the iteration count. As an example, if a user has completed 250 workouts, 250/100=2.5, which rounds up to 3. I would make 3 calls to endpoint #3, first two would return 100 workout IDs each and the 3rd would return the final 50.

  4. Assuming I’m able to do #3, the last thing is to hit a final API endpoint #4 that takes a specific workout ID and returns the calories expended for that workout. What I need to do here, is take each of the workout IDs returned in step 3 above, repeatedly call this API endpoint 4 (it would be 250 times if the user has completed 250 workouts for example). I would have to keep adding the returned calorie count to a stored value to compute a cumulative total.

As I mentioned, I have completed up to step 2.5. I have the total number of workouts and the total number of pages (for endpoint number 3). I’m trying to figure out answers to these questions
a. How do I call endpoint #3 a variable number of times, based on how many workouts the user has completed?
b. Should I store all the workout IDs in the database (if so, what’s the best way to do this? - should there be one table per user? keep in mind that this thing could have 100s of thousands of users) Or aternatively, should I not store them and instead, just make API call number 4 as soon as I have each workout ID (if so what’s the best approach here - a “formula” that triggers “actions”?)
c. How to build a cumulative total? I guess this relatively straightforward, if I can figure out a and b above. It would just be a simple formula that updates a db field by adding to its previous value.

Thanks in advance for any help or pointers :pray: :pray: :pray:

Anil

Just an update that I couldn’t figure out a graceful way of achieving this natively in Adalo so I’m learning how Xano works as a way of decoupling the DB and business logic from the Adalo frontend and hoping that it provides more flexibility

1 Like

Xano is great for loops and recursive workflows. Hoping you’ve found success with it and if you have any questions please reach out to us in our community or support!

Were you able to figure this out in Xano? I’m running into the same problem in Xano with a paginated API and I need to be able to ROUNDUP from the total number of records, then divide by 100, to determine how many loops to run. If you solved it and you could let me know how that would be awesome! Thanks.