Light Logo
JavaScript SDK (v0.39.1)Guides

Pagination

A guide on how to paginate through items using the WriftAI JavaScript client

Operations that return lists of resources from WriftAI's API use cursor‑based pagination, allowing you to fetch data in manageable chunks instead of retrieving everything at once.

This guide explains how to fetch the first page, request additional pages, and handle pagination cursors.

Fetch the first page

If you don't pass any options, the first page is returned.

const page = await wriftai.models.list();

Fetch the first page with a custom page size

You can control the number of items returned in each page.

const page = await wriftai.models.list({
  page_size: 15,
});

Fetch the next page

Use the next cursor returned in the page response.

if (page.next_cursor) {
  const nextPage = await wriftai.models.list({
    cursor: page.next_cursor,
  });
}

If next_cursor is null, you are on the last page.

Fetch the previous page

Use the previous page cursor from the page response.

if (page.previous_cursor) {
  const previousPage = await wriftai.models.list({
    cursor: page.previous_cursor,
  });
}

If previous_cursor is null, you are on the first page.

Iterate through all pages

You can loop through pages by repeatedly requesting the next page until no next cursor is returned.

let page = await wriftai.models.list();

while (page.next_cursor) {
  page = await wriftai.models.list({
    cursor: page.next_cursor,
  });

  // process page.items
}

Iterate backwards through pages

If you start from a page other than the first, you can page backwards using the previous cursor.

while (page.previous_cursor) {
  page = await wriftai.models.list({
    cursor: page.previous_cursor,
  });

  // process page.items
}