JavaScript Examples
Custom implementation examples using JavaScript and popular libraries.
For production use, see the TypeScript SDK which provides full type safety, WebSocket streaming, and advanced features.
Setup
npm install axios ethers
Basic Request
const axios = require('axios');
const client = axios.create({
baseURL: 'https://api.axol.io/v1',
headers: {
'X-API-Key': process.env.AXOL_API_KEY,
'Content-Type': 'application/json'
}
});
async function getStatus(chain) {
const { data } = await client.get(`/blockchain/${chain}/status`);
return data;
}
WebSocket Connection
const WebSocket = require('ws');
function connectWebSocket() {
const ws = new WebSocket(`wss://ws.axol.io?token=${process.env.AXOL_API_KEY}`);
ws.on('open', () => {
ws.send(JSON.stringify({
type: 'subscribe',
channels: ['gas_prices_ethereum']
}));
});
ws.on('message', (data) => console.log('Update:', JSON.parse(data)));
ws.on('error', console.error);
return ws;
}
Transaction Monitoring
async function monitorTransaction(txHash, chain = 'ethereum') {
while (true) {
const { data } = await client.get(`/blockchain/${chain}/tx/${txHash}`);
if (data.confirmations >= 12) {
console.log('Transaction confirmed!');
break;
}
console.log(`Confirmations: ${data.confirmations}`);
await new Promise(r => setTimeout(r, 5000));
}
}
Gas Price Oracle
async function getOptimalGasPrice(chain = 'ethereum') {
const { data } = await client.get(`/gas/${chain}/current`);
return {
economy: data.slow,
normal: data.standard,
priority: data.fast
};
}
async function sendTransaction(wallet, to, value) {
const gas = await getOptimalGasPrice();
const ethers = require('ethers');
return wallet.sendTransaction({
to,
value: ethers.parseEther(value),
gasPrice: ethers.parseUnits(gas.normal.toString(), 'gwei')
});
}