Connections: List all (Javascript)
Connections: List all (Javascript)
This script lists all connections in alphabetical order by name.
Refer to the Sigma REST API Recipes QuickStart for step-by-step instructions.
Example script
JavaScript
// This script lists all connections in alphabetically order by name// 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 use-case specific variables from environment variablesconst baseURL = process.env.baseURL; // Your base URL// Define an asynchronous function to fetch and sort connectionsasync function listConnections() {const accessToken = await getBearerToken();if (!accessToken) {console.error('Failed to obtain Bearer token.');return;}try {const endpoint = `${baseURL}/connections?includeArchived=false`;console.log(`Fetching connections from: ${endpoint}`);// API request to fetch connectionsconst response = await axios.get(endpoint, {headers: { 'Authorization': `Bearer ${accessToken}`, 'Accept': 'application/json' },});const connections = response.data.entries; // Access 'entries' field in responseconsole.log('Raw Response:', response.data); // Debugging log to confirm data structureif (connections && connections.length > 0) {// Sort connections alphabetically by nameconst sortedConnections = connections.sort((a, b) =>a.name.localeCompare(b.name));// Display sorted connectionssortedConnections.forEach((connection, index) => {console.log(`#${index + 1}: Name: ${connection.name}, ID: ${connection.connectionId}, Type: ${connection.type}`);});} else {console.log('No connections found.');}} catch (error) {console.error('Error fetching connections:', error.message);if (error.response) {console.error('Response Data:', error.response.data);}}}// Execute the function to list connections if this script is run directlyif (require.main === module) {listConnections();}// Export the listConnections function for reuse in other modulesmodule.exports = listConnections;
Response Example
#1: Name: Sigma Sample Database, ID: 10eed7b7-4a10-4c40-802b-xxxxxxxxxx, Type: snowflake
Endpoints used
- Get authentication token:
getBearerTokenfunction - List connections

