> ## 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_getTransactionCount | Gnosis

> Gnosis Chain API method that returns the number of transactions sent from an address at the selected block. Gnosis via Chainstack.

Gnosis Chain API method that returns the number of transactions sent from an address at the selected block. This value is also called `nonce`; it is an important piece of information, especially to ensure that a transaction is not sent twice.

<Note>
  When called against a block older than the latest \~128 blocks, this method is treated as an archive request (2 RUs instead of 1 RU). See [request units](/docs/request-units#archive-state-methods).
</Note>

## Parameters

* `address`— the address to retrieve the transaction count.
* `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://eth.wiki/json-rpc/API#the-default-block-parameter) 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 identifying the number of transactions sent from an address at the specified block.

## `eth_getTransactionCount` 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 getNonce = async (address, blockId) => {
      const nonce = await provider.send("eth_getTransactionCount", [address, blockId]);
      console.log(nonce);
    };

    getNonce("0xc448b51CF665210419543C5A53F13B8cE87834B2", "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_transaction_count("0xc448b51CF665210419543C5A53F13B8cE87834B2", "latest")) 
  ```
</CodeGroup>

## Use case

One of the most common use cases for `eth_getTransactionCount`is to create the transaction object built in a script designed to send a transaction. The nonce field is required, and it is retrieved using the `eth_getTransactionCount` method.

The following code shows how to build a `rawTransaction` using ethers.js.

<Warning>
  **Security notice**

  You need your private key to sign the transaction; never share your private key with anyone.

  On a side note, the private key in this case must be imported without '0x' at the beginning of the string.
</Warning>

```javascript index.js theme={"system"}
const ethers = require("ethers");

const NODE_URL = "CHAINSTACK_NODE_URL";
const provider = new ethers.JsonRpcProvider(NODE_URL);

// Initialize the wallet with your private key
const PRIVATE_KEY = "PRIVATE_KEY";
const wallet = new ethers.Wallet(PRIVATE_KEY, provider);

// Validate input parameters and return an error message if invalid
const validateInputs = (fromAddress, toAddress) => {
  if (!ethers.isAddress(fromAddress)) {
    throw new Error(`Invalid fromAddress: ${fromAddress}`);
  }
  if (!ethers.isAddress(toAddress)) {
    throw new Error(`Invalid toAddress: ${toAddress}`);
  }
};

// Async function to create a raw transaction
async function createRawTransaction(fromAddress, toAddress, value) {
  validateInputs(fromAddress, toAddress);

  const nonce = await provider.getTransactionCount(fromAddress);
  const gasPrice = (await provider.getFeeData()).gasPrice;
  const gasLimit = await provider.estimateGas({
    from: fromAddress,
    to: toAddress,
  });

  // Build the transaction object
  const transactionObject = {
    to: toAddress,
    gasPrice: gasPrice,
    gasLimit: gasLimit,
    nonce: nonce,
    value: value,
  };

  // Sign the transaction using the wallet and return the raw transaction
  const rawTransaction = await wallet.signTransaction(transactionObject);

  return rawTransaction;
}

async function main() {
  const rawTransaction = await createRawTransaction('0x6f46cf5569aefa1acc1009290c8e043747172d89', "0xe341b2f448eb190495ed4a89c01f20078b26b0f6" , "100000000000000")
  console.log(`Raw transaction: ${rawTransaction}`)
}

main()
```

In summary, the code creates a valid raw transaction that can be broadcasted to the network.

During the process, the script retrieves important values such as the `nonce`, `gasPrice`, and `gasLimit`, builds a transaction object, and signs it using a private key.

First, the `createRawTransaction` function calls `validateInputs` to ensure that the `fromAddress` and `toAddress` parameters are valid addresses. If either of these addresses is invalid, the function throws an error with a descriptive message.

Next, the function makes a call to `provider.getTransactionCount` with the `fromAddress` as a parameter. This method returns the number of transactions sent from the `fromAddress`, and is used as the `nonce` for the transaction.

The function retrieves the `gasPrice` and `gasLimit` for the transaction using `provider.getFeeData` and `provider.estimateGas`, respectively. The [eth\_gasPrice](/reference/gasprice) is the amount of gas that the transaction sender is willing to pay per unit of gas consumed by the transaction, while the [eth\_estimateGas](/reference/gnosis-estimategas) is an estimate of the maximum amount of gas that the transaction will consume.

With the `nonce`, `gasPrice`, and `gasLimit` values, the function builds a transaction object, which includes the `toAddress`, `gasPrice`, `gasLimit`, `nonce`, and `value` of the transaction. The value is the amount transferred from the `fromAddress` to the `toAddress`.

The transaction object is then passed to `wallet.signTransaction`, which signs it using the wallet's private key and returns the serialized signed transaction as a `rawTransaction`. This raw transaction can be broadcasted to the network to execute the transaction using [eth\_sendRawTransaction](/reference/gnosis-sendrawtransaction).


## OpenAPI

````yaml openapi/gnosis_node_api/accounts_info/eth_getTransactionCount.json POST /512e720763b369ed620657f84d38d2af
openapi: 3.0.0
info:
  title: Upload
  version: 1.0.0
  description: This is an API for interacting with a Chainstack node.
servers:
  - url: https://nd-500-249-268.p2pify.com
security: []
paths:
  /512e720763b369ed620657f84d38d2af:
    post:
      tags:
        - upload
      summary: eth_getTransactionCount
      operationId: getTransactionCount
      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_getTransactionCount
                params:
                  type: array
                  items:
                    anyOf:
                      - type: string
                        title: Address
                        description: The address to check
                      - type: string
                        title: Block identifier
                        description: The block identifier
                  default:
                    - '0x7584F3c08bF632EED687f45592EA9f5690fc1D41'
                    - latest
      responses:
        '200':
          description: The address nonce
          content:
            application/json:
              schema:
                type: object
                properties:
                  jsonrpc:
                    type: string
                  id:
                    type: integer
                  result:
                    type: object

````