DataStax Node.js driver

Before using a DataStax driver, review Best practices for DataStax drivers and upgrade or install the latest supported driver for your language. For more details about supported software, see the DataStax Support Policy.

Upgrade your driver to a compatible version to connect to Astra DB databases. For more information, see the DataStax Driver Matrix.

Connect the Node.js driver

  1. Create a database, and then set environment variables for database ID, region, and keyspace.

  2. Create an application token, and then set a token environment variable.

    The token.json has the following format:

    {
      "clientId": "CLIENT_ID",
      "secret": "CLIENT_SECRET",
      "token": "APPLICATION_TOKEN"
    }

    For authentication with the driver, you can use either clientId and secret or the literal string token and the AstraCS token value. If you are on an older driver version that doesn’t support token and AstraCS, then you might need to use clientId and secret.

  3. Download your database’s Secure Connect Bundle (SCB).

  4. Install Node.js LTS version with npm.

  5. Install the DataStax Node.js driver:

    npm install cassandra-driver
  6. Create a connect-database.js file in the main directory for your Node.js project:

    cd nodejsProject
    touch connect-database.js
  7. Copy the following connection code into the connect-database.js file:

    Include the absolute path to the Secure Connect Bundle (SCB) for your Astra DB database (secure-connect-DATABASE_NAME.zip) in the secureConnectBundle parameter, as shown in the following examples:

    'use strict'
    const { Client } = require('cassandra-driver');
  8. After the Client class, add the following code to connect-database.js. This code creates a Client instance to connect to your Astra DB database, runs a CQL query, and prints the output to the console.

    async function run() {
      const client = new Client({
        cloud: { secureConnectBundle: 'PATH_TO_SCB' },
        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();
  9. Save and close the connect-database.js file.

  10. 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.

Connect the Node.js Cloud driver (deprecated)

The legacy Stargate APIs and their associated drivers are deprecated for Astra DB Classic as of September 2024, and end-of-life (EOL) is scheduled for the end of 2025. As EOL approaches, DataStax will provide migration information to support your transition to other options.

If you have questions or concerns, contact your account representative or DataStax Support.

  1. Create a keyspace.

  2. Create a table for your keyspace.

  3. Install stargate-grpc-node-client using either npm or yarn:

    • npm

    • Yarn

    npm i @stargate-oss/stargate-grpc-node-client
    yarn add @stargate-oss/stargate-grpc-node-client
  4. While running Stargate on Astra DB, generate a token for your database, and then add the token to the connection portion of the script.

    The Bearer Token to use in the header of API calls is the same as your database’s application token, which is prefixed by AstraCS:, followed by a generated alphanumeric string.

    // DB configuration
    // replace with values from the Astra Portal
    const astra_uri = "DATABASE_ID-REGION_NAME.apps.astra.datastax.com:443";
    const bearer_token = "APPLICATION_TOKEN";
    
    // Set up the authentication
    // For Astra DB: Enter the database's application token for the bearer token
    const bearerToken = new StargateBearerToken(bearer_token);
    const credentials = grpc.credentials.combineChannelCredentials(
      grpc.credentials.createSsl(), bearerToken);
    
    // Uncomment if you need to check the credentials
    //console.log(credentials);

    For a connection to a remote Stargate instance, like Astra DB, automatically generate on every call to the client:

    // Create the gRPC client
    // For Astra DB: passing the credentials created above
    const stargateClient = new StargateClient(astra_uri, credentials);
    
    console.log("made client");
    
    // Create a promisified version of the client, so we don't need to use callbacks
    const promisifiedClient = promisifyStargateClient(stargateClient);
    
    console.log("promisified client")

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2024 DataStax | Privacy policy | Terms of use

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com