> ## Documentation Index
> Fetch the complete documentation index at: https://docs.chainstack.com/llms.txt
> Use this file to discover all available pages before exploring further.

# net_peerCount | Ethereum

> Ethereum API method that returns the number of peers connected to the node. Reference for net_peerCount on Ethereum via Chainstack.

Ethereum API method that returns the number of peers connected to the node. This method can be useful for developers who want to monitor the connectivity of their node and ensure that it is functioning as expected. By checking the number of connected peers, developers can verify that their node is receiving updates and is properly synchronized with the network.

<Check>
  **Get your own node endpoint today**

  [Start for free](https://console.chainstack.com/) and get your app to production levels immediately. No credit card required.

  You can sign up with your GitHub, X, Google, or Microsoft account.
</Check>

## Parameters

* `none`

## Response

* `quantity` — an integer value indicating the current number of peers connected to the node

## `net_peerCount` code examples

<Note>
  Learn more about the `ChainstackProvider` in `ethers.js`: [ethers ChainstackProvider Documentation](/reference/ethersjs-chainstackprovider).
</Note>

<CodeGroup>
  ```javascript ethers.js theme={"system"}
  const ethers = require("ethers");

  // Create a ChainstackProvider instance for Ethereum mainnet
  const chainstack = new ethers.ChainstackProvider("mainnet");

  const peersCount = async () => {
    const peers = await chainstack.send("net_peerCount"); // This value is returned in HEX
    const decimalPeers = parseInt(peers, 16);
    
    console.log(`Peers connected: ${decimalPeers}`);
  };

  peersCount();
  ```

  ```javascript web3.js theme={"system"}
  const { Web3 } = require("web3");
  const NODE_URL = "CHAINSTACK_NODE_URL";
  const web3 = new Web3(NODE_URL);

  async function getPeers() {
    const peers = await web3.eth.net.getPeerCount()
    console.log(peers);
  }

  getPeers();
  ```

  ```python web3.py theme={"system"}
  from web3 import Web3  
  node_url = "CHAINSTACK_NODE_URL"
  web3 = Web3.HTTPProvider(node_url)

  peers = web3.provider.make_request('net_peerCount', [])
  print(peers)
  ```
</CodeGroup>

## Use case

One practical use case for the `net_peerCount` method is to monitor the number of peers connected to a node and take some action if the number drops below a certain threshold. For example, a developer might want to send a notification to check the node if the peer count is low for more than 10 minutes.

Here is an implementation of this concept using web3.js:

```javascript index.js theme={"system"}
const { Web3 } = require("web3");
const NODE_URL = "CHAINSTACK_NODE_URL";
const web3 = new Web3(NODE_URL);

const MIN_PEER_COUNT = 5; // Set the minimum number of peers.
let lowPeerCountTime = null; // Track the time when peer count first dropped below the threshold.

/**
 * Checks the number of connected peers and sends a notification if the peer count has been low for more than 10 minutes.
 */
function checkPeerCount() {
  web3.eth.net.getPeerCount().then((peerCount) => {
    console.log(`Number of connected peers: ${peerCount}`);

    if (peerCount < MIN_PEER_COUNT) {
      // Peer count has dropped below the threshold.

      if (lowPeerCountTime === null) {
        // This is the first time we've seen a low peer count.
        lowPeerCountTime = Date.now();
        console.log(`Low peer count detected at ${new Date(lowPeerCountTime)}.`);

      } else if (Date.now() - lowPeerCountTime > 10 * 60 * 1000) {
        // Peer count has been low for more than 10 minutes, send a notification.
        console.log('Sending notification to check the node.');
        // TODO: Add code here to send a notification, e.g. via email or SMS.

      }
    } else {
      // Peer count is above the threshold.
      lowPeerCountTime = null;
      
    }
  });
}

// Check the peer count once when the script starts.
checkPeerCount();

// Check the peer count every minute.
setInterval(checkPeerCount, 60 * 1000);
```

This code defines a function called `checkPeerCount` that retrieves the current number of connected peers using the `web3.eth.net.getPeerCount` method. The function then checks whether the peer count is below a minimum threshold (defined as `MIN_PEER_COUNT`), and takes appropriate action based on the result.

If the peer count is below the threshold and this is the first time the count has dropped below the threshold, the function records the current time and logs a message to the console. If the peer count remains below the threshold for more than 10 minutes, the function logs a message to the console indicating that a notification should be sent and optionally sends a notification via email or SMS.

If the peer count is above the threshold, the function resets the `lowPeerCountTime` variable to null.

By running the `checkPeerCount` function periodically, we can ensure that we are notified if the peer count drops below a certain threshold and stays low for an extended period of time, which can indicate issues with the node's connectivity.


## OpenAPI

````yaml /openapi/ethereum_node_api/client_info/net_peerCount.json POST /0a9d79d93fb2f4a4b1e04695da2b77a7
openapi: 3.0.0
info:
  title: Chainstack Node API
  version: 1.0.0
  description: This is an API for interacting with a Chainstack node.
servers:
  - url: https://nd-422-757-666.p2pify.com
security: []
paths:
  /0a9d79d93fb2f4a4b1e04695da2b77a7:
    post:
      tags:
        - Update
      summary: net_peerCount
      operationId: peerCount
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                id:
                  type: integer
                  default: 1
                jsonrpc:
                  type: string
                  default: '2.0'
                method:
                  type: string
                  default: net_peerCount
                params:
                  type: array
                  default: []
      responses:
        '200':
          description: The number of peers connected to the client.
          content:
            application/json:
              schema:
                type: object
                properties:
                  jsonrpc:
                    type: string
                  id:
                    type: integer
                  result:
                    type: string

````