Members: Recent Workbooks (JavaScript)

View as MarkdownOpen in Claude

This recipe demonstrates how to retrieve a list of workbooks, including the permissions set and last date accessed by user (memberId).

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 returns all the workbooks for a specified member, ordered by most recent
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 memberId = process.env.MEMBERID; // The unique identifier of the member whose type you want to update
14const baseURL = process.env.baseURL; // Your base URL
15
16// Define an asynchronous function to list recent documents and folders accessible to a specific member.
17async function listRecentDocuments() {
18 // Obtain a bearer token using the previously imported function.
19 const accessToken = await getBearerToken();
20 // If unable to obtain a token, log an error message and exit the function.
21 if (!accessToken) {
22 console.log('Failed to obtain Bearer token.');
23 return;
24 }
25
26 // Construct the URL for accessing the API endpoint that lists recent documents and folders.
27 const recentsURL = `${baseURL}/members/${memberId}/files/recents`;
28
29 console.log(`URL sent to Sigma: ${recentsURL}`); // Log the constructed URL before sending the request
30
31 try {
32 // Make a GET request to the specified URL, including the bearer token in the request headers for authentication.
33 const response = await axios.get(recentsURL, {
34 headers: {
35 'Authorization': `Bearer ${accessToken}`,
36 'Accept': 'application/json' // Specify the accepted response format
37 }
38 });
39
40 // Extract the entries array from the response data, which contains the documents and folders.
41 const entries = response.data.entries;
42
43 // Process each entry to extract and keep only the name, permission, and lastInteractionAt fields.
44 // Then, sort the processed entries by the lastInteractionAt field in descending order.
45 const processedEntries = entries.map(({ name, permission, lastInteractionAt }) => ({
46 name,
47 permission,
48 lastInteractionAt
49 })).sort((a, b) => new Date(b.lastInteractionAt) - new Date(a.lastInteractionAt));
50
51 // Log the processed and sorted entries to the console in a readable JSON format.
52 console.log("Recent documents and folders:", JSON.stringify(processedEntries, null, 2));
53 } catch (error) {
54 // If the request fails, log the error details.
55 console.error('Error listing recent documents:', error.response ? error.response.data : error);
56 }
57}
58
59// Execute the function to list recent documents and folders.
60listRecentDocuments();

Endpoints used

  1. Get authentication token: ${baseURL}/authURL
  2. List recent documents: ${baseURL}/members/${memberId}/files/recents