eth_getBlockTransactionCountByNumber - Immutable zkEVM

This method returns the number of transactions in a block identified by its number or tag, as a hex-encoded integer.

Parameters

ParameterTypeRequiredDescription
blockParameterstringYesBlock number in hex, or "latest", "earliest", "pending"

Request

bashcurl --location --request POST 'https://shared.us-east-1.getblock.io/<ACCESS-TOKEN>/' \
--header 'Content-Type: application/json' \
--data-raw '{
    "jsonrpc": "2.0",
    "method": "eth_getBlockTransactionCountByNumber",
    "params": ["latest"],
    "id": "getblock.io"
}'

example.js

javascriptconst axios = require('axios');

const response = await axios.post('https://shared.us-east-1.getblock.io/<ACCESS-TOKEN>/', {
    jsonrpc: '2.0',
    method: 'eth_getBlockTransactionCountByNumber',
    params: ['latest'],
    id: 'getblock.io'
}, {
    headers: { 'Content-Type': 'application/json' }
});

console.log(response.data.result);

example.py

pythonimport requests

response = requests.post(
    'https://shared.us-east-1.getblock.io/<ACCESS-TOKEN>/',
    headers={'Content-Type': 'application/json'},
    json={
        'jsonrpc': '2.0',
        'method': 'eth_getBlockTransactionCountByNumber',
        'params': ['latest'],
        'id': 'getblock.io'
    }
)

print(response.json())

example.rs

rustuse reqwest::Client;
use serde_json::{json, Value};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new();

    let response = client
        .post("https://shared.us-east-1.getblock.io/<ACCESS-TOKEN>/")
        .header("Content-Type", "application/json")
        .json(&json!({
            "jsonrpc": "2.0",
            "method": "eth_getBlockTransactionCountByNumber",
            "params": ["latest"],
            "id": "getblock.io"
        }))
        .send()
        .await?
        .json::<Value>()
        .await?;

    println!("Result: {}", response["result"]);
    Ok(())
}

Response

json{
    "jsonrpc": "2.0",
    "id": "getblock.io",
    "result": "0x2a"
}

Response Parameters

ParameterTypeDescription
jsonrpcstringJSON-RPC protocol version ("2.0")
idstringRequest identifier matching the request
resultstringHex-encoded transaction count in the block

Use Cases

  • Block Sizing: Read a block's transaction count by height or tag
  • Index Iteration: Bound a loop over transaction indices by block number
  • Throughput Metrics: Sample transaction counts across a range of heights
  • Live Monitoring: Track fullness of the "latest" block

Error Handling

Error CodeMessageDescription
-32602Invalid paramsMalformed block number or tag
-32603Internal errorThe node failed to count the block's transactions

Web3 Integration

ethers-example.js

javascriptimport { ethers } from 'ethers';

const provider = new ethers.JsonRpcProvider('https://shared.us-east-1.getblock.io/<ACCESS-TOKEN>/');

const block = await provider.getBlock('latest');
console.log(block.transactions.length);

viem-example.js

javascriptimport { createPublicClient, http } from 'viem';

const client = createPublicClient({
  transport: http('https://shared.us-east-1.getblock.io/<ACCESS-TOKEN>/')
});

const count = await client.getBlockTransactionCount({ blockTag: 'latest' });