Best-pratice for HTTP-Request that include pagination

I’ve come up with an even better solution as I’m learning the ins and outs of n8n. This one only requires a single HTTP node, and IF node and a function node to combine the results.

{
    "nodes": [
        {
            "parameters": {
                "authentication": "oAuth2",
                "url": "https://connect.squareup.com/v2/catalog/list",
                "options": {},
                "headerParametersUi": {
                    "parameter": [
                        {
                            "name": "Content-type",
                            "value": "application/json"
                        }
                    ]
                },
                "queryParametersUi": {
                    "parameter": [
                        {
                            "name": "types",
                            "value": "ITEM,ITEM_VARIATION,CATEGORY,IMAGE,ITEM_OPTION,ITEM_OPTION_VAL,DISCOUNT,TAX,MODIFIER,MODIFIER_LIST"
                        },
                        {
                            "name": "cursor",
                            "value": "={{$json.cursor || \"\"}}"
                        }
                    ]
                }
            },
            "name": "Fetch Page",
            "type": "n8n-nodes-base.httpRequest",
            "typeVersion": 1,
            "position": [
                150,
                300
            ],
            "executeOnce": true,
            "credentials": {
                "oAuth2Api": "Square"
            }
        },
        {
            "parameters": {
                "functionCode": "const allItems = []\nlet counter = 0\n\nwhile (true) {\n  try {\n    $items(\"Fetch Page\", 0, counter).map(i => i.json.objects.map(o => allItems.push(o)))\n  } catch (err) {\n    break\n  }\n  counter++\n}\n\nreturn allItems.map(o => ({json: o}))\n\n"
            },
            "name": "Combine Results",
            "type": "n8n-nodes-base.function",
            "typeVersion": 1,
            "position": [
                350,
                300
            ]
        },
        {
            "parameters": {
                "conditions": {
                    "string": [
                        {
                            "value1": "={{$json.cursor || \"\"}}"
                        }
                    ]
                }
            },
            "name": "Empty Cursor",
            "type": "n8n-nodes-base.if",
            "typeVersion": 1,
            "position": [
                150,
                120
            ]
        }
    ],
    "connections": {
        "Fetch Page": {
            "main": [
                [
                    {
                        "node": "Empty Cursor",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Empty Cursor": {
            "main": [
                [
                    {
                        "node": "Combine Results",
                        "type": "main",
                        "index": 0
                    }
                ],
                [
                    {
                        "node": "Fetch Page",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        }
    }
}
3 Likes