Connect with the Node.js driver
DataStax recommends the Data API and clients for Serverless (Vector) databases. You can use the Data API to perform CQL operations on your table data in Serverless (Vector) databases. DataStax recommends drivers only for Serverless (Non-Vector) databases, legacy applications that rely on a driver, or for CQL functions that aren’t supported by the Data API. For more information, see Compare connection methods. |
To use the DataStax Node.js driver, you need to install the driver and its dependencies, and then connect the driver to your Astra DB Serverless database. Once connected, you can write scripts that use the driver to run commands against your database.
Prerequisites
-
Set the following environment variables:
-
ASTRA_DB_ID
: The database ID -
ASTRA_DB_REGION
: A region where your database is deployed and where you want to connect to the database, such asus-east-2
-
ASTRA_DB_KEYSPACE
: A keyspace in your database, such asdefault_keyspace
-
ASTRA_DB_APPLICATION_TOKEN
: An application token with the Database Administrator role.The
token.json
has the following format:{ "clientId": "CLIENT_ID", "secret": "CLIENT_SECRET", "token": "APPLICATION_TOKEN" }
For driver authentication, you can use either
clientId
andsecret
or the literal stringtoken
and theAstraCS
token value. If you are on an older driver version that doesn’t support thetoken
option, then you might need to useclientId
andsecret
. For more information, see Token details.
-
-
Download your database’s Secure Connect Bundle (SCB).
-
Install Node.js LTS version with
npm
.
Install the Node.js driver
-
Install the DataStax Node.js driver:
npm install cassandra-driver
Make sure you use a driver version that is compatible with Astra DB. For more information, see DataStax driver matrix.
Connect the Node.js driver
-
In the root of your Node.js project, create a
connect-database.js
file:cd nodejsProject touch connect-database.js
-
Copy the following connection code into the
connect-database.js
file, and then replacePATH_TO_SCB
with the absolute path to your database’s Secure Connect Bundle (SCB) (secure-connect-DATABASE_NAME.zip
):connect-database.jsconst cassandra = require('cassandra-driver'); const cloud = { secureConnectBundle: "**PATH_TO_SCB**" }; const authProvider = new cassandra.auth.PlainTextAuthProvider('token', process.env['ASTRA_DB_APPLICATION_TOKEN']); const client = new cassandra.Client({ cloud, authProvider }); async function run() { await client.connect(); // ... }
This code creates a
Client
instance to connect to your Astra DB database, through which you can run commands against your database. -
Save and then run
connect-database.js
with the Node.js runtime to test the connection:node connect-database.js
Run commands with the Node.js driver
After you connect to the database, you can use the driver to perform operations on your database. For example, the following code connects to an Astra DB database, runs a CQL query, and then prints the output to the console:
// Import libraries and connect to the database
const cassandra = require('cassandra-driver');
const cloud = { secureConnectBundle: "**PATH_TO_SCB**" };
const authProvider = new cassandra.auth.PlainTextAuthProvider('token', process.env['ASTRA_DB_APPLICATION_TOKEN']);
const client = new cassandra.Client({ cloud, authProvider });
async function run() {
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();
}
You can extend or modify this example script to run other commands against your database or connect to other databases. For more examples and information, see Node.js driver quickstart, Node.js driver documentation, and Developing applications with DataStax drivers.