Announcing `exchangerates`

This post announces the new exchangerates library. It allows you to fetch exchange rates from the https://exchangeratesapi.io/ website. Formerly https://fixer.io.

Free currency exchange rate API's are surprisingly hard to find. https://exchangeratesapi.io/ is the only one that I found, so I decided to write a little Haskell client to call the API. The API is free to use, and the author asks to cache the results as much as possible, so the Haskell client automatically caches all results as much as possible.

The library is laid out as follows:

To call the https://exchangeratesapi.io/ APi in the simplest way possible, we need to specificy a cache file, and our query. The following is a minimal example that gets the exchange rates for 2018-01-19 and prints them to standard output:

main :: IO
main = do
    rates <-
        autoRunExchangeRatesClient $
          withFileCache "/tmp/exchangerates.cache" $
            getAtDate (fromGregorian 2018 01 19) (Just EUR) Nothing
    case rates of
        Left err -> die $ show err
        Right v -> print v

This little piece of code will call the live API and save the cache with results to /tmp/exchangerates.cache. The next time this piece of code is run, it will read /tmp/exchangerates.cache and notice that it does not need to call the live API anymore, and just print the results.

If you liked this blog post, please consider becoming a supporter:

Become A Supporter