Light Logo
Go SDK (v0.41.1)Guides

Pagination

A guide on how to paginate through items using the WriftAI Go 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.

users, err := client.Users.List(
    context.TODO(),
    nil, // default pagination options
)

Fetch the first page with a custom page size

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

pageSize := 10
users, err := client.Users.List(
    context.TODO(),
    &wriftai.UserPaginationOptions{
        PaginationOptions: wriftai.PaginationOptions{
            PageSize: &pageSize,
        },
    },
)

Fetch the next page

Use the next cursor returned in the page response.

if page.NextCursor != nil {
    nextPage, err = client.Users.List(
        context.TODO(),
        &wriftai.UserPaginationOptions{
            PaginationOptions: wriftai.PaginationOptions{
                Cursor: page.NextCursor,
            },
        },
    )
}

If NextCursor is nil, you are on the last page.

Fetch the previous page

Use the previous page cursor from the page response.

if page.PreviousCursor != nil {
    previousPage, err = client.Users.List(
        context.TODO(),
        &wriftai.UserPaginationOptions{
            PaginationOptions: wriftai.PaginationOptions{
                Cursor: page.PreviousCursor,
            },
        },
    )
}

If PreviousCursor is nil, 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.

page, err := client.Users.List(context.TODO(), nil)

for page.NextCursor != nil {
    page, err = client.Users.List(
        context.TODO(),
        &wriftai.UserPaginationOptions{
            PaginationOptions: wriftai.PaginationOptions{
                Cursor: page.NextCursor,
            },
        },
    )
    // 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.

for page.PreviousCursor != nil {
	page, err = client.Users.List(
		context.TODO(),
		&wriftai.UserPaginationOptions{
			PaginationOptions: wriftai.PaginationOptions{
				Cursor: page.PreviousCursor,
			},
		},
	)
	// process page.Items
}