Understanding Covalent
Covalent is a powerful tool that offers unified APIs to access a broad range of data, including event logs for smart contracts, token balances, historical pricing, transaction records, and more. This capability enables developers and analysts to obtain blockchain data for creating decentralized applications, conducting in-depth data analysis, and gaining valuable insights into the ever-changing blockchain ecosystem. The Covalent API has a unified design that eliminates the need to deal with the complexities of interfacing with multiple blockchains. This saves developers time and energy while improving the user experience for navigating blockchain data.Setting up the project environment
In this section, we will set up the environment required for our JavaScript project, including installing node.js and the required packages.Prerequisites
For this project, you will need the following:- node.js installed on your machine to run JavaScript code; download and install the latest version of node.js from the official website.
-
Access to Covalent itself. To get it, do the following:
- Purchase and install the Covalent app on the Chainstack Marketplace.
- Generate a Chainstack API key if you don’t have one.
- Claim your JSON web token required to access Covalent API.
Learn more about JTWs by reading Mastering JSON web tokens: How to implement secure user authentication.
Initialize a new project
The first step is to initialize a new node.js project. Create a new directory for your project and navigate to it in your terminal or command prompt. Then run the following:package.json file in your project directory, which will store your project’s dependencies and other information.
Install the required dependencies
For this project, we will use the Axios package to make HTTP requests and the dotenv package to manage environment variables. Install both packages by running the following command in your project directory:Set up the environment variables
It is good practice to use secure environment variables even in a developing or a learning environment; for this reason, we use the dotenv library to manage the Access token. Create a file named.env in your root directory and add your Covalent JSON web token (JWT) in the following format:
Using a
.env file allows you to reduce the risk of exposing secrets. Read How to store your Web3 DApp secrets: Guide to environment variables to learn more about keeping your secrets safe.Coding the program
Now that the environment is ready, we have all the tools to start coding. The goal is to use the Covalent API to retrieve every transaction ever made by an Ethereum address. In this tutorial, we target Ethereum, but you can use this code for any chain supported by the Covalent API. Create a new file in the root directory namedindex.js and paste the following code:
Understanding the code
The code defines aChainstackApi class to interact with the Get transactions for address from the Covalent API using Axios.
It fetches every transaction ever made by an address on a given blockchain network, the Ethereum mainnet in this example. Here’s how the code works step by step:
-
Define the
ChainstackApiclass with a constructor that initializes the JWT and creates an encoded key for authentication.- The constructor sets
COVALENT_JWT_TOKENto the value from the environment variable.
- The constructor sets
-
Define the
fetchTransactionsmethod within theChainstackApiclass.-
This method takes four parameters:
chainName,walletAddress,currency, andnoLogs. You can use thenoLogsoption to decide whether you want to retrieve the logs or not; depending on your need, keeping the logs off can save some resources, as logs are usually an extensive response. - It sends a GET request to the Covalent API using Axios, passing the parameters as part of the URL and the encoded key in the headers.
- If the request is successful, it returns the transaction data. If there’s an error, it logs the error and throws it.
-
This method takes four parameters:
-
Define the
mainasync function that:- Creates a new instance of the
ChainstackApiclass. - Defines the required parameters:
chainName,walletAddress,currency, andnoLogs. - Calls the
fetchTransactionsmethod of theChainstackApiinstance and destructures thedataproperty from the response, storing it intransactionsData. - Logs the
transactionsDatato the console. - If an error occurs during the execution, logs the error.
- Creates a new instance of the
-
Invoke the
mainfunction to execute the code.
This program is designed to be modular, so you can add more methods to the
ChainstackApi class and then export it with module.exports = ChainstackApi;.This allows you to import the ChainstackApi class into other files and use its methods. Import it with this line, const ChainstackApi = require('./PATH_TO_YOUR_FILE').Run the code and understand the response
To run the code, simply save the file and run it with thenode command:
transactionsData object from the response, which includes general information and an items object listing the details of every transaction.
Example of a response:
More Covalent APIs
In this tutorial, we focused on the Get transactions for address API specifically but Covalent offers many unified APIs, including balances, historical prices, and NFTs. You can use the code we made today with any of the unified APIs available; simply add the request as a method in theChainstackApi class.
Find a list of unified APIs available on the Covalent docs.
