Skip to main content

Code Examples

Practical examples for using NodeTrace with popular Web3 libraries.

Quick Start with cURL

# Get current block number
curl -X POST https://eth.nodetrace.xyz/rpc/YOUR_API_KEY \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}'

# Get account balance
curl -X POST https://eth.nodetrace.xyz/rpc/YOUR_API_KEY \
-H "Content-Type: application/json" \
-d '{
"jsonrpc":"2.0",
"method":"eth_getBalance",
"params":["0x742d35Cc6634C0532925a3b844Bc9e7095833a06", "latest"],
"id":1
}'

Web3.js Example

const Web3 = require('web3');

// Create Web3 instance with API key in URL
const web3 = new Web3(
new Web3.providers.HttpProvider(
`https://eth.nodetrace.xyz/rpc/${process.env.NODETRACE_API_KEY}`
)
);

// Get account balance
async function getBalance(address) {
const balance = await web3.eth.getBalance(address);
return web3.utils.fromWei(balance, 'ether');
}

// Get transaction details
async function getTransaction(txHash) {
const tx = await web3.eth.getTransaction(txHash);
return tx;
}

// Example usage
const balance = await getBalance('0x742d35Cc6634C0532925a3b844Bc9e7095833a06');
console.log('Balance:', balance, 'ETH');

Ethers.js Example

const { ethers } = require('ethers');

// Create provider with API key in URL
const provider = new ethers.JsonRpcProvider(
`https://eth.nodetrace.xyz/rpc/${process.env.NODETRACE_API_KEY}`
);

// Get account balance
async function getBalance(address) {
const balance = await provider.getBalance(address);
return ethers.formatEther(balance);
}

// Monitor new blocks
provider.on('block', (blockNumber) => {
console.log('New block:', blockNumber);
});

Viem Example

import { createPublicClient, http, formatEther } from 'viem';
import { mainnet } from 'viem/chains';

// Create client with API key in URL
const client = createPublicClient({
chain: mainnet,
transport: http(
`https://eth.nodetrace.xyz/rpc/${process.env.NODETRACE_API_KEY}`
),
});

// Get account balance
async function getBalance(address: `0x${string}`) {
const balance = await client.getBalance({ address });
return formatEther(balance);
}

// Get block with transactions
async function getBlockWithTransactions(blockNumber: bigint) {
const block = await client.getBlock({
blockNumber,
includeTransactions: true,
});
return block;
}

Error Handling

async function makeRPCCall(method, params) {
try {
const response = await fetch(
`https://eth.nodetrace.xyz/rpc/${process.env.NODETRACE_API_KEY}`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
jsonrpc: '2.0',
method,
params,
id: 1,
}),
}
);

if (response.status === 429) {
console.log('Rate limited. Please wait before retrying.');
return null;
}

const data = await response.json();

if (data.error) {
throw new Error(`RPC Error: ${data.error.message}`);
}

return data.result;
} catch (error) {
console.error('RPC call failed:', error);
throw error;
}
}

Best Practices

  1. Store API keys securely - Use environment variables, never hardcode
  2. Handle rate limits - Check response status and implement retry logic
  3. Cache responses - Cache immutable data like historical blocks
  4. Use batch requests - Combine multiple calls when possible

Need More Examples?