Hello there, I get trouble merging my outputs after looping an API call. My original goal is running an api multiple times and then merge all of the data together.
Here is my workflow, I use a conditional switch node to loop the HTTP node, and each run returns a different output. After looping the switch the result will go into the Final Page branch.
Here is the issue, since the final output will go into Final Page branch as all other previous pages go into Next Page branch, I can not access Next Page branch outputs from the other branch Final Page. In this case, I can only get the final iteration’s data.
I have two possible idea but I don’t know how to proceed:
- Save the previous execution’s output before the next call. If this is possible how and where do I set up this node to save the data iteratively.
- Merge all of the data from both Final Page and Next Page branch. Still it doesn’t work if I simply applies a merge node to Final Page and Next Page branch.
Output ScreenShot (Only last Run has Final Page Item and Previous Run has Next Page Item):
Method 2 Flow ScreenShot (The merge iterates for every run, but data does not merge):
Have you tried HTTP Request node’s pagination functionality to simplify this (in Options)?
If you want to maintain your structure, you should do the following:
Use a Function node to accumulate data manually
This pattern is based on saving the accumulated data in a variable ($item.json.allData or similar) in each loop cycle.
Example of a Function Node that accumulates:
Place it before entering the switch, or just after the HTTP Request:
// Get previous data (if it exists)
let allData = $items("Function")[0]?.json?.allData || [];
allData.push(...$json.inventory); // or whatever your field to accumulate is
return [{
json: {
allData: allData
}
}];
Then in the last step, extract allData.
The workflow would be similar to:
A([Trigger]) → B[Function: Init Accumulator (Run Once)] → C[HTTP Request: Call API with pagination] → D[Function: Accumulate Data (Run Once)] → E[If: hasNextPage?] → F[Loop back to C if yes] → G[Function: Final Output (Run Once)]
Thanks for your reply!
I tried add an Accumulate node to save data, but for each run this code node will be refreshed and data is not saved then. Can you check if I didn’t the accumulation right?
And do you think if I can get all of the iterations at Code3 node by accessing “Process Response1” node and then merge the data instead of accessing the Accumulate node(to get whole accumulated output)?
The flow structure should look like this:
- [Trigger]
- [Function - Init Accumulator] → allData =
- [HTTP Request - Paged]
- [Function - Accumulate Data] → Push to the allData array
- [IF - Are there more pages?]
└── Yes → Return to step 3
└── No → Continue
- [Function - Final Result] → Returns allData
I just follow your structure and set up a init accumulator to save all the iterated output. However, I still can not get the data saved.
I set up this at the init accumulator node.
{
"cursor": "",
"hasNextPage": true,
"allData": []
}
and I get the previous saved data by this. But I get nothing as for allData. May I know how can I save the data correctly?
let allData = $('Initialize Accumulator Step2').first().json.allData
Thanks for the reply. I just tried applying pagination at HTTP directly but I was stuck how to set up the correct cursor. Can you give me some idea regarding these two questions:
- will the paginated HTTP run multiple times and return multiple outputs directly?
- and how can I set up the cursor correctly? I am using GraphQL at Shopify Admin API.
This is my node and how I set up the cursor and stop condition.
Change to the following:
let allData = $('Initialize Accumulator Step2').first().json.allData;
For this:
let allData = $input.first().json.allData || [];
In this case I will get the data from the last node which is ProcessResponse2 (Node cleaning HTTP result); and it should be equal to input.all() which returns HTTP result.
Do you mean to get all of the items (17 items for example) returned by HTTP? but can I get it at current run?
If you are still meaning save all the results at the variable created by “Init Accumulator”, how can I get it correctly and append data to this variable correctly?
Sorry for so many follow up questions, and thank you very much for your patience and help.
In Accumulation Step 4, we need to validate if more pages need to be processed.
Modify based on your logic:
let allData = $input.first().json.allData || [];
const newData = $input.all().map(item => item.json.inventory).flat();
allData.push(...newData);
// Determine if there is a next page (this may vary depending on your API)
const hasNextPage = $input.first().json.hasNextPage; // or some cursor logic
return [{
json: {
allData,
hasNextPage
}
}];
Now, with this, the expected structure for Switch 2:
Condition: $json.hasNextPage == true
- If Yes: return to Shopify Inventory Step 3
- If No: skip to the final Code node
How do you know it’s correct?
- When Switch2 receives only 1 item, not 17.
- When the “Next Page” branch is triggered only if hasNextPage is true, and when it is false, it continues to the final output.