Given an API with pagination, each API call returns all entries within a page, the page count and current page. I want to have 2 loops: the inner loop process the batch data somehow, and the outer loop iterates along the pages until exhaustion. After inner loop processing, on branch ‘done’, I am able to set an ‘if’-block to check if current page is equal to number of pages. However, I am not aware how to define and increment a variable which correspond to current page, which will be plugged on url to perform the next request.
Ensure to copy your n8n workflow and paste it in the code block, that is in between the pairs of triple backticks, which also could be achieved by clicking </> (preformatted text) in the editor and pasting in your workflow.
```
<your workflow>
```
That implies to any JSON output you would like to share with us.
Make sure that you have removed any sensitive information from your workflow and include dummy or pinned data with it!
I do not see the need in the loop at all. HTTP Request has built-in pagination feature, which you have to configure correctly, HTTP Request node documentation | n8n Docs. Once all the data retrieved (all pages returned) you simply use another HTTP Request node that you added to the Loop.
That is, you simply need two HTTP Request nodes, no need to the Loop node.
My API has a page query parameter. By passing the option ‘page’ on query option of HTTP Request node with certain value e.g. 4, still constrains me to previous issue of predefined page count iteration.
Bonus question: After retrieving a single page content, the loop does not iterate along each item when I press ‘Test step’ rather retrieves the first item over and over again. It is frustrating.
Hey @brunolnetto , it is a matter of setting up the pagination correctly. Could you share your HTTP Request node configuration and the example of the output produced by the first page retrieved?
Sure. You can replace the credentials on edit block by test credentials json below. They will not affect any customer neither generate financial burdens to anyone, so why not. Also, on block ‘Post contact’, I tried to plug the block ‘Edit’ to retriece the required key chatwoot_token, but I could just get keys from either the loop or the key-dictionary.
Note the starting point is https://api.suasvendas.com/v2/Contato/?pagina=1 and the pagination is done by incremeting pagina with help of the expression {{ $pageCount + 1 }}. The condition for the pagination completion is an empty body returned by the API.
I’m not sure I follow. Why paginate if you want only three items? What do you mean by “count”?
You might not need the loop at all. Items are processed one by one by default. What do you intend to do next with all those items you retreived with pagination technique?
The goal is bring all contacts from a base to another, in this case, “Contato” (Brazilian portuguese for singular word ‘Contact’). Since importing all at once for testing is not very productive, I wish to just limit to 3 contacts after pagination for import purposes (I can remove them manually afterwards to avoid duplication error). Then, I retrieve the contact data from the payload list and build the respective body for insertion.