DEV Community

Exchange Rate API
Exchange Rate API

Posted on

Build a 'Which Bank Pays Me the Most?' Tool in 40 Lines of JavaScript

If a worker in Dubai sends dirhams home to Sri Lanka, the receiving bank sets the exchange rate, and banks differ. On a 1,000 USD transfer, the gap between the best and worst bank board can easily be several thousand rupees.

This tool lists every covered bank in a country and ranks them by what they pay for foreign currency. The central bank's official rate sits alongside as a neutral yardstick.

The data

GET /api/v1/bank-rates/{country} returns every covered bank's latest board in that country. Each row has the bank's buy and sell prices, its spread_pct, and the central bank's official rate for the same currency. The API covers 1,219 banks in 124 countries; the country and bank codes are listed on the bank codes page.

The code

const KEY = process.env.ALLRATES_KEY;

async function bestBanks(country, ccy, amount) {
  const res = await fetch(
    `https://allratestoday.com/api/v1/bank-rates/${country}`,
    { headers: { Authorization: `Bearer ${KEY}` } }
  );
  const data = await res.json();

  const rows = data.banks
    .map((b) => ({ bank: b.name, r: b.rates.find((x) => x.ccy === ccy), at: b.captured_at }))
    .filter((x) => x.r)
    .map(({ bank, r, at }) => ({
      bank,
      buy: r.buy,                      // what the bank pays you per 1 unit
      youGet: +(amount * r.buy).toFixed(2),
      spread: r.spread_pct,
      vsOfficial: r.official ? +(((r.buy / r.official) - 1) * 100).toFixed(2) : null,
      at,
    }))
    .sort((a, b) => b.buy - a.buy);

  return rows;
}

const table = await bestBanks('lk', 'USD', 1000);
console.table(table);
Enter fullscreen mode Exit fullscreen mode

The output is one row per bank, best first: how many rupees 1,000 USD becomes, the bank's spread, and how far its buy rate sits below the central bank's official rate.

Things worth knowing

  • Buy vs sell. When you receive foreign currency, the bank's buy rate applies (higher is better). When you send money abroad, its sell rate applies (lower is better).
  • Compare like with like. Banks print separate boards for transfers and for cash. Each response carries a basis field (for example "Telegraphic transfer"), and every bank in a country uses the same basis.
  • Timestamps. captured_at says when the board was read, and bank_updated_at carries the bank's own stamp where it prints one.

Why one API matters here

This tool uses two kinds of rate: bank boards and the central bank's official rate. Add the live mid-market rate from /api/v1/rates and you can also show what the transfer is worth before any bank margin. All three come from one provider with one key.

Free key: allratestoday.com/register ยท Docs: allratestoday.com/docs#bank-rates

Top comments (0)