> ## 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.

# eth_getBlockTransactionCountByHash | Polygon

> Polygon API method that returns the number of transactions in a block specified by block hash. This information can be useful for analytics purposes.

Polygon API method that returns the number of transactions in a block specified by block hash. This information can be useful for analytics purposes.

## Parameters

* `hash` — the block hash of the requested block.

## Response

* `quantity` — an integer value representing how many transactions are included in the block.

## `eth_getBlockTransactionCountByHash` code examples

<CodeGroup>
  ```javascript ethers.js theme={"system"}
  const ethers = require('ethers');
  const NODE_URL = "CHAINSTACK_NODE_URL";
  const provider = new ethers.JsonRpcProvider(NODE_URL);

  const getTransactionsCount = async (blockHash) => {
      const count = await provider.send("eth_getBlockTransactionCountByHash", [blockHash]);
      console.log(count);
     };

     getTransactionsCount('0x7709b7cacb9a3d647afe557d51891c00ad0c00e01d5bffdbcfe6c1730d36290c');
  ```

  ```python web3.py theme={"system"}
  from web3 import Web3  
  node_url = "CHAINSTACK_NODE_URL" 

  web3 = Web3(Web3.HTTPProvider(node_url)) 
  print(web3.eth.get_block_transaction_count("0x7709b7cacb9a3d647afe557d51891c00ad0c00e01d5bffdbcfe6c1730d36290c"))
  ```
</CodeGroup>

## Use case

`eth_getBlockTransactionCountByHash` is a useful tool for analyzing transaction volume on the Polygon blockchain. On average, a [new block is generated on the Polygon mainnet every 2.1 seconds](https://polygonscan.com/chart/blocktime), resulting in approximately 1,700 blocks per hour. Using the ethers.js library, one can inspect the past 1,700 blocks starting from the latest block, retrieve the hash of each block, and use `eth_getBlockTransactionCountByHash` to find the number of transactions in each block.

```javascript index.js theme={"system"}
const ethers = require("ethers");
const NODE_URL = "CHAINSTACK_NODE_URL";
const provider = new ethers.JsonRpcProvider(NODE_URL);

async function retrieveTransactionsCount() {
  // Get the current block number
  const currentBlockNumber = await provider.getBlockNumber();

  // Calculate the block number from one hour ago
  const oneHourAgoBlockNumber = currentBlockNumber - 1700; // 1700 blocks/hr on average on the Polygon mainnet
  
  // Initialize a variable to store the total number of transactions
  let totalTransactionCount = 0;

  // Loop through all blocks from the current block number to the block number from one hour ago
  for (let block = currentBlockNumber; block > oneHourAgoBlockNumber; block--) {

    // Get the hash of each block
    const getBlockHash = await provider.getBlock(block, false);
    const blockHash = getBlockHash.hash

    // Get the number of transactions in this block using each hash
    const transactionCount = await provider.send("eth_getBlockTransactionCountByHash", [blockHash]);

    // Add the number of transactions in this block to the total
    totalTransactionCount += Number(transactionCount);

    console.log(`Block #${block} has ${Number(transactionCount)} transactions.`);
  }

  console.log(`The total number of transactions in the previous hour is: ${totalTransactionCount}`);
}


retrieveTransactionsCount();
```

This example connects to a Polygon node using the ethers.js library. The `retrieveTransactionsCount` function gets the current block number, calculates the block number from one hour ago, and uses a for loop to iterate through all blocks in that range.

The function then retrieves the hash of each block and the number of transactions in each block and adds it to a total transaction count. The final result, the total number of transactions in the previous hour, is logged to the console.

This is a good example of how you can combine different methods.


## OpenAPI

````yaml openapi/polygon_node_api/blocks_info/eth_getBlockTransactionCountByHash.json POST /a9bca2f0f84b54086ceebe590316fff3
openapi: 3.0.0
info:
  title: Polygon Node API
  version: 1.0.0
  description: This is an API for interacting with a Polygon node.
servers:
  - url: https://nd-828-700-214.p2pify.com
security: []
paths:
  /a9bca2f0f84b54086ceebe590316fff3:
    post:
      tags:
        - Blocks info
      summary: eth_getBlockTransactionCountByHash
      operationId: getBlockTransactionCountByHash
      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: eth_getBlockTransactionCountByHash
                params:
                  type: array
                  items:
                    type: string
                    description: The block hash identifier.
                  default:
                    - >-
                      0x7709b7cacb9a3d647afe557d51891c00ad0c00e01d5bffdbcfe6c1730d36290c
      responses:
        '200':
          description: The block information
          content:
            application/json:
              schema:
                type: object
                properties:
                  jsonrpc:
                    type: string
                  id:
                    type: integer
                  result:
                    type: string

````