> ## 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_getFilterChanges | Avalanche

Avalanche API polling method used to retrieve updates from a filter. A filter is an object used to track changes to the state of the blockchain.

Filters can be created using one of the following methods:

* [eth\_newFilter](/reference/avalanche-newfilter)
* [eth\_newPendingTransactionFilter](/reference/avalanche-newpendingtransactionfilter)
* [eth\_newBlockFilter](/reference/avalanche-newblockfilter)

After creating a filter using one of the available methods, the resulting filter ID can be used to fetch changes by calling the `eth_getFilterChanges` method.

<Warning>
  **Disclaimer**

  Note that the default interactive example in this page will not work as the filter will be expired.

  To test `eth_getFilterChanges` in this page, first create a new filter using one of the following:

  * [eth\_newFilter](/reference/avalanche-newfilter)
  * [eth\_newPendingTransactionFilter](/reference/avalanche-newpendingtransactionfilter)
  * [eth\_newBlockFilter](/reference/avalanche-newblockfilter)

  Then use the fresh filter ID as the parameter for `eth_getFilterChanges`.
</Warning>

<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

* `string` — the filter ID of the filter for which you want to retrieve the changes

## Response

* `array` — an array that represents the changes that have occurred on the blockchain since the last time the filter was polled:
  * For filters created with `eth_newBlockFilter`:
    * `blockHash` — the hashes of the new blocks since the last time the filter was polled.
  * For filters created with `eth_newPendingTransactionFilter`:
    * `transactionHash` — the hashes identifying new pending transactions since the last time the filter was polled.
  * For filters created with `eth_newFilter`, the following event logs:
    * `address` — the contract address from which the event originated.
    * `topics` — an array of 32-byte data fields containing indexed event parameters.
    * `data` — the non-indexed data that was emitted along with the event.
    * `blocknumber` — the block number in which the event was included. `null` if it is pending.
    * `transactionhash` — the hash of the transaction that triggered the event. `null` if pending.
    * `transactionindex` — the integer index of the transaction within the block's list of transactions. `null` if it is pending.
    * `blockhash` — the hash of the block in which the event was included. `null` if it is pending.
    * `logindex` — the integer identifying the index of the event within the block's list of events. `null` if pending.
    * `removed` — the boolean value indicating if the event was removed from the blockchain due to a chain reorganization. `True` if the log was removed. `False` if it is a valid log.

## `eth_getFilterChanges` code examples

<Note>
  Note that the `web3.eth.filter` methods have been deprecated and replaced with the `web3.eth.subscribe` in web3.js. See [web3.js subscriptions](/reference/avalanche-web3js-subscriptions-methods).
</Note>

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

  async function getFilterChanges(filter) {
    try {
      const changes = await provider.send('eth_getFilterChanges', [filter]);
      console.log(changes); // Log the with the new data
    } catch (err) {
      console.error(err); // Handle errors that may occur
    }
  }

  const filterId =  '0x4e7ef166cd43f188b0f8f9e218966a8f' //'YOUR_FILTER_iD'
  getFilterChanges(filterId)
  ```

  ```python web3.py theme={"system"}
  from web3 import Web3  
  node_url = "CHAINSTACK_NODE_URL" 
  web3 = Web3(Web3.HTTPProvider(node_url))

  def get_filtered_events(filter_id):
      try:
          filter_changes = web3.eth.get_filter_changes(filter_id)
          print(filter_changes) # the array of events that match the filter
      except Exception as e:
          print(e)

  filter_id = '0x0'
  get_filtered_events(filter_id)       
  ```
</CodeGroup>

## Use case

The `eth_getFilterChanges` method can be used to periodically poll a filter to retrieve new data. In the following Python program, using the web3.py library creates a filter using [eth\_newBlockFilter](/reference/avalanche-newblockfilter) to poll for new blocks, and uses `eth_getFilterChanges` to poll the filter and retrieve new blocks.

Here is the implementation in Python:

```python main.py theme={"system"}
from web3 import Web3  
node_url = "CHAINSTACK_NODE_URL" 
web3 = Web3(Web3.HTTPProvider(node_url))

def create_block_filter():
    try:
        # Create a new block filter
        block_filter = web3.eth.filter('latest')

        # Split the string at the space character
        parts = str(block_filter).split(' ')

        # Extract the filter value from the second part
        filter_id = parts[2]

        return filter_id
    except Exception as e:
        print(f'Error creating block filter: {e}')

def get_filtered_events(filter_id):
    try:
        filter_changes = web3.eth.getFilterChanges(filter_id)
        return filter_changes # the array of events that match the filter
    except Exception as e:
        print(f'Error getting filter changes: {e}')

def main():
    # Create a block filter
    block_filter_id = create_block_filter()
    if block_filter_id is not None:
        print(f'New block filter ID: {block_filter_id}')
    else:
        print('Error creating block filter')
        return

    # Loop forever and poll the filter every 10 seconds
    while True:
        print('Polling for new blocks...')
        new_blocks = get_filtered_events(block_filter_id)
        if new_blocks is not None:
            print(new_blocks)
        else:
            print('Error getting filter changes')
        time.sleep(10)

if __name__ == '__main__':
    main()
```

Here is the explanation of the code:

The `create_block_filter` function creates a new block filter with the `web3.eth.filter('latest')` method, which matches against the latest block. The resulting filter object is converted to a string, split into parts using the `split` method and the filter ID is extracted from the second part using array indexing. The filter ID is then returned by the function.

The `get_filtered_events` uses the `eth_getFilterChanges` method to retrieve the new blocks. The function takes a single parameter, `filter_id`, which is a string that represents the ID of the filter to be polled, the filter created by the `create_block_filter` function in this case.

If the filter ID is valid, `get_filtered_events` calls `web3.eth.getFilterChanges(filter_id)` to retrieve the array of events that match the filter criteria. This array is then returned by the function, which is then printed to the console in the `main` function.

The `main` function encapsulates the program logic, calling `create_block_filter` to create a new block filter and print the resulting filter ID to the console. The function then enters an infinite loop, calling `get_filtered_events` every 10 seconds to retrieve the changes to the blockchain that match the filter criteria. If the changes are not `None`, they are printed to the console. An error message is printed to the console if an error occurs while creating the block filter or retrieving the filter changes.


## OpenAPI

````yaml /openapi/avalanche_node_api/filter_handling/eth_getFilterChanges.json POST /8763cb5a211e1d4345acd51bde484c00/ext/bc/C/rpc
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-418-459-126.p2pify.com
security: []
paths:
  /8763cb5a211e1d4345acd51bde484c00/ext/bc/C/rpc:
    post:
      summary: eth_getFilterChanges
      operationId: getFilterChanges
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                jsonrpc:
                  type: string
                  default: '2.0'
                method:
                  type: string
                  default: eth_getFilterChanges
                params:
                  type: array
                  items:
                    type: string
                    title: The filter ID
                  default:
                    - '0xbdc5b4b99ca699e1d734fc4202afee79'
                id:
                  type: integer
                  default: 1
      responses:
        '200':
          description: The filter changes.
          content:
            application/json:
              schema:
                type: object
                properties:
                  jsonrpc:
                    type: string
                  id:
                    type: integer
                  result:
                    type: array
                    items:
                      type: string

````