Snowflake - how to connect to Snowflake using NodeJS

Viewed 3461

Does anyone has a working code to connect NodeJS and Snowflake.

Tried following instructions for NodeJS:

var snowflake = require('snowflake-sdk');

var connection = snowflake.createConnection({
  account: 'account1',
  username: 'user1',
  password: 'pass1',
  region: 'us-east-1'
});

connection.connect(function(err, conn) {
  if (err) {
    console.error('Unable to connect: ' + err.message);
  } else {
    console.log('Successfully connected as id: ' + connection.getId());
  }
});

Keep getting errors:

Network error. Could not reach Snowflake.

Similarly - following instructions for Python works without problem (using EXACT same user/pass/account etc. as for NodeJS):

import snowflake.connector

ctx = snowflake.connector.connect(
    user='user1', 
    password='pass1',
    account='account1'
)

print ("SELECT current_version():")
cs = ctx.cursor()
try:
    cs.execute("SELECT current_version()")
    one = cs.fetchone()
    print(one[0])  # 2.50.2
finally:
    cs.close()
5 Answers

The account that use in python has "account_id.region" something like

ctx = snowflake.connector.connect(
user='user1', 
password='pass1',
account='abc.us-east-1'
)

For node.js you need to give the same as:

var connection = snowflake.createConnection({
  account: 'abc',
  username: 'user1',
  password: 'pass1',
  region: 'us-east-1'
});

my config worked for me:

 var connection = snowflake.createConnection({
   account: 'abc.us-east-1',
   username: 'user1',
   password:'pass1',
   authenticator: 'SNOWFLAKE',
   clientSessionKeepAlive: true,
});

connection.connect( 
    function(err, conn) {
        if (err) {
            console.error('Unable to connect: ' + err.message);
            } 
        else {
            console.log('Successfully connected to Snowflake.');
            // Optional: store the connection ID.
            connection_ID = conn.getId();
            }
        }
    );

Based strictly on code provided, the functional difference between the 2 examples is that the Node.js one specifies the us-east-1 region, and the Python one does not specify a region. The default region is us-west-2, which does not need to be specified. The linked documentation notes: "use only if your account is not in US West". So specifying an incorrect region could cause connection errors.

I had to add the cloud provider as well as the region, so in my case it was:

const connection = snowflake.createConnection({
   account: 'abc.us-east-1.aws',
   username: 'user1',
   password:'pass1',
   authenticator: 'SNOWFLAKE'
});

Related