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.This API allows you to seamlessly extract a comprehensive list of nodes associated with your account. Additionally, we will demonstrate how to automate the creation of a .env file with the fetched data.Note that this script is designed for EVM compatible chains.
Prerequisites
Prerequisites
You will need a Chainstack account with an API and Python
Environment setup
Environment setup
To successfully set up your project environment, follow these steps:
1
Create a New Python Virtual Environment
This isolated environment ensures that your project dependencies do not interfere with the global Python setup. Use the following command in your terminal:This command creates a new virtual environment named
Bash
get_endpoints
.2
Activate the Virtual Environment
To activate the environment, run:After activation, you will notice that the terminal prompt is prefixed with
Bash
(get_endpoints)
, indicating that the virtual environment is active.3
Install Required Packages
Within the activated environment, install the necessary packages using pip. For this project, you need
python-dotenv
for managing environment variables and web3
for interacting with EVM blockchains:Bash
Environment variables
Environment variables
It’s important to store your Chainstack API key securely. For this purpose, in your project’s root directory, create a
.env
file, which will hold sensitive configuration details like API keys, away from your main codebase.Here’s how you can set it up:1
Navigate to Your Project's Root Directory
Ensure you’re in the root directory of your project, where your main application files reside.
2
Create a `.env` File
In this directory, create a new file named
.env
. This file is commonly used to store environment variables.3
Add Your Chainstack API Key
Open the Replace
.env
file with a text editor and insert the following line:env
YOUR_API_KEY
with your actual Chainstack API key.The script
The script
Now that you’ve set up your environment, the next step involves creating a Python script.Follow these instructions:
1
Creating a New Python File in the Root Directory
Create a new Python file in your project’s root directory (the same location where you created the
.env
file). You can name this file get_endpoints.py
.This naming convention indicates the file’s purpose, which is to retrieve endpoint data.2
Adding Code to the Python File
After creating
get_endpoints.py
, open it in your preferred code editor. You are now ready to add the necessary code. Paste the Python script.Understanding the code
Understanding the code
This Python script is designed to interact with the Chainstack API, retrieve blockchain node information, and create a configuration file with these details.
Environment Setup
Environment Setup
- Environment Setup and Logging:
- The script starts by loading environment variables from a
.env
file, particularly theCHAINSTACK_API_KEY
. This approach is a security best practice, keeping sensitive information from the source code. - Logging is initialized for recording the script’s activities, errors, and informational messages, which is crucial for monitoring and debugging.
- The script starts by loading environment variables from a
get_endpoints.py
Get data from the API
Get data from the API
- Fetching Data from Chainstack API:
- The
fetch_chainstack_data
function makes a GET request to the Chainstack API using the provided API key. It retrieves information about the blockchain nodes on your account. - If successful, the function logs the number of items fetched and returns the data; otherwise, it logs an error and returns
None
.
- The
get_endpoints.py
Process the response
Process the response
- Processing Chainstack Data:
process_chainstack_item
takes an item from the Chainstack data and extracts important details like ID, name, and endpoint information. This function is vital for organizing and simplifying the raw API data.
get_endpoints.py
Test the endpoints
Test the endpoints
- Web3 Connection Test:
- The
connect_to_web3
function attempts to establish a connection to a Web3 endpoint. This step is crucial for verifying the functionality of the blockchain nodes as they are built from the HTTPS endpoint, and the AUTH key is fetched from the API. - It logs a successful connection or warns if it fails, providing valuable feedback about each endpoint’s status.
- The
get_endpoints.py
Write the .env file
Write the .env file
- Data Sanitization and .env File Creation:
sanitize_name
ensures that the names of the endpoints are formatted correctly to be used as environment variable keys in the .env file.create_env_file
writes the endpoint URLs into a .env file, making them easily accessible and usable in other project parts. This file serves as a central configuration point, enhancing the modularity and scalability of the system.
get_endpoints.py
Main flow
Main flow
- Main Execution Flow:
- The
main
function orchestrates the entire process: checking the API key, fetching and processing data, testing endpoints, and creating the .env file. - It ensures each step is executed in sequence and handles the absence of data or API keys, making the script robust and user-friendly.
- The
get_endpoints.py
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.