Workbook: Shared with Me (JavaScript)

View as MarkdownOpen in Claude

This script retrieves all workbooks shared to a specific user based on their memberId. It retrieves the user’s files, filters out the workbooks, and then lists the workbook names, URLs, and version numbers.

Each section of the script has inline comments provided.

Refer to the Sigma REST API Recipes QuickStart for step-by-step instructions.

Example script

JavaScript
1// This script lists all workbook names, URLs, and version numbers for the specified memberId
2
3// 1: Load environment variables from a specific .env file for configuration
4require('dotenv').config({ path: 'sigma-api-recipes/.env' });
5
6// 2: Import the function to obtain a bearer token from the authenticate-bearer module
7const getBearerToken = require('../get-access-token');
8
9// 3: Import Axios for making HTTP requests
10const axios = require('axios');
11
12// 4: Load use-case specific variables from environment variables
13const baseURL = process.env.baseURL; // Your base URL
14const memberId = process.env.MEMBERID; // Retrieve the memberId from .env
15
16// Function to fetch all items with limit = 500
17async function fetchMemberFiles(memberId, accessToken) {
18 try {
19 const fullUrl = `${baseURL}/members/${memberId}/files?typeFilters=workbook&limit=500`;
20 console.log(`Fetching member files: ${fullUrl}`);
21 const response = await axios.get(fullUrl, {
22 headers: {
23 'Authorization': `Bearer ${accessToken}`,
24 },
25 });
26
27 return response.data.entries;
28 } catch (error) {
29 console.error('Error fetching member files:', error);
30 return [];
31 }
32}
33
34// Main function to list workbooks accessible to a specific member
35async function listAccessibleWorkbooks() {
36 const accessToken = await getBearerToken();
37 if (!accessToken) return;
38
39 try {
40 // Fetching all URL IDs from the member's files
41 const memberFiles = await fetchMemberFiles(memberId, accessToken);
42 const memberFileIds = memberFiles.map(file => file.id);
43
44 // Fetching all workbooks with a manual limit of 200 rows
45 const workbooksUrl = `${baseURL}/workbooks?limit=200`;
46 const response = await axios.get(workbooksUrl, {
47 headers: {
48 'Authorization': `Bearer ${accessToken}`,
49 },
50 });
51 const allWorkbooks = response.data.entries;
52
53 // Filtering workbooks accessible to the member
54 const accessibleWorkbooks = allWorkbooks.filter(workbook => {
55 // Check if the workbookId is in the member's file IDs
56 return memberFileIds.includes(workbook.workbookId);
57 });
58
59 console.log(`Fetched ${accessibleWorkbooks.length} workbooks.`);
60
61 // Displaying the filtered workbooks
62 if (accessibleWorkbooks.length > 0) {
63 accessibleWorkbooks.forEach((workbook, index) => {
64 console.log(`#${index + 1}: Name: ${workbook.name}, URL: ${workbook.url}, Latest Version: ${workbook.latestVersion}`);
65 });
66 } else {
67 console.log('No matching workbooks found for this member based on workbookIds.');
68 }
69 } catch (error) {
70 console.error('Error listing accessible workbooks:', error);
71 }
72}
73
74// Execute the function to list accessible workbooks
75listAccessibleWorkbooks();

Endpoints used

  1. Get authentication token: getBearerToken function
  2. Fetch member files: ${baseURL}/members/${memberId}/files?typeFilters=workbook&limit=500
  3. Get all workbooks: ${baseURL}/workbooks?limit=200