Workbook: Export to CSV with Date Range Parameters (JavaScript)
Workbook: Export to CSV with Date Range Parameters (JavaScript)
This recipe demonstrates how to trigger an export job for a specific workbook and includes a parameter-based date range for the data.
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
// This script triggers an export job in Sigma and downloads the export once ready.// Load environment variables from a specific .env file for configurationrequire('dotenv').config({ path: 'sigma-api-recipes/.env' });// Import the function to obtain a bearer token from the authenticate-bearer moduleconst getBearerToken = require('../get-access-token');// Import Axios for making HTTP requestsconst axios = require('axios');// Load necessary modules for file handlingconst fs = require('fs');const path = require('path');// Load use-case specific variables from environment variablesconst baseURL = process.env.baseURL; // Base URL for the Sigma APIconst workbookId = process.env.WORKBOOK_ID; // Workbook ID from which to export dataconst elementId = process.env.ELEMENT_ID; // Element ID within the workbook to target for exportasync function initiateExport(accessToken) {// Prepare the options for the export request with correct format and filtersconst exportOptions = {elementId: elementId,format: { type: 'csv' }, // Define the export formatparameters: {"DateFilter":"min:2024-04-16,max:2024-04-17" // Define the date range for the export},runAsynchronously: true // Request the export to run asynchronously};console.log('Final export options:', JSON.stringify(exportOptions, null, 2));try {const response = await axios.post(`${baseURL}/workbooks/${workbookId}/export`,exportOptions,{ headers: { Authorization: `Bearer ${accessToken}`, 'Content-Type': 'application/json' } });console.log('Export initiated successfully, response:', response.data);return response.data.queryId; // Extract and return the query ID from the response} catch (error) {console.error('Failed to initiate export:', error);return null;}}async function checkExportReady(queryId, accessToken) {// Continuously check if the export is ready for downloadconsole.log(`Checking export readiness for queryId: ${queryId}`);while (true) {try {const response = await axios.get(`${baseURL}/query/${queryId}/download`,{ headers: { Authorization: `Bearer ${accessToken}` }, responseType: 'stream' });if (response.status === 200) { // Check if the export is readyconsole.log('Export is ready for download.');return response.data;} else {console.log(`Received unexpected status code: ${response.status}`);}} catch (error) {if (error.response && error.response.status === 204) {// Export not ready yet, wait before retryingconsole.log('Export is not ready yet. Waiting to retry...');await new Promise(resolve => setTimeout(resolve, 10000));} else {console.error('Failed to check export status:', error);return null;}}}}async function downloadExport(data, filename) {// Handle the download of the export fileconst filePath = path.join(__dirname, filename);const writer = fs.createWriteStream(filePath);return new Promise((resolve, reject) => {data.pipe(writer);let error = null;writer.on('error', err => {error = err;writer.close();reject(err);});writer.on('finish', () => {if (!error) {console.log(`Export downloaded successfully to: ${filePath}`);resolve(true);}});});}async function exportWorkflow() {// Main workflow to manage the export processconst accessToken = await getBearerToken();if (!accessToken) {console.error('Failed to obtain bearer token.');return;}const queryId = await initiateExport(accessToken);if (!queryId) {console.error('Failed to initiate export or obtain queryId.');return;}const data = await checkExportReady(queryId, accessToken);if (data) {await downloadExport(data, 'export.csv');} else {console.error('Failed to prepare the export for download.');}// Forcibly exit the process if the script is still hangingprocess.exit(0);}exportWorkflow();
Endpoints used
- Get authentication token:
getBearerTokenfunction - Initiate an export:
${baseURL}/workbooks/${workbookId}/export - Check the export status and download the file:
${baseURL}/query/${queryId}/download

