---
title: "Pagination"
description: "Read forward and backward through cursor-based list responses."
---

> Documentation Index
> Fetch the complete documentation index at: https://docs.affinityrx.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Pagination

Await a list call to read one page, or use async iteration to read across pages.
Each page has `data` and `hasMore` fields.

## Iterate resources

```typescript
for await (const practice of affinity.practices.iterate({ limit: 25 })) {
  console.log(practice.id, practice.name);
}
```

The SDK fetches the next page as needed and preserves your filters.
Breaking the loop stops further page requests. Iteration is forward-only; use explicit list calls for backward paging.
List endpoints without cursor parameters return an ordinary response.

## Collect a bounded array

```typescript
const practices = [];
for await (const practice of affinity.practices.iterate({ limit: 25 })) {
  practices.push(practice);
  if (practices.length === 100) break;
}
```

Stop the loop when you have collected the required number of records.

## Read the first page

```typescript
const page = await affinity.forPractice(practiceId).orders.list({
  limit: 25,
});
```

## Read the next page

Pass the last resource ID to `startingAfter`.

```typescript
if (page.hasMore && page.data.length > 0) {
  const nextPage = await affinity.forPractice(practiceId).orders.list({
limit: 25,
startingAfter: page.data.at(-1)!.id,
  });
}
```

## Read the previous page

Pass the first resource ID to `endingBefore`.

```typescript
if (page.data.length > 0) {
  const previousPage = await affinity.forPractice(practiceId).orders.list({
limit: 25,
endingBefore: page.data[0]!.id,
  });
}
```

Do not pass `startingAfter` and `endingBefore` in the same request.

Source: https://docs.affinityrx.com/guides/reference/sdks/typescript-pagination/index.mdx
