Here is an example of a looping api endpoint. In this case, it is importing contacts from Google, and creating a contact record in Bubble for each contact received. The use case is that because the user may have thousands of contacts in Google, we want to break it into manageable chunks.
Let's first create an endpoint that will be used to create a single contact record. Our looping endpoint (which we will create next) will call this endpoint and pass it the values needed to create the contact:
Next we create the looping endpoint used to break into chunks (import_contacts.) We pass to this endpoint the values needed, which in this example are a starting point for the import and the maximum number of contacts to retrieve.
The first step in the loop is to call a Google api and get a list of contacts. We pass to that api a starting point, and how many to retrieve.
Step 2 schedules our contact creation endpoint on a list. The list we give to it, is the contacts received in step 1:
Step 3 then schedules the execution of the looping endpoint (itself) but only does so when the total results is greater than what we have previously processed. In this example, the Google api provides us with the total results available for our query (not to be confused with max results which is the number of results we ask for.) So the calculation is our current starting point plus the number of results we get in each batch must be <= to total results. If it is, that means we have more results to fetch, otherwise we have received the last batch and won’t schedule it again. We also increment our start index, which is how you loop.
And finally, we can just trigger the import with a button, setting the starting point and max results:
Looking to accelerate your app development?
Let me turn
Development through Coaching at https://uniqueideas.com or schedule a free intro session