Code Example: Get latest rates

Get all the rates with base currency Euro. Every other base is also possible. For getting even more accurate rates, attach your premium token to the request url

Please note that these are basic examples not targeted for production usage.

const request = require('request');

//Make request
request('https://api.currex.info/json/latest/EUR', function (error, response, body) {
    //Error catching
    if (error){
        console.error(error);
        return;
    }

    //Parse JSON data
    let responseObj = JSON.parse(body);

    //Dataset timestamp
    //1000 because JavaScript uses milliseconds for date - unix timestamp is only seconds
    let dataTimestamp = new Date(responseObj.timestamp*1000);
    console.info("Data Timestamp: "+ dataTimestamp);

    //Iterate over rates and print them
    //foreach-loop for objects - kind of ;)
    Object.keys(responseObj.rates).forEach(function(key) {
        let singleRate = responseObj.rates[key];

        //1000 because JavaScript uses milliseconds for date - unix timestamp is only seconds
        let singleRateTimestamp = new Date(singleRate.timestamp*1000);
        let singleRateRate = singleRate.rate;
        let singleRateCurrencyCode = singleRate.currency;

        console.log("["+singleRateTimestamp+"]: 1 EUR = "+singleRateRate+" "+singleRateCurrencyCode);
    });


});

Last updated