DataStax Node.js driver
Use the unified DataStax Node.js driver to connect to your DataStax Astra database and begin building your own application.
DataStax recommends using the unified DataStax Node.js driver. If you have an existing Apache Cassandra or DataStax Enterprise (DSE) Node.js driver, migrate the driver to a version that is capable of connecting to Astra databases. |
Prerequisites
-
Download the secure connect bundle to obtain connection credentials for your DataStax Astra database.
-
Download and install a current Node.js LTS version with
npm
. -
Client ID and Client Secret by creating your application token for your username and password.
Alternatively, have a teammate provide access to their Astra database. |
Procedure
-
Install the DataStax Node.js driver:
npm install cassandra-driver
-
Create a
connect-database.js
file in the main directory for your Node.js project.cd nodejsProject touch connect-database.js
-
Copy the following connection code into the
connect-database.js
file.Include the absolute path to the secure connect bundle for your Cassandra database (
secure-connect-database_name.zip
) in thesecureConnectBundle
parameter, as shown in the following examples.'use strict' const { Client } = require('cassandra-driver');
-
After the
Client
class, add the following code toconnect-database.js
. This code creates aClient
instance to connect to your Astra database, runs a CQL query, and prints the output to the console.async function run() { const client = new Client({ cloud: { secureConnectBundle: 'path/to/secure-connect-database_name.zip' }, credentials: { username: 'clientId', password: 'clientSecret' } }); await client.connect(); // Execute a query const rs = await client.execute('SELECT * FROM system.local'); console.log(`Hello from cluster: ${rs.first()['cluster_name']}`); await client.shutdown(); } // Run the async function run();
-
Save and close the
connect-database.js
file. -
Run the
connect-database.js
example with the Node.js runtime:node connect-database.js
The console output displays the cluster_name value from the system.local
table.