How to Send a Private Transaction on Ethereum

In this tutorial, we will learn how to send a private transaction on Ethereum.

When transactions are sent to the blockchain, they are distributed across the entire network. Transactions that have not been mined yet are classified as “pending” transactions. You can learn more about the various transactions states, here. Pending transactions live in the and are visible to the entire network, which makes them prone to front-running and other forms of arbitrage.

In order to protect your transactions, you can send them through a private endpoint, eth_sendPrivateTransaction, which skips the public mempool and is sent directly to miners. This endpoint is powered by Flashbots, a leading web3 organization focusing on protecting developers against these types of arbitrage opportunities (better known as Miner Extractable Value, or MEV).

This tutorial will teach you how to use this endpoint to protect your transactions.

3 Steps to Send a Private Transaction

  1. Create free Alchemy account
  2. Create an Alchemy App
  3. Make a sendPrivateTransaction API call via the Alchemy SD

Create free Alchemy account

We’ll use Alchemy to send our private transaction to Ethereum. You can create an account for free here.

3360

Create free Alchemy Account

Create an Alchemy App

After creating a new account successfully, we will be redirected to our dashboard, where we can create an App by clicking on the Create App button as shown below.

3319

Create an Alchemy Ethereum Mainnet App

Next, we will input the following app information:

  • Name: Transaction App
  • Description: Feel free to put anything here.
  • Chain: Ethereum
  • Network: Mainnet

2808

Create an Alchemy Ethereum Mainnet App

Click on the View Key button as shown below**,** which will open a popup with our app’s HTTP and Websocket URLs. In this tutorial, we will be using the Api Key. Save this for later.

1771

View Alchemy App - API key

Make sendPrivateTransaction call via the Alchemy SDK

Now that we’ve got everything set up, we’ll look at how to send private transactions using eth_sendPrivateTransaction over the Alchemy SDK

1. Create a new project directory and cd into it

Create a new project directory from the command line (terminal for macs) and navigate into it:

shell
$mkdir sendtx-example
>cd sendtx-example

2. Install the Alchemy SDK and dotenv

Run the following command in your project directory:

shell
$npm install alchemy-sdk dotenv

3. Create the .env file

We’ll use a .env file to safely store our API key and private key.

We make a .env file to securely store private environmental variables in our local machine that we may access from other files (some of which we can make public).

If you want to check out how dotenv actually works in the context of a conventional NodeJS server file, check out this helpful video!

Create a .env file (make sure the file is literally just named .env, nothing more) in your project directory and add the following (replacing your-api-key and your-private-key, keeping both within the quotation marks):

  • To find your Alchemy API Key, use the instructions shared above.
  • To find your private key using Metamask, check out this guide.
.env
1API_KEY = "your-api-key"
2PRIVATE_KEY = "your-private-key"

4. Create sendPrivateTx.js file

Great, now that we have our sensitive data protected in a .env file, let’s start coding. For our send transaction example, we’ll be sending Ethereum to a test wallet.

Create a sendTx.js file, which is where we will configure and send our example transaction, and add the following lines of code to it:

sendPrivateTx.js
1// Setup: npm install alchemy-sdk
2// Github: https://github.com/alchemyplatform/alchemy-sdk-js
3import { Alchemy, Network, Wallet, Utils } from "alchemy-sdk";
4import dotenv from "dotenv";
5
6dotenv.config();
7const { API_KEY, PRIVATE_KEY } = process.env;
8
9const settings = {
10 apiKey: API_KEY,
11 network: Network.ETH_MAINNET, // Replace with your network.
12};
13const alchemy = new Alchemy(settings);
14
15let wallet = new Wallet(PRIVATE_KEY);
16
17const nonce = await alchemy.core.getTransactionCount(wallet.address, "latest");
18
19let exampleTx = {
20 to: "0x4b9007B0BcE78cfB634032ec31Ed56adB464287b",
21 value: 10,
22 gasLimit: "21000",
23 maxFeePerGas: Utils.parseUnits('20', 'gwei'),
24 nonce: nonce,
25 type: 2,
26 chainId: 5,
27};
28
29let rawTransaction = await wallet.signTransaction(exampleTx);
30
31const signedTx = await alchemy.transact.sendPrivateTransaction(
32 rawTransaction,
33 (await alchemy.core.getBlockNumber()) + 1
34);
35
36console.log(signedTx);

Now, before we jump into running this code, let’s talk about some of the components here.

  • Wallet: This object stores your private key, and can be accessed to sign a transaction with your private key or to return your public address.

  • nonce : The nonce specification is used to keep track of the number of transactions sent from your address. We need this for security purposes and to prevent replay attacks. To get the number of transactions sent from your address we use eth_getTransactionCount

  • transaction: The transaction object has a few aspects we need to specify

    • to: This is the address we want to send Eth to. In this case, we are sending Eth to a test wallet.
    • gasLimit: This is the maximum amount of gas you are willing to consume on a transaction. Standard limit is 21000 units.
    • value: This is the amount we wish to send, specified in wei where 10^18 wei = 1 ETH
    • maxFeePerGas: This is the total amount you are willing to pay per gas for the transaction to execute. Since EIP 1559, this field or the maxPriorityFeePerGas field is required. Specified in wei, where 10^18 wei = 1 ETH
    • nonce: see above nonce definition. Nonce starts counting from zero.
    • [OPTIONAL] data: Used for sending additional information with your transfer, or calling a smart contract, not required for balance transfers, check out the note below.
  • rawTransaction: To sign our transaction object we will use the signTransaction method of our wallet, generating a raw “signed” transaction.

  • sendPrivateTransaction: Once we have a signed transaction, we can send it off via Alchemy’s servers to be included in a subsequent block by using sendPrivateTransaction request.

There are two main types of transactions that can be sent in Ethereum.

5. Run the code using node sendPrivateTx.js

Navigate back to your terminal or command line and run:

shell
$node sendPrivateTx.js

From there you can view your transaction on Etherscan by clicking on the icon circled in red!

Yippieeee! You just sent your first private transaction using Alchemy 🎉

Once you complete this tutorial, let us know how your experience was or if you have any feedback by tagging us on Twitter @Alchemy!

_For feedback and suggestions about this guide, please message Deric on Alchemy’s Discord! _

_Not sure what to do next? As a final test of your skills, get your hands dirty with some solidity programming by implementing our Hello World Smart Contract tutorial.