Skip to main content

Ethers.js: How to send batch requests with Infura

In this guide we'll write a simple script using Ethers.js and Infura for sending batch requests.

We'll take advantage of Ethers.js using the batch request method, you may find all the relevant details in the Ethers.js documentation.

Please don't forget to use your Infura API key.

Our script will send 2 requests using eth_getBalance and eth_getTransactionCount and then print the results.

const { providers, JsonRpcBatchProvider } = require('ethers')

const infuraProvider = new providers.InfuraProvider('goerli', 'YOUR_INFURA_API_KEY')
const batchProvider = new JsonRpcBatchProvider(infuraProvider)

const requests = [
{
method: 'eth_getBalance',
params: ['0x742d35Cc6634C0532925a3b844Bc454e4438f44e', 'latest'],
},
{
method: 'eth_getTransactionCount',
params: ['0x742d35Cc6634C0532925a3b844Bc454e4438f44e', 'latest'],
},
]

batchProvider.send(requests).then(results => {
console.log(results)
})
Was this helpful?
Connect MetaMask to provide feedback
What is this?
This is a trial feedback system that uses Verax to record your feedback as onchain attestations on Linea Mainnet. When you vote, submit a transaction in your wallet.