Batch requests
MM Connect provides a metamask_batch method to send multiple JSON-RPC requests in a single call.
These requests can be contract calls or other JSON-RPC methods (for example, signing messages or sending transactions).
Despite being batched into one HTTP request, each call still requires individual user approval, and if any request is rejected, the entire batch fails.
"Batching" can also refer to Wagmi contract read batching or sending atomic batch transactions in MetaMask.
Batch JSON-RPC requests
You can directly use MM Connect's metamask_batch method to group multiple JSON-RPC requests into a single HTTP call.
Use cases include:
- Batching multiple signatures - Send multiple signing requests in one batch.
- Switching networks - Switch the EVM network, perform an action such as sending a transaction, and switch back, all in one batch.
- Mixed transactions and signatures - Combine transaction sending and signing requests in one batch.
When using metamask_batch, keep in mind the following:
- Even though the requests are batched, each individual request still requires user approval.
- If any request in the batch is rejected, the entire batch will fail.
The following is an example of batching JSON-RPC requests using metamask_batch:
import { MetaMaskSDK } from "@metamask/sdk";
const MMSDK = new MetaMaskSDK();
const provider = MMSDK.getProvider();
async function handleBatchRequests() {
// Example batch: one personal_sign call and one eth_sendTransaction call.
const requests = [
{ method: "personal_sign", params: ["Hello from batch!", "0x1234..."] },
{
method: "eth_sendTransaction",
params: [
{
from: "0x1234...",
to: "0xABCD...",
// Additional transaction parameters.
},
],
},
];
try {
const results = await provider.request({
method: "metamask_batch",
params: [requests],
});
console.log("Batch Results:", results);
} catch (err) {
console.error("Batch request failed:", err);
}
}
document.getElementById("batchBtn").addEventListener("click", handleBatchRequests);
The following HTML displays a Send Batch button:
<button id="batchBtn">Send Batch</button>
- For a better user experience, it's important to use reliable RPC providers instead of public nodes. We recommend using services like MetaMask Developer to ensure better reliability and performance.
- Ensure that requests in a batch do not depend on one another's chain context, as mid-batch state changes can affect outcomes.