Exchange Rates and Currency Conversion with fixer.io

By (Sponsor)  on  

My most stressful tasks as a coder have always revolved around money.  Whether you're coding a site for a client, your own business, or your employer, you want to be damn sure that any handling of currency is accurate, secure, and localized.  We always want our code to work but when someone's livelihood or employment is on the line, another level of importance is realized.

After using fixer.io, I'm convinced I've found the best solution.  fixer.io provides currency conversion and exchange rates for present and historical time, ensuring accurate data at checkout as well as refunds in the case that currency has changed over time.

Quick Hits

  • fixer.io lets you sign up for free
  • fixer.io is trusted by Microsoft, Kraken, Samsung, and instacart
  • fixer.io is a simple, easy to use API
  • fixer.io provides accurate current and historical currency data
  • fixer.io also provides Bitcoin (BTC) information
  • fixer.io is from apilayer, the same service provider for currencylayereversign, and streetlayer

Using fixer.io

fixer.io is another apilayer masterclass in creating an API that's easy to use and very focused.  The easiest API call is as follows:

# Get currency rate based on EUR
curl http://data.fixer.io/api/latest?access_key=API_KEY&base=EUR
{  
   "success":true,
   "timestamp":1521677363,
   "base":"EUR",
   "date":"2018-03-22",
   "rates":{  
      "AED":4.538449,
      "AFN":85.146208,
      // ...
      "BTC":0.000139,
      // ...
      "USD":1.235794
   }
}

With this call you'll get currency conversions with a given base currency, in this case the US Dollar.

Now consider the case of a refund, especially a refund to a currency like Bitcoin that fluctuates quickly.  If the currency sharply increases, it's possible that customers may want to request a refund simply due to the value change.  You can protect yourself or your client by using fixer.io's state_date and end_date parameters to get a currency value change over a given date:

curl http://data.fixer.io/api/timeseries
    ? access_key = API_KEY
    & start_date = 2012-05-01
    & end_date = 2012-05-25
{
    "success": true,
    "timeseries": true,
    "start_date": "2012-05-01",
    "end_date": "2012-05-03",
    "base": "EUR",
    "rates": {
        "2012-05-01":{
          "USD": 1.322891,
          "AUD": 1.278047,
          "CAD": 1.302303
        },
        "2012-05-02": {
          "USD": 1.315066,
          "AUD": 1.274202,
          "CAD": 1.299083
        },
        "2012-05-03": {
          "USD": 1.314491,
          "AUD": 1.280135,
          "CAD": 1.296868
        },
        [...]
    }
}

Retrieving that value lets you refund to the given fiat currency without losing value based on the time of transaction!

apilayer always allows you to use JSONP with their APIs, and fixer.io is no exception:

// set endpoint and your access key
endpoint = 'latest'
access_key = 'API_KEY';

// get the most recent exchange rates via the "latest" endpoint:
$.ajax({
    url: 'http://data.fixer.io/api/' + endpoint + '?access_key=' + access_key,   
    dataType: 'jsonp',
    success: function(json) {

        // exchange rata data is stored in json.rates
        alert(json.rates.GBP);
        
        // base currency is stored in json.base
        alert(json.base);
        
        // timestamp can be accessed in json.timestamp
        alert(json.timestamp);
        
    }
});

So many APIs don't support JSONP, making you create your own proxies -- thankfully not fixer.io!

Conclusion

fixer.io is exactly what an API should be:  easy to use, no hassle, and reliable;  I enjoy using and reviewing apilayer's APIs for just that reason.  When the data really matters, especially in the case of money, use an API you can trust -- fixer.io!

Discussion

    Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!