Does Alchemy support batch requests?
Alchemy supports batch requests for certain types of requests, but there are specific limitations and best practices to be aware of, particularly with enhanced and specialized APIs.
Understanding Batch Requests
Batch requests allow multiple API calls to be nested within a single HTTP request. These are processed concurrently by the server and responses may return in any order. While this method efficiently bundles several requests together, it can complicate the handling of responses and may impact reliability for certain API types.
Batch Request Limits
HTTP: You can include up to 1000 requests in a single batch over HTTP. Exceeding this number may lead to decreased reliability.
WebSockets: The maximum number of requests per batch over WebSocket connections is limited to 20.
Supported and Unsupported APIs for Batch Requests
Batch requests are not supported for Alchemy’s enhanced APIs (e.g., APIs prefixed with alchemy_) and specific APIs under the Trace/Debug categories. Here are more details:
Unsupported APIs:
Transfers API
Most Transact APIs (exceptions: eth_sendPrivateTransaction and eth_cancelPrivateTransaction)
Transaction Receipts API
Token APIs
Most Subscription APIs (exceptions: newPendingTransactions, newHeads, and logs)
Trace and Debug APIs
Supported APIs: Standard JSON-RPC methods, such as eth_blockNumber, can be batched.
Making a Batch Request
To make a batch request, format your HTTP POST request with an array containing the individual API calls. Here’s an example for a batch request using the eth_blockNumber method:
curl <https://eth-mainnet.g.alchemy.com/v2/your-api-key> \\
-X POST \\
-H "Content-Type: application/json" \\
-d '[{"jsonrpc": "2.0", "id": 1, "method": "eth_blockNumber", "params": []},
{"jsonrpc": "2.0", "id": 2, "method": "eth_blockNumber", "params": []},
{"jsonrpc": "2.0", "id": 3, "method": "eth_blockNumber", "params": []},
{"jsonrpc": "2.0", "id": 4, "method": "eth_blockNumber", "params": []}]'
Handling Errors in Batch Requests
Even if a batch request returns an HTTP status code of 200, individual requests within the batch may fail. It is important to parse the JSON-RPC error codes in the response to determine the success or failure of each included request.
📘 For more information on batch requests and detailed guidance on making and handling them, please refer to Alchemy’s documentation here