> 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: List All (JavaScript)

> This script fetches all workbooks accessible to a specific member based on their memberId.

This script fetches all workbooks from a specified base URL, with pagination, and lists the workbook name, URL, URL ID, and latest version.

If the script is run directly, it lists the workbooks, otherwise, the script exports the function for reuse in other modules.

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 lists all workbooks, returning name, URL, URL ID, and version

// 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

// Define an asynchronous function to fetch workbooks with pagination
async function fetchWorkbooksWithPagination(url, accessToken) {
    let results = [];
    let nextPageToken = ''; // Initialize pagination token
    do {
        const fullUrl = `${url}${nextPageToken ? `?page=${nextPageToken}` : ''}`;
        console.log(`Fetching: ${fullUrl}`);
        try {
            const response = await axios.get(fullUrl, {
                headers: { 'Authorization': `Bearer ${accessToken}` },
            });
            results = [...results, ...response.data.entries];
            nextPageToken = response.data.nextPage; // Update the nextPageToken with the next page value
        } catch (error) {
            console.error(`Error fetching workbooks: ${error}`);
            break; // Exit loop on error
        }
    } while (nextPageToken); // Continue fetching pages until there's no nextPageToken
    return results;
}

// Define an asynchronous function to list workbooks
async function listWorkbooks() {
    const accessToken = await getBearerToken();
    if (!accessToken) {
        console.error('Failed to obtain Bearer token.');
        return;
    }

    try {
        // Fetch all workbooks with pagination
        const workbooks = await fetchWorkbooksWithPagination(`${baseURL}/workbooks`, accessToken);

        if (workbooks.length > 0) {
            // Display information for each workbook
            workbooks.forEach((workbook, index) => {
                console.log(`#${index + 1}: Name: ${workbook.name}, URL: ${workbook.url}, URL ID: ${workbook.workbookUrlId}, Latest Version: ${workbook.latestVersion}`);
            });
        } else {
            console.log('No workbooks found.');
        }
    } catch (error) {
        console.error('Error listing workbooks:', error);
    }
}

// Execute the function to list workbooks if this script is run directly
if (require.main === module) {
    listWorkbooks();
}

// Export the listWorkbooks function for reuse in other modules
module.exports = listWorkbooks;
```

## Endpoints used

1. Get authentication token: `getBearerToken` function
2. List workbooks with pagination: `${baseURL}/workbooks?page=${nextPageToken}`