# eth\_getTransactionByBlockNumberAndIndex - Base

The `eth_getTransactionByBlockNumberAndIndex` method returns information about a transaction by block number and transaction index position. This is useful for sequentially processing transactions within a known block.

## Parameters

| Parameter      | Type   | Required | Description                                             |
| -------------- | ------ | -------- | ------------------------------------------------------- |
| blockParameter | string | Yes      | Block number in hex, or "latest", "earliest", "pending" |
| index          | string | Yes      | Transaction index position in hex                       |

## Request

{% tabs %}
{% tab title="cURL" %}

```bash
curl --location --request POST 'https://go.getblock.io/<ACCESS-TOKEN>/' \
--header 'Content-Type: application/json' \
--data-raw '{
    "jsonrpc": "2.0",
    "method": "eth_getTransactionByBlockNumberAndIndex",
    "params": ["latest", "0x0"],
    "id": "getblock.io"
}'
```

{% endtab %}

{% tab title="Axios" %}

```javascript
const axios = require('axios');

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

const tx = response.data.result;
if (tx) {
    console.log('From:', tx.from);
    console.log('To:', tx.to);
    console.log('Hash:', tx.hash);
}
```

{% endtab %}

{% tab title="Request" %}

```python
import requests

response = requests.post(
    'https://go.getblock.io/<ACCESS-TOKEN>/',
    headers={'Content-Type': 'application/json'},
    json={
        'jsonrpc': '2.0',
        'method': 'eth_getTransactionByBlockNumberAndIndex',
        'params': ['latest', '0x0'],
        'id': 'getblock.io'
    }
)

result = response.json()
tx = result['result']
if tx:
    print(f'From: {tx["from"]}')
    print(f'To: {tx["to"]}')
    print(f'Hash: {tx["hash"]}')
```

{% endtab %}

{% tab title="Rust" %}

```rust
use 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://go.getblock.io/<ACCESS-TOKEN>/")
        .header("Content-Type", "application/json")
        .json(&json!({
            "jsonrpc": "2.0",
            "method": "eth_getTransactionByBlockNumberAndIndex",
            "params": ["latest", "0x0"],
            "id": "getblock.io"
        }))
        .send()
        .await?
        .json::<Value>()
        .await?;
    
    println!("Transaction: {}", serde_json::to_string_pretty(&response["result"])?);
    Ok(())
}
```

{% endtab %}
{% endtabs %}

## Response

```json
{
    "jsonrpc": "2.0",
    "id": "getblock.io",
    "result": {
        "blockHash": "0x849a3ac8f0d81df1a645701cdb9f90e58500d2eabb80ff3b7f4e8c13f025eff2",
        "blockNumber": "0x12d687f",
        "from": "0x742d35Cc6634C0532925a3b844Bc9e7595f5bE21",
        "gas": "0x5208",
        "gasPrice": "0x5f5e100",
        "hash": "0x633982a26e0cfba940613c52b31c664fe977e05171e35f62da2426596007e249",
        "input": "0x",
        "nonce": "0x1a",
        "to": "0x1234567890123456789012345678901234567890",
        "transactionIndex": "0x0",
        "value": "0xde0b6b3a7640000",
        "type": "0x2",
        "chainId": "0x2105"
    }
}
```

## Response Parameters

| Parameter        | Type   | Description                    |
| ---------------- | ------ | ------------------------------ |
| hash             | string | 32-byte transaction hash       |
| blockHash        | string | 32-byte block hash             |
| blockNumber      | string | Block number in hex            |
| from             | string | 20-byte sender address         |
| to               | string | 20-byte recipient address      |
| value            | string | Value transferred in wei (hex) |
| gas              | string | Gas limit provided (hex)       |
| transactionIndex | string | Index in block (hex)           |
| type             | string | Transaction type               |

## Use Cases

* Block Iteration: Process all transactions in a block sequentially.
* First Transaction Analysis: Get the first transaction in recent blocks.
* Chain Indexing: Build transaction indexes by position.
* Data Processing: Batch process transactions by block.
* MEV Research: Analyze transaction ordering patterns.

## Error Handling

| Error Code | Message        | Description                          |
| ---------- | -------------- | ------------------------------------ |
| -32602     | Invalid params | Invalid block number or index format |
| -32603     | Internal error | Block or transaction not found       |

## Web3 Integration

{% tabs %}
{% tab title="Ethers.js" %}

```javascript
const { ethers } = require('ethers');

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

async function getTxByBlockNumberAndIndex(blockNumber, index) {
    // Get the block with full transactions
    const block = await provider.getBlock(blockNumber, true);
    
    if (block && block.transactions[index]) {
        const tx = block.transactions[index];
        console.log('Transaction Hash:', tx.hash);
        console.log('From:', tx.from);
        console.log('To:', tx.to);
        console.log('Value:', ethers.formatEther(tx.value), 'ETH');
        return tx;
    } else {
        console.log('Transaction not found');
        return null;
    }
}

// Get first transaction of latest block
getTxByBlockNumberAndIndex('latest', 0);

// Get specific block and index
getTxByBlockNumberAndIndex(19800000, 0);
```

{% endtab %}

{% tab title="Viem" %}

```javascript
import { createPublicClient, http, formatEther } from 'viem';
import { base } from 'viem/chains';

const client = createPublicClient({
    chain: base,
    transport: http('https://go.getblock.io/<ACCESS-TOKEN>/')
});

async function getTxByBlockNumberAndIndex(blockNumber, index) {
    const tx = await client.getTransaction({
        blockNumber: BigInt(blockNumber),
        index: index
    });
    
    if (tx) {
        console.log('Transaction Hash:', tx.hash);
        console.log('From:', tx.from);
        console.log('To:', tx.to);
        console.log('Value:', formatEther(tx.value), 'ETH');
    }
    
    return tx;
}

getTxByBlockNumberAndIndex(19800000, 0);
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.getblock.io/api-reference/base/eth_gettransactionbyblocknumberandindex-base.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
