Quickstart: Build a Cassandra app with Node.js SDK and Azure Cosmos DB


In this quickstart, you create an Azure Cosmos DB Cassandra API account, and use a Cassandra Node.js app cloned from GitHub to create a Cassandra database and container. Azure Cosmos DB is a multi-model database service that lets you quickly create and query document, table, key-value, and graph databases with global distribution and horizontal scale capabilities.


If you don't have an Azure subscription, create a free account before you begin. Alternatively, you can Try Azure Cosmos DB for free without an Azure subscription, free of charge and commitments.

In addition, you need:

Create a database account

Before you can create a document database, you need to create a Cassandra account with Azure Cosmos DB.

  1. In a new browser window, sign in to the Azure portal.

  2. In the left menu, select Create a resource.

    Create a resource in the Azure portal

  3. On the New page, select Databases > Azure Cosmos DB.

    The Azure portal Databases pane

  4. On the Create Azure Cosmos DB Account page, enter the settings for the new Azure Cosmos DB account.

    Setting Value Description
    Subscription Your subscription Select the Azure subscription that you want to use for this Azure Cosmos DB account.
    Resource Group Create new

    Then enter the same name as Account Name
    Select Create new. Then enter a new resource group name for your account. For simplicity, use the same name as your Azure Cosmos account name.
    Account Name Enter a unique name Enter a unique name to identify your Azure Cosmos DB account. Your account URI will be cassandra.cosmos.azure.com appended to your unique account name.

    The account name can use only lowercase letters, numbers, and hyphens (-), and must be between 3 and 31 characters long.
    API Cassandra The API determines the type of account to create. Azure Cosmos DB provides five APIs: Core (SQL) for document databases, Gremlin for graph databases, MongoDB for document databases, Azure Table, and Cassandra. You must create a separate account for each API.

    Select Cassandra, because in this quickstart you are creating a table that works with the Cassandra API.

    Learn more about the Cassandra API.
    Location Select the region closest to your users Select a geographic location to host your Azure Cosmos DB account. Use the location that's closest to your users to give them the fastest access to the data.
    Capacity mode Provisioned throughput or Serverless Select Provisioned throughput to create an account in provisioned throughput mode. Select Serverless to create an account in serverless mode.

    Select Review+Create. You can skip the Networking, Backup, Encryption and Tags section.

    The new account page for Azure Cosmos DB

  5. The account creation takes a few minutes. Wait for the portal to display the page saying Congratulations! Your Azure Cosmos DB account was created.

Clone the sample application

Now let's clone a Cassandra API app from GitHub, set the connection string, and run it. You see how easy it is to work with data programmatically.

  1. Open a command prompt. Create a new folder named git-samples. Then, close the command prompt.

    md "C:\git-samples"
  2. Open a git terminal window, such as git bash. Use the cd command to change to the new folder to install the sample app.

    cd "C:\git-samples"
  3. Run the following command to clone the sample repository. This command creates a copy of the sample app on your computer.

    git clone https://github.com/Azure-Samples/azure-cosmos-db-cassandra-nodejs-getting-started.git
  4. Install the Node.js dependencies with npm.

    npm install

Review the code

