Build A Crypto Price Tracker Using Node.js and Cassandra
In this tutorial, you will learn how to use a small Node.js application to get cryptocurrency data from an API and continuously ingest that into Cassandra.
Join the DZone community and get the full member experience.Join For Free
Since the big bang in the data technology landscape happened a decade and a half ago, giving rise to technologies like Hadoop, which cater to the four ‘V’s. — volume, variety, velocity, and veracity there has been an uptick in the use of databases with specialized capabilities to cater to different types of data and usage patterns. You can now see companies using graph databases, time-series databases, document databases, and others for different customer and internal workloads.
In this tutorial, you will learn how to use a small Node.js application to get cryptocurrency data from an API and continuously ingest that into Cassandra for downstream consumption. You can feed data from Cassandra to a wide variety of applications such as a cryptocurrency trading platform, cryptocurrency price monitoring and alerting, and so on.
Without further ado, let’s get going.
Before getting started with the tutorial, you’ll need the following:
- Node.js — a local Node.js installation that comes with npm — the default package manager.
- Git — to clone the GitHub repository that contains the code for this tutorial.
- DataStax Astra — for spinning up a serverless, Cassandra database to use with an API or driver.
- CoinGecko-API — a Node.js library to access CoinGecko cryptocurrency data.
Clone the Repository
You can clone the GitHub repository for Cassandra Node.js Cryptotracker using the git clone command:
git clone firstname.lastname@example.org:kovid-r/cassandra-nodejs-cryptotracker.git
After cloning the repository, the first order of business is to install the Node.js dependencies specified in the package.json and package-lock.json files. npm will take care of installing the dependencies using the following command in the repository directory:
These instructions are also present in the README file in the repository.
Create a Database
Although the price information changes as you change cloud providers and regions, for a Proof of Concept, or even small production applications, you won’t need any more than the free $25 credit per month that Astra already offers its users. This gets you roughly 40 GB of free storage, 40 million reads, and 5 million writes. No credit card is required to sign up, just log in with Google or GitHub.
Click on the Create Database option, and it will lead you to the following page where you can create a database on any of the three leading cloud providers globally — Google Cloud, AWS, and Azure. You will need to provide the database name (node app) and the keyspace name (coingecko). Please use the specified names.
Initialize the Database
To access the database for the first time, use the CQLSH in-browser console, as shown in the image below.
Use this console to run the two commands to create a keyspace if it doesn't exist in the database, and to create a table in the specified keyspace. The structure of the table is predefined in the initialize.sql file in the repository. The data from the CoinGecko API calls will land in this table.
Download the Secure Bundle
You need to download the DataStax Secure Bundle zip file to your Node.js application directory. Make sure that your browser doesn’t unzip the file while downloading it.
You will need to provide the path to this file to the configuration in a later step; for more information about the Secure Bundle, head over to DataStax Astra’s official documentation.
Generate an Authentication Token
The next step is to generate an authentication token. To do that, click the Billing option on the top menubar. You will land on a screen shown in the image below:
Go to the Token Management option in the left menubar and choose the R/W User role in the Select Role drop-down menu and press Generate Token.
You will see the following screen with your authentication token. Download the Client ID, Client Secret, and Token in a CSV, or copy them directly.
Use Npm Config for Authentication
Now, we are good to go ahead and run our Node.js Cryptotracker application. In this tutorial, we are using the config package to avoid storing token information in our code. We have included a file in the repository config/default.json which looks something like this:
To authenticate, replace the values for secureConnectBundle, username, and password in this JSON file as instructed, but make sure you DO NOT commit this to your codebase.
Run Npm Start
If you have configured everything correctly, you should be able to run the following command from your Node.js application directory now:
The Node.js application will run for 40 seconds and hit the CoinGecko API. You can change that by going to these two lines in the index.js file and reconfiguring the main function call.
After running npm start a couple of times, you can head over to the Astra website to see how much of your quota is used, as shown in the image below:
You can also log into your CQL console and write a CQL query to fetch some data from the coingecko.coin_prices table as shown in the image below:
If you want to capture the price for other coins, you can pass different parameters to the getCoinPrice method arguments — coinName and coinSymbol.
Connect Other Applications
Other applications can consume data from Cassandra. DataStax Astra supports drivers for C#, Python, Node.js, Java, and C++. You can seamlessly integrate popular visualization engines like Grafana with Astra.
In this tutorial, you learned how to use Cassandra as a time-series database for capturing cryptocurrency price movements using a Node.js application. You also learned how to set up a DataStax Astra database in the cloud using your preferred cloud provider.
Please reach out if you have any questions or feedback at email@example.com or connect with me on LinkedIn.
Published at DZone with permission of Kovid Rathee. See the original article here.
Opinions expressed by DZone contributors are their own.