Dev.to WebDev 🛠 Dev 👁 0 📖 3 min read

Build a Free Currency Converter for a Static Site: 25 Lines of JavaScript, No Backend

Static sites can't hide an API key, which is why most currency converter tutorials end with "now set up a small server". You can skip that step: AllRatesToday's open endpoint needs no key and allows cross-origin requests

Static sites can't hide an API key, which is why most currency converter tutorials end with "now set up a small server". You can skip that step: AllRatesToday's open endpoint needs no key and allows cross-origin requests, so the browser can call it directly.

Here's a complete converter for GitHub Pages, Netlify, Cloudflare Pages or a plain HTML file.

The HTML

<form id="fx">
  <input id="amount" type="number" value="100" min="0" step="any">
  <select id="from"></select> →
  <select id="to"></select>
  <output id="result"></output>
  <small id="asof"></small>
</form>
<p><a href="https://allratestoday.com">Exchange rates by AllRatesToday</a></p>

The JavaScript

const $ = (id) => document.getElementById(id);
let table = { base: null, rates: {}, date: "" };

async function load(base) {
  if (table.base === base) return;                                  // already have this base
  const r = await fetch(`https://allratestoday.com/api/open/latest/${base}`);
  const d = await r.json();
  table = { base, rates: d.rates, date: d.date };
}

async function update() {
  await load($("from").value);
  const out = $("amount").value * table.rates[$("to").value];
  $("result").textContent = `${out.toLocaleString(undefined, { maximumFractionDigits: 2 })} ${$("to").value}`;
  $("asof").textContent = `ECB reference rates of ${table.date}`;
}

(async () => {
  await load("USD");
  for (const code of Object.keys(table.rates).sort()) {
    $("from").add(new Option(code, code, code === "USD", code === "USD"));
    $("to").add(new Option(code, code, code === "EUR", code === "EUR"));
  }
  $("fx").addEventListener("input", update);
  update();
})();

That's the whole thing: 30 currencies, any base, updated once a day from the European Central Bank's reference rates.

Why it fetches by base

The endpoint returns every rate against the base you ask for (/latest/USD, /latest/EUR, ...). When the user changes the "from" currency, the code fetches that base's table once and reuses it for every keystroke. Changing only the amount or the "to" currency never makes a network call.

You could also fetch one base and cross-compute (rates[to] / rates[from]), which saves a request. Both give the same result to rounding.

Be polite with requests

The open endpoint allows about one request per minute per IP, and the data changes once a day. For a converter, that's plenty: one or two fetches per visit. If you want to survive reloads, store the table in localStorage until time_next_update_utc from the response.

Keep the link

The open endpoint is free for commercial sites too. The condition is a visible, normal link wherever the rates are shown publicly, which is the <p> line in the HTML above. Every response includes the snippet in attribution_html.

Don't want to write code? Use the widget

One div and one script tag give you a converter with the attribution built in:

<div class="art-currency-widget" data-base="USD" data-target="EUR"></div>
<script async src="https://allratestoday.com/widget.js"></script>

There's also an iframe version and themes on the widget page.

When you outgrow it

If you need live rates (every 60 seconds), 160+ currencies, or historical rates, move to the keyed API, with a free key to start. Call it from a server or serverless function, not from browser code, so the key stays private.

Links

Exchange rates by AllRatesToday.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.