This step is optional. If you're interested to learn how the code creates the database resources, you can review the following snippets. The snippets are all taken from the uprofile.js file in the C:\git-samples\azure-cosmos-db-cassandra-nodejs-getting-started folder. Otherwise, you can skip ahead to Update your connection string.

  • The username and password values were set using the connection string page in the Azure portal.

      let authProvider = new cassandra.auth.PlainTextAuthProvider(
  • The client is initialized with contactPoint information. The contactPoint is retrieved from the Azure portal.

    let client = new cassandra.Client({
        contactPoints: [`${config.contactPoint}:10350`],
        authProvider: authProvider,
        localDataCenter: config.localDataCenter,
        sslOptions: {
            secureProtocol: "TLSv1_2_method"
  • The client connects to the Azure Cosmos DB Cassandra API.

  • A new keyspace is created.

    var query =
        `CREATE KEYSPACE IF NOT EXISTS ${config.keySpace} WITH replication = {'class': 'NetworkTopologyStrategy', 'datacenter' : '1' }`;
    await client.execute(query);
  • A new table is created.

      query =
          `CREATE TABLE IF NOT EXISTS ${config.keySpace}.user (user_id int PRIMARY KEY, user_name text, user_bcity text)`;
      await client.execute(query);
  • Key/value entities are inserted.

    const arr = [
        `INSERT INTO  ${config.keySpace}.user (user_id, user_name , user_bcity) VALUES (1, 'AdrianaS', 'Seattle')`,
        `INSERT INTO  ${config.keySpace}.user (user_id, user_name , user_bcity) VALUES (2, 'JiriK', 'Toronto')`,
        `INSERT INTO  ${config.keySpace}.user (user_id, user_name , user_bcity) VALUES (3, 'IvanH', 'Mumbai')`,
        `INSERT INTO  ${config.keySpace}.user (user_id, user_name , user_bcity) VALUES (4, 'IvanH', 'Seattle')`,
        `INSERT INTO  ${config.keySpace}.user (user_id, user_name , user_bcity) VALUES (5, 'IvanaV', 'Belgaum')`,
        `INSERT INTO  ${config.keySpace}.user (user_id, user_name , user_bcity) VALUES (6, 'LiliyaB', 'Seattle')`,
        `INSERT INTO  ${config.keySpace}.user (user_id, user_name , user_bcity) VALUES (7, 'JindrichH', 'Buenos Aires')`,
        `INSERT INTO  ${config.keySpace}.user (user_id, user_name , user_bcity) VALUES (8, 'AdrianaS', 'Seattle')`,
        `INSERT INTO  ${config.keySpace}.user (user_id, user_name , user_bcity) VALUES (9, 'JozefM', 'Seattle')`,
    for (const element of arr) {
        await client.execute(element);
  • Query to get all key values.

    query = `SELECT * FROM ${config.keySpace}.user`;
    const resultSelect = await client.execute(query);
    for (const row of resultSelect.rows) {
            "Obtained row: %d | %s | %s ",
  • Query to get a key-value.

    query = `SELECT * FROM ${config.keySpace}.user where user_id=1`;
    const resultSelectWhere = await client.execute(query);
    for (const row of resultSelectWhere.rows) {
            "Obtained row: %d | %s | %s ",
  • Close connection.


Update your connection string

Now go back to the Azure portal to get your connection string information and copy it into the app. The connection string enables your app to communicate with your hosted database.

  1. In your Azure Cosmos DB account in the Azure portal, select Connection String.

  2. Use the button on the right side of the screen to copy the top value, the CONTACT POINT.

    View and copy the CONTACT POINT, USERNAME,and PASSWORD from the Azure portal, connection string page

  3. Open the config.js file.

  4. Paste the CONTACT POINT value from the portal over 'CONTACT-POINT on line 9.

    Line 9 should now look similar to

    contactPoint: "cosmos-db-quickstarts.cassandra.cosmosdb.azure.com",

  5. Copy the USERNAME value from the portal and paste it over <FillMEIN> on line 2.

    Line 2 should now look similar to

    username: 'cosmos-db-quickstart',

  6. Copy the PASSWORD value from the portal and paste it over USERNAME on line 8.

    Line 8 should now look similar to

    password: '2Ggkr662ifxz2Mg==',

  7. Replace REGION with the Azure region you created this resource in.

  8. Save the config.js file.

Run the Node.js app

  1. In the terminal window, ensure you are in the sample directory you cloned earlier:

    cd azure-cosmos-db-cassandra-nodejs-getting-started
  2. Run your node application:

    npm start
  3. Verify the results as expected from the command line.

    View and verify the output

    Press CTRL+C to stop execution of the program and close the console window.

  4. In the Azure portal, open Data Explorer to query, modify, and work with this new data.

    View the data in Data Explorer

Review SLAs in the Azure portal

The Azure portal monitors your Cosmos DB account throughput, storage, availability, latency, and consistency. Charts for metrics associated with an Azure Cosmos DB Service Level Agreement (SLA) show the SLA value compared to actual performance. This suite of metrics makes monitoring your SLAs transparent.

To review metrics and SLAs:

  1. Select Metrics in your Cosmos DB account's navigation menu.

  2. Select a tab such as Latency, and select a timeframe on the right. Compare the Actual and SLA lines on the charts.

    Azure Cosmos DB metrics suite

  3. Review the metrics on the other tabs.

Clean up resources

When you're done with your app and Azure Cosmos DB account, you can delete the Azure resources you created so you don't incur more charges. To delete the resources:

  1. In the Azure portal Search bar, search for and select Resource groups.

  2. From the list, select the resource group you created for this quickstart.

    Select the resource group to delete

  3. On the resource group Overview page, select Delete resource group.

    Delete the resource group

  4. In the next window, enter the name of the resource group to delete, and then select Delete.

Next steps

In this quickstart, you learned how to create an Azure Cosmos DB account with Cassandra API, and run a Cassandra Node.js app that creates a Cassandra database and container. You can now import additional data into your Azure Cosmos DB account.