A common ask in invoicing/reconciliation code: "what was the USD/EUR rate on 2024-01-15?" The naive approach — pick a rate source, hit it with a date, trust the number — glosses over one detail that matters: ECB reference rates only publish on business days, so "the rate on" a weekend or bank-holiday date doesn't exist as a distinct value.
The honest way to handle this is to resolve the requested date to the nearest actual trading date and say so in the response, rather than silently picking one:
async function getHistoricalRate(date, from, to) {
const res = await fetch(`https://currency-api.p.rapidapi.com/v1/historical?date=${date}&from=${from}&to=${to}`, {
headers: { "X-RapidAPI-Key": key, "X-RapidAPI-Host": "currency-api.p.rapidapi.com" },
});
return res.json(); // { from, to, rate, date } — "date" is the resolved trading date, not necessarily the one you asked for
}
Two failure modes worth guarding against explicitly, not just the happy path: a future date (there's no "historical" rate for a date that hasn't happened) and a date before your data source's coverage starts (ECB/Frankfurter data begins 1999-01-04 — pre-Euro-era rates aren't there). Both should be a clean 400, not a confusing 502 or a silently wrong 200.
Currency API's new /v1/historical endpoint does exactly this: validates the date up front (rejects future/out-of-range/malformed dates with a 400), resolves weekend/holiday dates to the prior business day, and returns which date it actually used — same "fail honestly, don't fabricate" principle as the rest of the API. Sibling APIs on the same account: Validate and QR API.
Top comments (0)