Dynamic API parameter and using ':split by' to parse Bubble DB data pulled from external API

Creating an example to illustrate my issue…

Let’s say that I want to get data on dogs from an external API, and then create a new Dog in my Bubble database for each dog available for me to pull from the API. Data must be retrieved from the API by page, with 10 Dogs on each page. However, when I pull the data from the API for a single page into my Bubble database, instead of one dog on each line…

  • Golden Retriever
  • German Shepherd
  • Poodle

…what I get is:

  • Golden Retriever, German Shepherd, Poodle

What I mean is that when I make my API call, I am unable to tell the API to “give me 5 dogs” - rather, I have to instead tell it to “give me page N of dogs”.

I am solving for 2 problems:

  1. Creating one record for each Dog retrieved from the external API source
  2. Looping through multiple pages to retrieve all Dogs from the external API without needing to create one step in a workflow for each page pulled

Sharing an older thread I found for context: Split by? Parse array? - #6 by randyw

Any insights would be incredibly helpful!

1 Like

Firstly you need to loop through all pages of data and append it all together in one place.

Store the total number of dogs in a state and have a current state as 1

Once you have done this create a backend loop which creates a new record for each dog when the current <= total, split by , item (current)

Then schedule itself again but change current to current+1

I probably haven’t explained it the best, but let me know if you need any further help.

How should I use custom states in conjunction with backend API workflows? I’ve tried passing values (e.g., ID or page number) through API keys and custom states but haven’t been able to find their intersection when trying to use them together.

You may be best to append all data into a state and then once completed save the value to the database. Then build a workflow which loops through this data and creates a new record for each split, maybe send me some screenshots of what you have so far.

I was able to figure it out.

In summary, I dump all the data from my GET request into a single record, then use that record as a reference in my API workflow to create one new record for each item in the original record. I do this using a single parameter for the API workflow. This parameter is referenced twice: first for parsing a specific item in the list of data returned by the GET request and second when the API workflow is rescheduled by itself (the parameter is increased by 1). Increasing the parameter by 1 each time the API workflow runs (upon rescheduling) allows me to incrementally index different Things in the GET record and pick a different Thing during each iteration of the API workflow.

Workflow 1 (on button click - client-side)

  1. GET request to API
  2. Create a new Thing
    a. for each {{parameter}}: Result of Step 1’s Things:items until #10’s {{paramter}}
  3. Schedule API Workflow
    a. {{counter}} = 1

Workflow 2 (API workflow - server-side)

This workflow has one parameter, which is a number value, that is used to keep track of how many loops the workflow has run.

  1. Create a new Thing
    a. for each parameter: Search for Thing with Arbitrary text (, ):first item’s {{paramater}}:split by (, ):item #{{counter}}
  2. Schedule API workflow > {{counter}} = {{counter}} + 1
1 Like

A really important note:

The API workflow must not be exposed as a public workflow and Parameter definition: Manual definition. This is required to pass the {{counter}} parameter when the API workflow is scheduled.

1 Like

Brilliant! I am glad you managed to work it out, that was roughly what I was getting at but explained really badly, my apologies lol.

And yes that is a great point and one I forget about many times lol.

1 Like