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 public mempool 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 tutorial will teach you how to use this endpoint to protect your transactions.

3 Steps to Send a Private Transaction

Create free Alchemy account

We'll use Alchemy to send our private transaction to Ethereum. You can create an account for free here.
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.
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
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 HTTP URL.
View Alchemy App - API key
View Alchemy App - API key

Make eth_sendPrivateTransaction API call

Now that we've got everything set up, we'll look at how to send private transactions using eth_sendPrivateTransaction over ethers.js.
Next, we will input the following app information:

Request

Ethers.js
Alchemy - Postman
Alchemy - Curl
1
const ethers = require("ethers.js");
2
3
const {
4
FlashbotsBundleProvider,
5
} = require("@flashbots/ethers-provider-bundle");
6
7
8
const provider = new ethers.providers.JsonRpcProvider({
9
url: "https://eth-mainnet.alchemyapi.io/v2/your-api-key", // Our app HTTP URL
10
});
11
12
const authSigner = new ethers.Wallet(
13
"ETHEREUM_PRIVATE_KEY" // Our Ethereum Private Key
14
);
15
16
17
const flashbotsProvider = await FlashbotsBundleProvider.create(
18
provider,
19
authSigner
20
);
21
22
const signedBundle = await flashbotsProvider.signBundle([
23
{
24
signer: "SIGNER_TO_SEND_FROM", // Signer adddress to send the transaction from
25
transaction: "TRANSACTION_TO_SEND", // The transaction to be sent
26
},
27
]);
28
29
const bundleReceipt = await flashbotsProvider.sendRawBundle(
30
signedBundle,
31
"TARGET_BLOCK_NUMBER" // The target block number
32
);
Copied!
1
URL: https://eth-mainnet.alchemyapi.io/v2/your-api-key // Our app HTTP URL
2
3
RequestType: POST
4
5
Body:
6
{
7
"jsonrpc":"2.0",
8
"method":"eth_sendPrivateTransaction",
9
"params":[{"tx": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675","maxBlockNumber": "0xcd23a0","preferences": { "fast": true }}],"id":1}'
10
"id":1
11
}
Copied!
1
curl https://eth-mainnet.alchemyapi.io/v2/your-api-key \
2
-X POST \
3
-H "Content-Type: application/json" \
4
-d '{"jsonrpc":"2.0","method":"eth_sendPrivateTransaction","params":[{"tx": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675","maxBlockNumber": "0xcd23a0","preferences": { "fast": true }}],"id":1}'
Copied!

Response

1
{
2
"jsonrpc": "2.0",
3
"id": 1,
4
"result": "0x45df1bc3de765927b053ec029fc9d15d6321945b23cac0614eb0b5e61f3a2f2a" // tx hash
5
}
Copied!

Conclusion

This tutorial explained how to send a private transaction on Ethereum using Ethers.js and Alchemy.
If you enjoyed this tutorial for sending a private transaction on Ethereum, give us a tweet @AlchemyPlatform! (Or if you have any questions/feedback)
Don't forget to join our Discord server to meet other blockchain devs, builders, and entrepreneurs!