# How to Set Up Pagination in your Flow?

This guide walks you through configuring your flow to fetch records in chunks (e.g., 1000 at a time) using a loop that continues until all records are retrieved.

## **Step 1: Initialize Pagination Variables**

Start your flow by adding a **Flow Variable Initializer** component to define:

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2FBpxmz2Gpq2L61uAtUaPg%2Fimage.png?alt=media&#x26;token=cf094808-2ebb-4e56-8dca-60932e1d7b1c" alt=""><figcaption></figcaption></figure>

* `batchSize`: The number of records to fetch in one iteration (e.g., `1000`)
* `skipToken`: The initial offset value (set to `0`)

## **Step 2: Configure the Loop for Pagination**

In your main loop (e.g., when pulling records from Acumatica):

* Set the loop to **continue while records are returned**.
* Use the pagination variables to limit and offset your record fetch.

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2FHIC2xZbtt3LyGZGXHHVZ%2Fimage.png?alt=media&#x26;token=55675417-db6e-4028-bcf6-e961c4be3d9d" alt=""><figcaption></figcaption></figure>

Inside the loop:

* Add the **Get Records** action (e.g., from the Acumatica connector).

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2FOBvS5lCptXYjuCQlXa3V%2Fimage.png?alt=media&#x26;token=3bcbe862-e8e7-4609-bf55-85db351b98ba" alt=""><figcaption></figcaption></figure>

* In its parameter mapping:
  * Set `top` to `{{var.batchSize}}`
  * Set `skip` (or offset) to `{{var.skipToken}}`

This setup fetches records in chunks, e.g., the first 1000, then the next 1000, and so on.

## **Step 3: Update `skipToken` After Each Iteration**

After fetching records in a loop iteration:

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2FWmLRfOruiEtNxPphNqDE%2Fimage.png?alt=media&#x26;token=07696d47-641e-4ea2-8542-83591a51ff16" alt=""><figcaption></figcaption></figure>

* Add a **Variable Connector**
* Add the `skipToken` variable to update.
* Click the three-dot menu in the configure tab.

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2F3QMAcRcOxVvDzsVOI1M9%2Fimage.png?alt=media&#x26;token=ac9002c7-8883-4190-90d4-4f47b67c660a" alt=""><figcaption></figcaption></figure>

* Choose **Advanced Action → Mathematical Expression**
* Set the expression as:

```handlebars
{{var.skipToken}} + {{var.batchSize}}
```

{% hint style="info" %}
Ensure both `skipToken` and `batchSize` are of type `integer` to support arithmetic operations.
{% endhint %}

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2F7WkSFICe33vmjwyOm0Dv%2Fimage.png?alt=media&#x26;token=773e067d-1808-492a-baae-66aa3ed9a8b3" alt=""><figcaption></figcaption></figure>

This ensures:

* First iteration: `skip = 0`
* Second iteration: `skip = 1000`
* Third iteration: `skip = 2000`

...and so on.

## **Step 4: Handle Loop Exit When No More Records**

When the **Get Records** action returns an **empty list**, the loop will automatically stop.

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2FFFf2xlWsnlO3brXseKFG%2Fimage.png?alt=media&#x26;token=26373afc-61b0-45a3-b7e2-60b309b3a6eb" alt=""><figcaption></figcaption></figure>

You can optionally add a **Filter** or a conditional **Switch** to handle this case explicitly by checking if the returned array is empty.

## **Step 5: Test the Flow with a Known Dataset**

Use a known dataset (e.g., an Acumatica table with 22,000+ records) for testing:

1. Run the SQL count in the connected DB to validate the total:

   ```sql
   SELECT COUNT(*) FROM acumatica_custom_pricing_testtenantddp;
   ```
2. Ensure the full record count (e.g., `22,388`) is fetched in batches.

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2Fx60i6IsxrBkXuNS6uZ0e%2Fimage.png?alt=media&#x26;token=58dcb5c5-1aa8-4be0-a2a7-7cecdc01c687" alt=""><figcaption></figcaption></figure>

* Check the **Logs** tab in Fastn:
  * Filter logs by tenant
  * Inspect the duration and status of each batch

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2FCjDqBg6Nr3m3F1C5Vn8W%2Fimage.png?alt=media&#x26;token=a2b2a7cd-27d4-407b-a0f0-344691502abc" alt=""><figcaption></figcaption></figure>

### Summary

| Parameter   | Purpose                   | Mapping                                 |
| ----------- | ------------------------- | --------------------------------------- |
| `top`       | Batch size per fetch      | `{{var.batchSize}}`                     |
| `skip`      | Offset for pagination     | `{{var.skipToken}}`                     |
| `skipToken` | Updated after every batch | `{{var.skipToken}} + {{var.batchSize}}` |

**Pagination is commonly supported by many APIs that return large datasets, including those that follow patterns like OData.** This makes the approach effective for systems such as Acumatica, Dynamics, and many others with batch data access capabilities.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.fastn.ai/flows/tutorials/flow-customization-and-operations/how-to-set-up-pagination-in-your-flow.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
