Money Currencylayer Bank

Gem Version Gem Build Status Code Climate Inline Docs Dependency Status License

A gem that calculates the exchange rate using published rates from

Currencylayer API

~~~ json { “timestamp”: 1441101909, “source”: “USD”, “quotes”: { /* 168 currencies / “USDAUD”: 1.413637, “USDCAD”: 1.316495, “USDCHF”: 0.96355, “USDEUR”: 0.888466, “USDBTC”: 0.004322, / Includes Bitcoin currency! / … } }


See more about Currencylayer product plans on


  • supports 168 currencies
  • includes Bitcoin virtual currency
  • precision of rates up to 6 digits after point
  • uses fast and reliable json api
  • average response time < 20ms
  • supports caching currency rates
  • calculates every pair rate calculating inverse rate or using base currency rate
  • supports multiple server instances, thread safe


Add this line to your application’s Gemfile:

“by gem ‘money-currencylayer-bank’

And then execute:

$ bundle

Or install it yourself as:

$ gem install money-currencylayer-bank


~~~ ruby

Minimal requirements

require ‘money/bank/currencylayer_bank’ mclb = mclb.access_key = ‘your access_key from’

Update rates (get new rates from remote if expired or access rates from cache)


Force update rates from remote and store in cache



Set the base currency for all rates. By default, USD is used.

CurrencylayerBank only allows USD as base currency for the free plan users.

mclb.source = ‘EUR’


Set the seconds after than the current rates are automatically expired

by default, they never expire, in this example 1 day.

mclb.ttl_in_seconds = 86400


Use https to fetch rates from CurrencylayerBank

CurrencylayerBank only allows http as connection for the free plan users.

mclb.secure_connection = true

Define cache (string or pathname)

mclb.cache = ‘path/to/file/cache’

Set money default bank to Currencylayer bank

Money.default_bank = mclb


More methods

~~~ ruby mclb =

Returns the base currency set for all rates.


Expires rates if the expiration time is reached.


Returns true if the expiration time is reached.


Get the API source url.


Get the rates timestamp of the last API request.


Get the rates timestamp of loaded rates in memory.



How to exchange

~~~ ruby

Exchange 1000 cents (10.0 USD) to EUR, ‘USD’).exchange_to(‘EUR’) # => #

Maruku could not parse this XML/HTML: 
<Money fractional:89 currency:EUR>, 'USD').exchange_to('EUR').to_f   # => 8.9

Format, ‘USD’).exchange_to(‘EUR’).format # => €8.90

Get the rate

Money.default_bank.get_rate(‘USD’, ‘CAD’) # => 0.9


See more on

Using gem money-rails

You can also use it in Rails with the gem money-rails.

~~~ ruby require ‘money/bank/currencylayer_bank’

MoneyRails.configure do |config| mclb = mclb.access_key = ‘your access_key from’ mclb.update_rates

config.default_bank = mclb end



You can also provide a Proc as a cache to provide your own caching mechanism perhaps with Redis or just a thread safe Hash (global). For example:

~~~ ruby mclb.cache = do |v| key = ‘money:currencylayer_bank’ if v Thread.current[key] = v else Thread.current[key] end end



The gem fetches all rates in a cache with USD as base currency. It’s possible to compute the rate between any of the currencies by calculating a pair rate using base USD rate.


You can place your own key on a file or environment variable named TEST_ACCESS_KEY and then run:

~~~ bundle exec rake



Other Implementations



  1. Fork it ([your-username]/money-currencylayer-bank/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request


The MIT License

Copyright (c) 2017 Phlegx Systems OG