SDK Examples

Real-world code examples for common OpenScan.AI API tasks.

Each example is a complete, runnable script. Set OPENSCAN_API_KEY to raise rate limits, or run anonymously for light usage.


Portfolio tracker

Fetch the native balance and token balances for an address:

const ADDRESS = "0x1234567890abcdef1234567890abcdef12345678";
const BASE = "https://xdcscan.io";

// Native balance
const addr = await (await fetch(BASE + "/api/v2/addresses/" + ADDRESS)).json();
console.log("Native balance:", Number(addr.coin_balance) / 1e18, "XDC");

// Token balances
const tokens = await (
await fetch(BASE + "/api/v2/addresses/" + ADDRESS + "/tokens?type=ERC-20")
).json();

for (const item of tokens.items) {
const decimals = Number(item.token.decimals);
const amount = Number(item.value) / 10 ** decimals;
console.log(item.token.symbol + ":", amount);
}

Whale alert bot

Poll an address’s transactions and post large incoming transfers to a webhook:

const WATCHED = "0x1234567890abcdef1234567890abcdef12345678";
const WEBHOOK = process.env.ALERT_WEBHOOK_URL;
const THRESHOLD_XDC = 100000;

let lastSeen = null;

async function poll() {
const res = await fetch(
"https://xdcscan.io/api/v2/addresses/" + WATCHED + "/transactions"
);
const { items } = await res.json();

for (const tx of items.reverse()) {
if (lastSeen && tx.hash <= lastSeen) continue;
const amount = Number(tx.value) / 1e18;
if (tx.to?.hash === WATCHED && amount >= THRESHOLD_XDC) {
await fetch(WEBHOOK, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
text: "Whale alert: " + amount.toLocaleString() +
" XDC received in tx " + tx.hash,
}),
});
}
lastSeen = tx.hash;
}
}

setInterval(poll, 15000);
poll();

Tip: for production alerting, use the Account API watchlist with webhook notifications instead of polling.

Export token holders to CSV

Paginate through all holders of a token and write them to a CSV file:

import { createWriteStream } from "node:fs";

const TOKEN = "0xabcdefabcdefabcdefabcdefabcdefabcdefabcd";
const out = createWriteStream("holders.csv");
out.write("address,balance_raw\n");

let params = null;
do {
const url =
"https://xdcscan.io/api/v2/tokens/" + TOKEN + "/holders" +
(params ? "?" + new URLSearchParams(params).toString() : "");
const page = await (await fetch(url)).json();

for (const holder of page.items) {
out.write(holder.address.hash + "," + holder.value + "\n");
}
params = page.next_page_params;
} while (params);

out.end();
console.log("Export complete");

Decode a transaction’s method call

Fetch a transaction and inspect the decoded input (works for verified contracts):

curl https://xdcscan.io/api/v2/transactions/0x9a8b7c6d5e4f3a2b1c0d9e8f7a6b5c4d3e2f1a0b9c8d7e6f5a4b3c2d1e0f9a8b

Search autocomplete

Wire a search box to the quick-search endpoint:

async function autocomplete(query) {
if (query.length < 3) return [];
const res = await fetch(
  "https://xdcscan.io/api/v2/search/quick?q=" + encodeURIComponent(query)
);
return res.json();
}

// Route results by entity type
for (const item of await autocomplete("usdc")) {
const paths = {
token: "/token/" + item.address,
address: "/address/" + item.address,
transaction: "/tx/" + item.tx_hash,
block: "/block/" + item.block_hash,
};
console.log(item.type, "->", paths[item.type]);
}

More resources