> ## 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_getBlockTransactionCountByNumber | Ethereum

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

<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

* `quantity or tag` — the integer of a block encoded as hexadecimal or the string with:

  * `latest` — the most recent block in the blockchain and the current state of the blockchain at the most recent block. A chain reorganization is to be expected.
  * `safe` — the block that received justification from the beacon chain. Although this block could be involved in a chain reorganization, it would necessitate either a coordinated attack by the majority of validators or an instance of severe propagation latency.
  * `finalized` — the block accepted as canonical by more than 2/3 of the validators. A chain reorganization is extremely unlikely, and it would require at least 1/3 of the staked ETH to be burned.
  * `earliest` — the earliest available or genesis block
  * `pending` — the pending state and transactions block. The current state of transactions that have been broadcast to the network but have not yet been included in a block.

<Note>
  See the [default block parameter](https://ethereum.org/en/developers/docs/apis/json-rpc/#default-block) and [How The Merge Impacts Ethereum’s Application Layer](https://blog.ethereum.org/2021/11/29/how-the-merge-impacts-app-layer).
</Note>

## Response

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

## `eth_getBlockTransactionCountByNumber` 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 getTransactionsCount = async (blockId) => {
    const count = await chainstack.send("eth_getBlockTransactionCountByNumber", [
      blockId,
    ]);
    console.log(count);
  };

  getTransactionsCount("0x1026220");
  ```

  ```javascript web3.js theme={"system"}
  const { Web3 } = require("web3");
  const NODE_URL = "CHAINSTACK_NODE_URL";
  const web3 = new Web3(NODE_URL);

  async function getTransactionsCount(blockId) {
    const count = await web3.eth.getBlockTransactionCount(blockId)
    console.log(count);
  }

  getTransactionsCount('latest')
  ```

  ```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('latest')) # A hex value starting with "0x" is accepted as well.
  ```
</CodeGroup>

## Use case

`eth_getBlockTransactionCountByNumber` can be used to analyze how many transactions are included on the Ethereum blockchain in a certain period. For instance, a [new block is generated on the Ethereum blockchain every 12 seconds](https://etherscan.io/chart/blocktime), resulting in approximately 300 blocks per hour. Using the ethers.js library, one can inspect the past 300 blocks starting from the latest block and use `eth_getBlockTransactionCountByNumber` to find the number of transactions in each block to sum them.

```javascript index.js theme={"system"}
const ethers = require('ethers');
const NODE_URL = "CHAINSTACK_NODE_URL";
const provider = new ethers.JsonRpcProvider(NODE_URL);

async function retrieveTransactionCounts() {
    // Get the current block number
    const currentBlockNumber = await provider.getBlockNumber();
  
    // Calculate the block number from one hour ago
    const oneHourAgoBlockNumber = currentBlockNumber - 300; // 300/blocks/hr on average on the Ethereum  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 number of transactions in this block
      const transactionCount = await provider.send("eth_getBlockTransactionCountByNumber", [block]);

      // Make the necessery conversions from hex to decimal
      const stringCount = BigInt(transactionCount).toString();
      const decimalCount = Number(stringCount)

      // Add the number of transactions in this block to the total
      totalTransactionCount += decimalCount;

      const stringBlock = BigInt(block).toString();
      const decimalBlock = Number(stringBlock)
      console.log(`Block ${decimalBlock} has ${decimalCount} transactions.`);
    }
  
    console.log(`The total number of transactions in the previous hour is: ${totalTransactionCount}`);
  }
  
  retrieveTransactionCounts();
```

Using `eth_getBlockTransactionCountByNumber` is a bit more straightforward compared to the [example shown using `eth_getBlockTransactionCountByHash`](/reference/ethereum-getblocktransactioncountbyhash) for this same use case, you don't need to extract the block hash. Instead, you can query the transaction count directly.

This example is made using the ethers.js library, and you can notice that it requires more parsing compared to web3.js, and the results are given as hexadecimal.


## OpenAPI

````yaml /openapi/ethereum_node_api/blocks_info/eth_getBlockTransactionCountByNumber.json POST /0a9d79d93fb2f4a4b1e04695da2b77a7
openapi: 3.0.0
info:
  title: Ethereum Node API
  version: 1.0.0
  description: This is an API for interacting with an Ethereum node.
servers:
  - url: https://nd-422-757-666.p2pify.com
security: []
paths:
  /0a9d79d93fb2f4a4b1e04695da2b77a7:
    post:
      tags:
        - Blocks info
      summary: eth_getBlockTransactionCountByNumber
      operationId: getBlockTransactionCountByNumber
      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_getBlockTransactionCountByNumber
                params:
                  type: array
                  items:
                    type: string
                    title: Block identifier
                    description: The block number or tag.
                  default:
                    - latest
      responses:
        '200':
          description: The block transaction count information
          content:
            application/json:
              schema:
                type: object
                properties:
                  jsonrpc:
                    type: string
                  id:
                    type: integer
                  result:
                    type: string

````