Overview
Overview
Chainstack provides a robust API that enables you to retrieve data about your projects and endpoints efficiently. This short tutorial will use the list all nodes API.
Prerequisites
Prerequisites
You will need a Chainstack account with an API and Node.js
Environment setup
Environment setup
To successfully set up your project environment, follow these steps:
- Create a new Node project:
Learn how to manage Node projects reading Web3 node.js: From zero to a full-fledged project
- Install Required Packages:
For this project, we’ll use
Axiosto interact with the Chainstack API,web3.jsto verify the endpoints, anddotenvto handle the Chainstack API key securely.
Environment variables
Environment variables
It’s important to store your Chainstack API key securely. For this purpose, create a
.env file in your project’s root directory, which will hold sensitive configuration details, like API keys, away from your main codebase.Here’s how you can set it up:- Navigate to Your Project’s Root Directory: Ensure you’re in the root directory of your project, where your main application files reside.
- Create a
.envFile: In this directory, create a new file named.env. This file is commonly used to store environment variables. - Add Your Chainstack API Key:
Open the
.envfile with a text editor and insert the following line:ReplaceYOUR_API_KEYwith your actual Chainstack API key.
The script
The script
Now that you’ve set up your environment, the next step involves creating a JavaScript file.Follow these instructions:
- Creating a New JavaScript File in the Root Directory:
Create a new JavaScript file in your project’s root directory (the same location where you created the
.envfile). You can name this filegetEndpoints.js.
- Adding Code to the JavaScript File:
After creating
getEndpoints.js, open it in your preferred code editor. You are now ready to add the necessary code. Paste the JavaScript script.
Understanding the code
Understanding the code
This JavaScript code is designed to interact with the Chainstack API, retrieve blockchain node information, and create a configuration file with these details.
- Importing Modules and Loading Environment Variables:
- The code begins by importing necessary modules:
axiosfor HTTP requests,web3for interacting with Ethereum blockchain, andfsfor file system operations. It also loads environment variables usingdotenv.
- The code begins by importing necessary modules:
- Constants and Global Variables:
- Two constants are defined:
CHAINSTACK_API_KEY(from environment variables) for API authentication andOUTPUT_FILE_NAMEas the default name for the output file.
- Two constants are defined:
- Fetching Data from Chainstack API:
- The
fetchChainstackDatafunction makes an asynchronous GET request to the Chainstack API using the Axios library. It uses the API key for authorization. If successful, it logs the number of items fetched and returns the data; if not, it logs an error and returns null.
- The
- Processing Individual Chainstack Items:
processChainstackItemtakes an item from the Chainstack data and extracts relevant information, including ID, name, HTTP endpoint, authentication key, and client configuration.
- Connecting to a Web3 Endpoint:
- The
connectToWeb3function attempts to connect to a Web3 endpoint using the provided URL. It uses theWeb3library to interact with the blockchain. It logs the success or failure of the connection.
- The
- Sanitizing Endpoint Names:
sanitizeNamesanitizes the name of an endpoint to be used as a key in the environment file. It replaces spaces, dashes, and slashes with underscores and converts the string to uppercase.
- Creating a
.envFile:createEnvFiletakes a dictionary of endpoint information and writes it to a.envfile. Each line in the file represents an endpoint with its URL, formatted as an environment variable.
- Orchestrating the Main Process:
- The
mainfunction orchestrates the overall process. It first checks for the Chainstack API key. Then, it fetches data from the Chainstack API and processes each item. For each item, it reconstructs the endpoint URL and attempts to connect via Web3. If successful, the endpoint information is added to a dictionary. Finally, if there are any successful endpoints, it creates a.envfile with this information.
- The
- Executing the Main Function:
- Finally, the script executes the
mainfunction to run the entire process.
- Finally, the script executes the
Outcome
Outcome
Executing this script will efficiently process and validate your Chainstack endpoints. It identifies those endpoints that successfully return a chain ID, indicating their active and functional status. These validated endpoints are then neatly recorded into a
.env file.With this setup, you gain a streamlined and organized method to access and utilize all your Chainstack endpoints.This approach simplifies endpoint management and enhances the overall efficiency of your interactions with Chainstack services.Note that non-EVM endpoints or improperly formatted ones will not work and will show a warning. This is a point you can build on and improve.