Learn how to create a .env
file with all your Chainstack endpoints using JavaScript.
Overview
Prerequisites
Environment setup
Axios
to interact with the Chainstack API, web3.js
to verify the endpoints, and dotenv
to handle the Chainstack API key securely.
Environment variables
.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:.env
File:
In this directory, create a new file named .env
. This file is commonly used to store environment variables..env
file with a text editor and insert the following line:
YOUR_API_KEY
with your actual Chainstack API key.The script
.env
file). You can name this file getEndpoints.js
.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
axios
for HTTP requests, web3
for interacting with Ethereum blockchain, and fs
for file system operations. It also loads environment variables using dotenv
.CHAINSTACK_API_KEY
(from environment variables) for API authentication and OUTPUT_FILE_NAME
as the default name for the output file.fetchChainstackData
function 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.processChainstackItem
takes an item from the Chainstack data and extracts relevant information, including ID, name, HTTP endpoint, authentication key, and client configuration.connectToWeb3
function attempts to connect to a Web3 endpoint using the provided URL. It uses the Web3
library to interact with the blockchain. It logs the success or failure of the connection.sanitizeName
sanitizes 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..env
File:
createEnvFile
takes a dictionary of endpoint information and writes it to a .env
file. Each line in the file represents an endpoint with its URL, formatted as an environment variable.main
function 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 .env
file with this information.main
function to run the entire process.Outcome
.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.