eth_getTransactionCount
curl --request POST \
--url https://polygon-mainnet.core.chainstack.com/0615fdf3c9eaf0681469e61a4308ea09 \
--header 'Content-Type: application/json' \
--data '
{
"id": 1,
"jsonrpc": "2.0",
"method": "eth_getTransactionCount",
"params": [
"0xe341b2f448eb190495ed4a89c01f20078b26b0f6",
"latest"
]
}
'import requests
url = "https://polygon-mainnet.core.chainstack.com/0615fdf3c9eaf0681469e61a4308ea09"
payload = {
"id": 1,
"jsonrpc": "2.0",
"method": "eth_getTransactionCount",
"params": ["0xe341b2f448eb190495ed4a89c01f20078b26b0f6", "latest"]
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
id: 1,
jsonrpc: '2.0',
method: 'eth_getTransactionCount',
params: ['0xe341b2f448eb190495ed4a89c01f20078b26b0f6', 'latest']
})
};
fetch('https://polygon-mainnet.core.chainstack.com/0615fdf3c9eaf0681469e61a4308ea09', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://polygon-mainnet.core.chainstack.com/0615fdf3c9eaf0681469e61a4308ea09",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'id' => 1,
'jsonrpc' => '2.0',
'method' => 'eth_getTransactionCount',
'params' => [
'0xe341b2f448eb190495ed4a89c01f20078b26b0f6',
'latest'
]
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://polygon-mainnet.core.chainstack.com/0615fdf3c9eaf0681469e61a4308ea09"
payload := strings.NewReader("{\n \"id\": 1,\n \"jsonrpc\": \"2.0\",\n \"method\": \"eth_getTransactionCount\",\n \"params\": [\n \"0xe341b2f448eb190495ed4a89c01f20078b26b0f6\",\n \"latest\"\n ]\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://polygon-mainnet.core.chainstack.com/0615fdf3c9eaf0681469e61a4308ea09")
.header("Content-Type", "application/json")
.body("{\n \"id\": 1,\n \"jsonrpc\": \"2.0\",\n \"method\": \"eth_getTransactionCount\",\n \"params\": [\n \"0xe341b2f448eb190495ed4a89c01f20078b26b0f6\",\n \"latest\"\n ]\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://polygon-mainnet.core.chainstack.com/0615fdf3c9eaf0681469e61a4308ea09")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n \"id\": 1,\n \"jsonrpc\": \"2.0\",\n \"method\": \"eth_getTransactionCount\",\n \"params\": [\n \"0xe341b2f448eb190495ed4a89c01f20078b26b0f6\",\n \"latest\"\n ]\n}"
response = http.request(request)
puts response.read_body{
"jsonrpc": "<string>",
"id": 123,
"result": {}
}Polygon node API
eth_getTransactionCount | Polygon
Polygon API method that returns the number of transactions sent from an address at the selected block. Chainstack Polygon reference.
POST
/
0615fdf3c9eaf0681469e61a4308ea09
eth_getTransactionCount
curl --request POST \
--url https://polygon-mainnet.core.chainstack.com/0615fdf3c9eaf0681469e61a4308ea09 \
--header 'Content-Type: application/json' \
--data '
{
"id": 1,
"jsonrpc": "2.0",
"method": "eth_getTransactionCount",
"params": [
"0xe341b2f448eb190495ed4a89c01f20078b26b0f6",
"latest"
]
}
'import requests
url = "https://polygon-mainnet.core.chainstack.com/0615fdf3c9eaf0681469e61a4308ea09"
payload = {
"id": 1,
"jsonrpc": "2.0",
"method": "eth_getTransactionCount",
"params": ["0xe341b2f448eb190495ed4a89c01f20078b26b0f6", "latest"]
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
id: 1,
jsonrpc: '2.0',
method: 'eth_getTransactionCount',
params: ['0xe341b2f448eb190495ed4a89c01f20078b26b0f6', 'latest']
})
};
fetch('https://polygon-mainnet.core.chainstack.com/0615fdf3c9eaf0681469e61a4308ea09', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://polygon-mainnet.core.chainstack.com/0615fdf3c9eaf0681469e61a4308ea09",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'id' => 1,
'jsonrpc' => '2.0',
'method' => 'eth_getTransactionCount',
'params' => [
'0xe341b2f448eb190495ed4a89c01f20078b26b0f6',
'latest'
]
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://polygon-mainnet.core.chainstack.com/0615fdf3c9eaf0681469e61a4308ea09"
payload := strings.NewReader("{\n \"id\": 1,\n \"jsonrpc\": \"2.0\",\n \"method\": \"eth_getTransactionCount\",\n \"params\": [\n \"0xe341b2f448eb190495ed4a89c01f20078b26b0f6\",\n \"latest\"\n ]\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://polygon-mainnet.core.chainstack.com/0615fdf3c9eaf0681469e61a4308ea09")
.header("Content-Type", "application/json")
.body("{\n \"id\": 1,\n \"jsonrpc\": \"2.0\",\n \"method\": \"eth_getTransactionCount\",\n \"params\": [\n \"0xe341b2f448eb190495ed4a89c01f20078b26b0f6\",\n \"latest\"\n ]\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://polygon-mainnet.core.chainstack.com/0615fdf3c9eaf0681469e61a4308ea09")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n \"id\": 1,\n \"jsonrpc\": \"2.0\",\n \"method\": \"eth_getTransactionCount\",\n \"params\": [\n \"0xe341b2f448eb190495ed4a89c01f20078b26b0f6\",\n \"latest\"\n ]\n}"
response = http.request(request)
puts response.read_body{
"jsonrpc": "<string>",
"id": 123,
"result": {}
}Polygon API method that returns the number of transactions sent from an address at the selected block. This value is also called
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; it is an important piece of information, especially to ensure that a transaction is not sent twice.
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.
Get your own node endpoint todayStart for free and get your app to production levels immediately. No credit card required.You can sign up with your GitHub, X, Google, or Microsoft account.
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 blockearliest— the earliest available or genesis blockpending— 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.
See the default block parameter.
Response
quantity— an integer value identifying the number of transactions sent from an address at the specified block.
eth_getTransactionCount code examples
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("0xe341b2f448eb190495ed4a89c01f20078b26b0f6", "latest")
from web3 import Web3
node_url = "CHAINSTACK_NODE_URL"
web3 = Web3(Web3.HTTPProvider(node_url))
print(web3.eth.get_transaction_count("0xe341b2f448eb190495ed4a89c01f20078b26b0f6", "latest"))
Use case
One of the most common use cases foreth_getTransactionCountis 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 and sign a raw transaction using ethers.js.
Security noticeYou need your private key to sign the transaction; never share your private key with anyone.
index.js
const ethers = require("ethers");
const NODE_URL = "CHAINSTACK_NODE_URL";
const provider = new ethers.JsonRpcProvider(NODE_URL);
// Initialize wallet with the 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()
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 | Polygon 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 | Polygon 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 signed using the wallet, which returns a rawTransaction that can be broadcasted to the network to execute the transaction using eth_sendRawTransaction | Polygon.Last modified on July 24, 2026
Was this page helpful?