# Pagination - SDK examples

Here are Python, Java, JavaScript/Node.js, and C# code examples of paging through items (in this case, workspace references) using Smartsheet SDKs. The examples use [token-based pagination](/api/smartsheet/guides/basics/pagination#token-based-pagination).

## Python SDK

Here's Python code that uses the Smartsheet Python SDK to page through Smartsheet workspaces.


```python
import smartsheet

client = smartsheet.Smartsheet(
    access_token='YOUR_API_TOKEN', # Replace with your token value
)

last_key = None
while True:
    page = client.Workspaces.list_workspaces(
        pagination_type='token',
        max_items=100,
        last_key=page.last_key
    )
    assert isinstance(page, smartsheet.models.IndexResult)

    print(page.to_dict()) # Do what you want with the page items

    last_key = getattr(page, "last_key", None)
    if not last_key:
        break
```

The `list_workspaces(...)` call returns up to 100 workspace references.

The `assert isinstance(page, smartsheet.models.IndexResult)` call checks that the response matches the expected schema (that is, `smartsheet.models.IndexResult`). If it doesn't match, the function raises an `AssertError`. Otherwise, processing continues.

> **Note:** The `# Do what you want ...` comments mark optimal places for processing the workspace references.


If the page includes a `last_key` attribute, there are more workspace references that you can retrieve.

The looping continues and calls `list_workspaces(...)` to get pages of data until the response no longer includes a `last_key` attribute.

## JavaScript SDK (can use in Node.js)

Here's JavaScript code that uses the Smartsheet JavaScript SDK to page through Smartsheet workspaces.

> **Note:** You can use the JavaScript SDK in Node.js apps.



```javascript
import smartsheet from "smartsheet";

const client = smartsheet.createClient({
  accessToken: "YOUR_API_TOKEN", // Replace with your token value
});

try {
  let lastKey = '';
  do {
    const page = await client.workspaces.listWorkspaces({
      queryParameters: { 
        paginationType: "token",
        maxItems: 100,
        lastKey },
    });

    console.log(page); // Do what you want with the page items

    lastKey = page?.lastKey;
  } while (lastKey);
} catch (err) {
  console.error(err);
  process.exit(1);
}
```

The `listWorkspaces(...)` call returns up to 100 workspace references.

> **Note:** The `# Do what you want ...` comments mark optimal places for processing the workspace references.


If the page includes a `lastKey` attribute, there are more workspace references that you can retrieve.

The loop continues and calls `listWorkspaces(...)` until the response no longer includes a `lastKey` attribute.

## Java SDK

Here's Java code that uses the Smartsheet Java SDK to page through Smartsheet workspaces.


```java
// Required imports

import com.smartsheet.api.Smartsheet;
import com.smartsheet.api.SmartsheetException;
import com.smartsheet.api.SmartsheetFactory;
import com.smartsheet.api.models.PagedResult;
import com.smartsheet.api.models.PaginationParameters;
import com.smartsheet.api.models.Workspace;

// Put the following code your class

Smartsheet client = SmartsheetFactory.createDefaultClient("YOUR_API_TOKEN");

String lastKey = null;
do {
    PagedResult<Workspace> page = client.workspaceResources()
            .listWorkspaces(
                new PaginationParameters("token", lastKey, 100));

    for (Workspace workspace : page.getData()) {
        System.out.println("Workspace: " + workspace.getName() + " (ID: " + workspace.getId() + ")");
    }

    lastKey = page.getLastKey();
} while (lastKey != null && !lastKey.isBlank());
```

It calls `listWorkspaces(...)` with the API token, the last key (`null` to get the first results page), and `100` as the maximum number of items on a page. The call returns up to 100 workspace references.

If the page includes a non-null `lastKey` attribute, there are more workspace references that you can retrieve.

The loop continues and calls `listWorkspaces(...)` until the response's `lastKey` attribute is `null`.

## C# SDK

Here's C# code that uses the Smartsheet C# SDK to page through Smartsheet workspaces.


```c#
using Smartsheet.Api;
using Smartsheet.Api.Models;

try
{
    SmartsheetClient client = new SmartsheetBuilder()
        .SetAccessToken("YOUR_API_TOKEN")
        .Build();

    string? lastKey = null;
    do
    {
        TokenPaginatedResult<Workspace> page = client.WorkspaceResources.ListWorkspaces(
            new ListWorkspacesTokenPaginationParameters(lastKey, 100, "token"));

        Console.WriteLine(page); // Do what you want with the page items

        lastKey = page.LastKey;
    } while (!string.IsNullOrEmpty(lastKey));
}
catch (Exception ex)
{
    Console.WriteLine($"SmartsheetException occurred: {ex.Message}");
}
```

First it calls `ListWorkspaces(...)` with the API token, `null` for the lastKey, and `100` as the maximum number of items on a page. The call returns up to 100 workspace references.

> **Note:** The `# Do what you want ...` comments mark optimal places for processing the workspace references.


If the page includes a non-null `lastKey` attribute, there are more workspace references that you can retrieve.

The loop continues and calls `ListWorkspaces(...)` until the response's `lastKey` attribute is `null`.

You've now examined C#, Java, JavaScript, and Python code examples that demonstrate paging through workspace references. You can use the same approach on other operations that support [token-based pagination](/api/smartsheet/guides/basics/pagination#token-based-pagination).

## Related content

- [Pagination](/api/smartsheet/guides/basics/pagination)
- JavaScript SDK project
- Java SDK project
- Java SDK - Javadocs
- Python SDK documentation
- C# SDK documentation