Workbook: Initiate Materialization Job (JavaScript)
Workbook: Initiate Materialization Job (JavaScript)
This script demonstrates how to start an existing materialization job, for a specified workbook and retrieves its status until the job is complete.
Refer to the Sigma REST API Recipes QuickStart for step-by-step instructions.
Example script
JavaScript
1 // This script starts a materialization job for a specified workbook and retrieves its status 2 3 // 1: Load environment variables from a specific .env file for configuration 4 require('dotenv').config({ path: 'sigma-api-recipes/.env' }); 5 6 // 2: Import the function to obtain a bearer token from the authenticate-bearer module 7 const getBearerToken = require('../get-access-token'); 8 9 // 3: Import Axios for making HTTP requests 10 const axios = require('axios'); 11 12 // 4: Load use-case specific variables from environment variables 13 const baseURL = process.env.baseURL; // Your base URL 14 const workbookId = process.env.WORKBOOK_ID; // Workbook ID for materialization 15 16 // Function to start a materialization job 17 async function startMaterialization() { 18 try { 19 const accessToken = await getBearerToken(); 20 if (!accessToken) { 21 console.error('Failed to obtain Bearer token.'); 22 return; 23 } 24 25 // Fetch materialization schedules to get the correct sheet ID 26 const materializationSchedulesURL = `${baseURL}/workbooks/${workbookId}/materialization-schedules`; 27 console.log(`URL sent to Sigma: ${materializationSchedulesURL}`); 28 const response = await axios.get(materializationSchedulesURL, { 29 headers: { 30 'Authorization': `Bearer ${accessToken}` 31 } 32 }); 33 34 // Parse the sheet ID from the response if schedules are available 35 const materializationSchedules = response.data.entries; 36 if (materializationSchedules.length === 0) { 37 console.error('No materialization schedules found for the specified workbook.'); 38 return; 39 } 40 41 const sheetId = materializationSchedules[0].sheetId; 42 console.log(`Starting materialization job for workbook ${workbookId} and sheet ${sheetId}...`); 43 44 const materializationsURL = `${baseURL}/workbooks/${workbookId}/materializations`; 45 console.log(`URL sent to Sigma: ${materializationsURL}`); 46 const startResponse = await axios.post(materializationsURL, { 47 sheetId: sheetId 48 }, { 49 headers: { 50 'Authorization': `Bearer ${accessToken}`, 51 'Content-Type': 'application/json' 52 } 53 }); 54 55 console.log('Materialization job started successfully:', startResponse.data); 56 57 // Retrieve materialization status 58 const materializationId = startResponse.data.materializationId; // Correctly extract the materialization ID 59 await checkMaterializationStatus(materializationId, accessToken, materializationsURL); 60 } catch (error) { 61 console.error('Error starting materialization job:', error.response ? error.response.data : error); 62 } 63 } 64 65 // Function to check materialization status 66 async function checkMaterializationStatus(materializationId, accessToken, materializationsURL) { 67 try { 68 const materializationStatusURL = `${materializationsURL}/${materializationId}`; 69 console.log(`URL sent to Sigma: ${materializationStatusURL}`); 70 console.log(`Checking materialization status for materialization ID: ${materializationId}`); 71 72 const response = await axios.get(materializationStatusURL, { 73 headers: { 74 'Authorization': `Bearer ${accessToken}` 75 } 76 }); 77 78 console.log('Materialization status:', response.data.status); 79 80 // Check if the materialization status is "ready" 81 if (response.data.status === 'ready') { 82 console.log('Materialization job completed successfully.'); 83 return; // Stop the script execution 84 } 85 86 // Check status periodically until it's either completed or failed 87 if (response.data.status !== 'COMPLETE' && response.data.status !== 'FAILED') { 88 await new Promise(resolve => setTimeout(resolve, 5000)); // Wait for 5 seconds 89 await checkMaterializationStatus(materializationId, accessToken, materializationsURL); 90 } 91 } catch (error) { 92 console.error('Error checking materialization status:', error.response ? error.response.data : error); 93 } 94 } 95 96 // Execute the script 97 startMaterialization();
Endpoints used
- Get authentication token:
getBearerTokenfunction - List materialization schedules:
${baseURL}/workbooks/${workbookId}/materialization-schedules - Start a materialization job:
${baseURL}/workbooks/${workbookId}/materializations - Check status of a materialization job:
${baseURL}/workbooks/${workbookId}/materializations/${materializationId}

