Send a transaction with web3.js
In this tutorial, you'll send a regular transaction from one account to another using the Web3 JavaScript library.
The subpages in this section document the same process for a variety of implementations.

Prerequisites

You can use MetaMask or similar to create an Ethereum account for testing purposes.

Steps

1. Fund your Ethereum account

Use a faucet to load testnet ETH on your Ethereum account for the Ropsten network.
If using a network other than Ropsten, ensure you update your environment file with the network name.

2. Create a project directory

Create a new directory for your project. This can be done from the command line:
mkdir sendTransaction
Change into the new directory:
cd sendTransaction

3. Install required packages

Install the web3 and dotenv packages in the project directory.
The dotenv package allows you to use a .env file to securely store private environment variables on your local machine.
Install the web3 package:
npm install web3
Install the dotenv package:
npm install dotenv --save

4. Create the .env file

Create a .env file in your project directory to store the project and Ethereum account details.
ETHEREUM_NETWORK = "ropsten"
INFURA_PROJECT_ID = "<Your-Project-ID>"
SIGNER_PRIVATE_KEY = "<Your-Private-Key>"
Ensure you replace the following values in the .env file:
If using a network other than Ropsten, ensure you update ETHEREUM_NETWORK with the network name.
Never disclose your private key. Anyone with your private keys can steal any assets held in your account.

5. Create send.js file

In this example we'll create a JavaScript file (send.js) in the project directory which configures and sends the transaction.
const Web3 = require("web3");
async function main() {
// Configuring the connection to an Ethereum node
const network = process.env.ETHEREUM_NETWORK;
const web3 = new Web3(
new Web3.providers.HttpProvider(
`https://${network}.infura.io/v3/${process.env.INFURA_PROJECT_ID}`
)
);
// Creating a signing account from a private key
const signer = web3.eth.accounts.privateKeyToAccount(
process.env.SIGNER_PRIVATE_KEY
);
web3.eth.accounts.wallet.add(signer);
// Estimatic the gas limit
var limit = web3.eth.estimateGas({
from: signer.address,
to: "0xAED01C776d98303eE080D25A21f0a42D94a86D9c",
value: web3.utils.toWei("0.001")
}).then(console.log);
// Creating the transaction object
const tx = {
from: signer.address,
to: "0xAED01C776d98303eE080D25A21f0a42D94a86D9c",
value: web3.utils.numberToHex(web3.utils.toWei('0.01', 'ether')),
gas: web3.utils.toHex(limit),
nonce: web3.eth.getTransactionCount(signer.address),
maxPriorityFeePerGas: web3.utils.toHex(web3.utils.toWei('2', 'gwei')),
chainId: 3,
type: 0x2
};
signedTx = await web3.eth.accounts.signTransaction(tx, signer.privateKey)
console.log("Raw transaction data: " + signedTx.rawTransaction)
// Sending the transaction to the network
const receipt = await web3.eth
.sendSignedTransaction(signedTx.rawTransaction)
.once("transactionHash", (txhash) => {
console.log(`Mining transaction ...`);
console.log(`https://${network}.etherscan.io/tx/${txhash}`);
});
// The transaction is now on chain!
console.log(`Mined in block ${receipt.blockNumber}`);
}
require("dotenv").config();
main();

6. Execute the transaction

To execute the transaction, run:
node send.js
The command line displays a link to view the transaction details.