> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://help.sigmacomputing.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://help.sigmacomputing.com/_mcp/server.

# Workbook: Pagination (JavaScript)

> This script fetches all workbooks from the Sigma API, handling pagination for large datasets.

This example script uses the list workbooks endpoint and the "page" query parameter to loop through all records until the end is reached, then displays the results in the browser console as a table.

Each section of the script has inline comments provided.

Refer to the [Sigma REST API Recipes](https://quickstarts.sigmacomputing.com/guide/developers_api_code_samples/index.html?index=../..index#0) QuickStart for step-by-step instructions.

## Example script

```javascript JavaScript
// This script will return specified fields for all workbooks, using pagination and format the response as a table.

// 1: Load environment variables from a specific .env file for configuration
require('dotenv').config({ path: 'sigma-api-recipes/.env' });

// 2: Import the function to obtain a bearer token from the authenticate-bearer module
const getBearerToken = require('../get-access-token');

// 3: Import Axios for making HTTP requests
const axios = require('axios');

// 4: Load use-case specific variables from environment variables
const baseURL = process.env.baseURL; // Your base URL

async function getAllWorkbooks() {
    let hasMore = true; // Initialize hasMore to true for the first request
    let nextPage = ''; // Initialize nextPage with an empty string for the first request
    let currentPage = 0; // Initialize currentPage counter
    let token = await getBearerToken(); // Obtain a bearer token for authentication

    
    if (!token) {
        console.error("Failed to obtain token, cannot proceed to fetch workbooks.");
        return; // Exit the function if token acquisition fails
    }

    while (hasMore) {
        try {
            currentPage++; // Increment the currentPage counter for each iteration
            const url = `${baseURL}/workbooks${nextPage ? '?page=' + nextPage : ''}`;
            console.log(`Fetching page ${currentPage} from Sigma: ${url}`); // Log the constructed URL before sending the request

            const response = await axios.get(url, {
                headers: { Authorization: `Bearer ${token}` } // Authorization header with the bearer token
            });

            // Process current page workbooks for table display
            console.log(`Workbooks on Page ${currentPage}:`);
            const workbooksForTable = response.data.entries.map((workbook, index) => ({
                "#": index + 1, // Sequence number within the current page
                Name: workbook.name, // Workbook name
                Path: workbook.path, // Workbook path
                LatestVersion: workbook.latestVersion // Latest version of the workbook
            }));

            console.table(workbooksForTable); // Display the current page workbooks in table format

            hasMore = response.data.hasMore; // Update hasMore based on the response
            nextPage = response.data.nextPage; // Update nextPage token/value for pagination

        } catch (error) {
            console.error('Error fetching workbooks:', error);
            break; // Exit the loop in case of an error
        }
    }
}

getAllWorkbooks().catch(error => {
    console.error('Failed to fetch workbooks:', error);
});

```

## Endpoints used

1. Get authentication token: `getBearerToken` function
2. List recent documents: `${baseURL}/members/${memberId}/files/recents`
3. List all workbooks: `${baseURL}/workbooks?page=${nextPage}`