Search
Close this search box.
building a rest API with cassandra on Datastax Astra using Python and Node

Building a REST API with Cassandra on DataStax Astra Using Python and Node

Rahul Singh, CEO and Co-founder at Anant, recently gave a webinar on building a REST API with Cassandra on DataStax Astra using python and node.

DataStax Astra is a cloud-native database-as-a-service built on Apache Cassandra.

Various capabilities Astra offers
Various capabilities Astra offers

In addition to those features, DataStax Astra also offers the ability to deploy to GCP and AWS; however, to do so, it will require a paid cluster.

After registering and/or signing-in, you will see this dashboard if you currently do not have an existing database.

Create Astra Database Page
Create Astra Database Page

You can set up the database to your liking, and you will see this dashboard once hitting launch database.

Astra Database Initializing
Astra Database Initializing

You can then access the CQL console as shown below once the database is initialized by clicking the CQL Console tab.

Astra CQL Console
Astra CQL Console

You can also view the health and metrics of the cluster by clicking the Health tab.

Astra Database Health
Astra Database Health

After setting up the database, we can then download the secure connect bundle as shown below. This will be needed so that the APIs can connect to the Astra database.

Download Astra Secure Connect Bundle
Download Astra Secure Connect Bundle

After downloading the bundle, you can use DataStax’s cassandra-driver docs to configure the code needed to connect to the Astra Database. The docs are linked below:

Example Python REST API Connection to DataStax Astra:

from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider

cloud_config= {
        'secure_connect_bundle': '/path/to/secure-connect-database_name.zip'
}
auth_provider = PlainTextAuthProvider('username', 'password')
cluster = Cluster(cloud=cloud_config, auth_provider=auth_provider)
session = cluster.connect()
row = session.execute("select release_version from system.local").one()
if row:
    print(row[0])
else:
    print("An error occurred.")

Example Node REST API Connection to DataStax Astra:

'use strict'

const { Client } = require('cassandra-driver');
async function run() {
  const client = new Client({
    cloud: { secureConnectBundle: 'path/to/secure-connect-database_name.zip' },
    credentials: { username: 'username', password: 'password' }
  });

  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();

Once the connection is working, we can utilize our API to expose the data in Astra. To see our public repository with example Node and Python APIs, you can visit https://github.com/anant/cassandra.api.

You can also watch this webinar for more information regarding how we built these APIs and why we chose to use DataStax Astra and an online IDE like Gitpod.

We will also include the slideshare associated with the webinar for additional information as well.

Cassandra.Link

Cassandra.Link is a knowledge base that we created for all things Apache Cassandra. Our goal with Cassandra.Link was to not only fill the gap of Planet Cassandra, but to bring the Cassandra community together. Feel free to reach out if you wish to collaborate with us on this project in any capacity.

Anant Corporation Events

Check out our events page to keep up with our upcoming and past webinars; as well as, any future virtual or in-person meetups.

We are a technology company that specializes in building business platforms. If you have any questions about the tools discussed in this post or about any of our services, feel free to send us an email!