Connections: List all (Javascript)

View as Markdown

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
1// This script lists all connections in alphabetically order by name
2
3// Load environment variables from a specific .env file for configuration
4require('dotenv').config({ path: 'sigma-api-recipes/.env' });
5
6// Import the function to obtain a bearer token from the authenticate-bearer module
7const getBearerToken = require('../get-access-token');
8
9// Import Axios for making HTTP requests
10const axios = require('axios');
11
12// Load use-case specific variables from environment variables
13const baseURL = process.env.baseURL; // Your base URL
14
15// Define an asynchronous function to fetch and sort connections
16async function listConnections() {
17 const accessToken = await getBearerToken();
18 if (!accessToken) {
19 console.error('Failed to obtain Bearer token.');
20 return;
21 }
22
23 try {
24 const endpoint = `${baseURL}/connections?includeArchived=false`;
25 console.log(`Fetching connections from: ${endpoint}`);
26
27 // API request to fetch connections
28 const response = await axios.get(endpoint, {
29 headers: { 'Authorization': `Bearer ${accessToken}`, 'Accept': 'application/json' },
30 });
31
32 const connections = response.data.entries; // Access 'entries' field in response
33
34 console.log('Raw Response:', response.data); // Debugging log to confirm data structure
35
36 if (connections && connections.length > 0) {
37 // Sort connections alphabetically by name
38 const sortedConnections = connections.sort((a, b) =>
39 a.name.localeCompare(b.name)
40 );
41
42 // Display sorted connections
43 sortedConnections.forEach((connection, index) => {
44 console.log(`#${index + 1}: Name: ${connection.name}, ID: ${connection.connectionId}, Type: ${connection.type}`);
45 });
46 } else {
47 console.log('No connections found.');
48 }
49 } catch (error) {
50 console.error('Error fetching connections:', error.message);
51 if (error.response) {
52 console.error('Response Data:', error.response.data);
53 }
54 }
55}
56
57// Execute the function to list connections if this script is run directly
58if (require.main === module) {
59 listConnections();
60}
61
62// Export the listConnections function for reuse in other modules
63module.exports = listConnections;
Response Example
1#1: Name: Sigma Sample Database, ID: 10eed7b7-4a10-4c40-802b-xxxxxxxxxx, Type: snowflake

Endpoints used

  1. Get authentication token: getBearerToken function
  2. List connections