axios
Here is a look at how Axios is used in blockchain development:
1. Using Axios as a Complement to Ethers.js
Ethers.js is a specialized library for interacting with Ethereum and EVM-compatible blockchains. It handles wallets, smart contracts, and direct JSON-RPC communication. Axios steps in for tasks that ethers.js doesn’t focus on, such as interacting with REST APIs or handling HTTP-specific scenarios.
Common Use Cases:
-
Querying Third-Party APIs: Axios is ideal for fetching data from external services like
Etherscan,
The Graph, or decentralized oracles (e.g.,
Chainlink).
const axios = require('axios');
const etherscanApi = 'https://api.etherscan.io/api';
const params = {
module: 'account',
action: 'txlist',
address: '0xYourWalletAddress',
apikey: 'YourAPIKey'
};
axios.get(etherscanApi, { params }).then(response => {
console.log(response.data);
});
Interfacing with Non-Standard APIs: Axios simplifies communication with services offering REST or GraphQL APIs, such as Dune Analytics or Messari.
Enhanced Error Handling: With built-in tools for managing HTTP-specific issues like timeouts, retries, and API rate limits, Axios improves resilience in API interactions.
2. Using Axios Without Ethers.js
Axios can also operate independently when ethers.js isn't required, such as for raw JSON-RPC interactions or working with non-EVM blockchains.
Common Use Cases:
-
Direct JSON-RPC Interactions: Axios allows sending raw JSON-RPC requests to blockchain nodes, useful for custom or low-level operations.
const axios = require('axios');
const rpcEndpoint = 'https://mainnet.infura.io/v3/YourProjectID';
const data = {
jsonrpc: '2.0',
method: 'eth_getBalance',
params: ['0xYourWalletAddress', 'latest'],
id: 1
};
axios.post(rpcEndpoint, data).then(response => {
console.log(response.data.result);
});
Custom Blockchain Solutions: Axios works well with non-EVM blockchains like
NEAR, or
Algorand, which use REST or gRPC APIs instead of Ethereum's JSON-RPC standard.
3. When to Choose Ethers.js Over Axios
Ethers.js is best suited for blockchain-native operations, including:
- Interacting with smart contracts.
- Managing wallets, signing transactions, and broadcasting them to the network.
- Fetching blockchain data like account balances or gas fees directly from a provider.
- Use Axios with ethers.js for external API calls and tasks beyond JSON-RPC functionality.
- Use Axios alone for raw JSON-RPC calls or when working with non-EVM blockchains.
- Use ethers.js for Ethereum-native operations and smart contract interactions.
With Axios and ethers.js, developers have a powerful toolkit to build feature-rich and efficient blockchain applications.
See: axios tutorial