How to Automatically Increment Page Number in a N8n Workflow for Pagination?

Hi Community,

I’m working on a workflow in N8n where I need to paginate through a set of data by incrementing a page number after each loop iteration. My goal is to generate paginated URLs where each URL corresponds to a different page of data.

Here’s how my current workflow is set up:

  1. Initial Setup:

    • I start with an initial page number set to 1 in the Edit Fields node.
    • The base URL and the page number are fed into a Loop Over Items node.
  2. Code Node:

    • Inside the Loop Over Items node, I use a Code node to generate the paginated URL and increment the page number. Below is the code I’m using:
    // Get the input data from the previous node
    const inputData = items[0].json;
    
    // Check if 'link' and 'page' are available in inputData
    if (!inputData.link || !inputData.page) {
      throw new Error("Missing 'link' or 'page' in input data");
    }
    
    // Extract the base URL from the input data
    const baseUrl = inputData.link;
    
    // Define the page number you want to generate a URL for
    let pageNumber = inputData.page;
    
    // Calculate the start value for the pagination (assuming 10 items per page)
    const startValue = (pageNumber - 1) * 10;
    
    // Generate the new paginated URL
    const newUrl = `${baseUrl}&start=${startValue}`;
    
    // Increment the page number for the next iteration
    pageNumber += 1;
    
    // Return the new URL and the incremented page number
    return [
      {
        json: {
          paginatedLink: newUrl,
          page: pageNumber
        }
      }
    ];
    
  3. Expected Behavior:

    • After the Code node runs, the page number should increment (e.g., from 1 to 2), and the workflow should continue to loop, generating a new URL for each subsequent page of data.
  4. Issue:

    • While the Code node generates the correct paginated URL and increments the page number, the page number in the Edit Fields node does not update. It stays at 1 instead of incrementing after each iteration.
  5. Workflow Design:

    • I’m connecting the output of the Code node back to the Edit Fields node to update the page number for the next iteration, but the page number doesn’t seem to persist or carry over between iterations.

My Questions:

  • How can I ensure that the page number updates correctly after each iteration in the workflow?
  • Is there a better way to design this workflow to handle pagination more effectively?
  • Am I missing a step or configuration that would allow the Edit Fields node to correctly update and persist the page number for the next loop?

Any guidance, tips, or examples of how to implement this would be greatly appreciated!

Thank you!

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hey @Rohit_Gurav,

Have you thought about using the pagination option in the HTTP Request node? It might make it a lot easier. Are you also able to share the actual JSON for your workflow so we can test it?

1 Like

@Jon here is the JSON

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.