Code Example: Get latest rates

Last updated 5 months ago

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.

Node.JS
PHP
Go
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);
});
});
$apiURL = "https://api.currex.info/v1/json/latest/EUR/";
$jsonApiData = file_get_contents($apiURL);
//Decode the the json to php object
$apiObj = json_decode($jsonApiData);
//dataset timestamp
printf(date('c',$apiObj->timestamp).'<br>');
//Iterate trough object
foreach($apiObj->rates as &$rate){
printf("[".date('c',$rate->timestamp)."]: 1 EUR = ".$rate->rate." ".$rate->currency."<br>");
}
package main
import (
"encoding/xml"
"net/http"
"encoding/json"
"fmt"
"io/ioutil"
)
type CurrencyRate struct{
Currency string `json:"currency" xml:"currency,attr"`
Rate float64 `json:"rate" xml:"rate,attr"`
}
type LatestRatesResponse struct{
XMLName xml.Name `xml:"latest_rates" json:"-"`
Timestamp string `json:"timestamp_utc" xml:"timestamp_utc,attr"`
BaseCurrency string `xml:"base_currency,attr" json:"base_currency"`
Rates map[string]CurrencyRate `json:"rates" xml:"rates"`
}
func main() {
var latestRates LatestRatesResponse
//Get api data
resp, err := http.Get("https://api.currex.info/v1/json/latest/EUR")
if err != nil{
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil{
panic(err)
}
err = json.Unmarshal(body,&latestRates)
if err != nil{
panic(err)
}
for k,v := range latestRates.Rates{
fmt.Printf("1 %s = %f %s\n",latestRates.BaseCurrency,v.Rate,k);
}
}