Blog / Build with AI /
Building a currency converter widget with AI
If you sell to people in other countries, or just travel and shop online, you’ve done this: opened a new tab, searched for “dollars to naira” or “pounds to euros”, and typed the number again.
A currency converter widget does it on the page. It’s a classic small project: a clear job, an outside data source, and enough edge cases to teach you something about working with APIs. Let’s build one with an AI assistant.
The finished thing
A small box with an amount field, a “from” currency, a “to” currency and a result. Change the amount and the answer updates. A swap button flips the two currencies. Under the result, it shows when the rates were last updated.
Where the rates come from
Your code can’t invent exchange rates. You need a source: an exchange-rate API.
Several providers offer one. Some are free with limits, some need a key, and some are paid. Free plans often update rates once a day, or less often than you’d like. Before you pick one, check:
- How often the rates update. Daily is fine for a rough tool; not for anything financial.
- Which currencies it covers. Not every API includes every currency, and some currencies, especially ones with tight controls or several rates, are not handled well.
- The limits. Requests per month, and whether the free tier allows commercial use.
- The terms. Some require you to show attribution.
Be careful about currencies where the official rate and the street rate differ greatly. A widget showing an official rate may not reflect what people actually pay. If your audience lives somewhere like that, say so on the page.
Step 1: don’t expose the API key
If the API needs a key, you shouldn’t put it in your page’s JavaScript, where anyone can copy it. Never put API keys in front-end code explains why. Instead, your page should call a small server function of your own, which holds the key and fetches the rates. If the API doesn’t need a key, you can call it directly.
Step 2: the prompt
Build a currency converter widget as a single HTML file with plain JavaScript. It has an amount input, two dropdowns for “from” and “to” currency, a swap button and a result line. Fetch the latest rates from [API URL] once per page load and store them in local storage along with the time they were fetched; reuse stored rates if they are less than 12 hours old. Convert in the browser using the rates. Show “Rates last updated: [date and time]” under the result. Format the numbers according to the user’s locale. If the fetch fails, use the stored rates if any and show a warning; otherwise show a clear error. Validate the input: ignore non-numeric characters, handle empty input and don’t allow negative amounts. Add comments.
You’ve given it the data source, the caching rule, the display rules and the failure behaviour. That level of detail is what separates a working widget from a fragile one.
Notice the 12-hour rule. Fetching new rates on every keystroke would burn through your API limit quickly. The same idea is behind caching AI answers to save money: don’t ask for the same thing twice.
Step 3: how the maths works
Most APIs return rates against one base currency. Say the base is USD and you get:
USD -> EUR: 0.92
USD -> NGN: 1500
(These numbers are only an illustration.) To convert from EUR to NGN, go through the base:
amount in USD = amount in EUR / rate(EUR)
amount in NGN = amount in USD * rate(NGN)
In code: result = amount / rates[from] * rates[to].
This cross-rate method is simple, but it’s a classic place for bugs, such as dividing the wrong way round. Test it with a conversion you can check by hand: 1 USD to EUR should give you the EUR rate, and converting back should return roughly 1.
Step 4: test the awkward cases
- Amount of zero, empty or a very big number.
- Same currency in both boxes.
- Decimals with a comma (“1,5”) as many countries write them.
- A currency with no decimal places, like the Japanese yen, versus most with two.
- Your internet turned off.
- A rates file that’s missing a currency.
For each failure, give your assistant the input, the result and the expectation. If you want to see this debugging loop in detail, see prompts that fix bugs vs prompts that create new ones.
Step 5: be honest on the page
This is the part most widgets skip. Add a line like:
Rates are indicative and may differ from what your bank or payment provider charges. Last updated [time].
Banks and card providers add their own margins and fees, so the “real” amount someone pays is usually different. A widget that implies otherwise can cause arguments, especially on a shop.
For shops, a converter is also not the same as a multi-currency checkout. If you want customers to pay in their own currency, use your platform’s built-in currency features, which handle rounding, fees and payment. A widget just estimates.
Step 6: make it look at home
- Match your site’s fonts and colours.
- Make the inputs large enough to use on a phone.
- Label everything, and make sure it works with a keyboard.
- Remember the user’s last-used currency pair in local storage.
- Keep the file small. A widget that slows your page down defeats the purpose. Making AI-built sites fast covers this.
If you want to embed it in WordPress, a custom HTML block or a shortcode will do. WordPress shortcodes built with AI shows how to wrap a widget so you can place it anywhere with a short tag.
Privacy and legal notes
A converter that runs in the browser doesn’t need to collect anything. Keep it that way. If you add analytics, mention it. If you display rates from a provider, check the terms about attribution and redistribution.
Ideas for later
- Show a few favourite currencies at once.
- Add a small history chart (and make sure the axis tells the truth).
- Offer a “price on this page in my currency” tool, which is more complex because it needs to read prices from the page.
Key takeaways
- A converter needs a rates source; check update frequency, coverage and limits.
- Cache rates for hours, not seconds, and protect any API key.
- Cross rates go through a base currency; test the maths by hand.
- Say the rates are indicative and show when they were fetched.
Related reading
- Building a markdown to HTML converter with AI
- Building a unit converter with AI
- Building a simple API for your own data with AI
Which currency pair would you test it on first?