JavaScript code examples
The following code examples demonstrate the JavaScript interaction with the API.
All examples also expect your
API key.
Example 1
Get the exchange rate between two currencies
function get(json) {
var rate = json['rate'];
}
var script = document.createElement('script');
script.src = 'http://jsonrates.com/get/?'+
'from=USD'+
'&to=EUR'+
'&callback=get';
document.head.appendChild(script);
Example 2
Convert an amount from one into another currency
function get(json) {
var amount = json['amount'];
}
var script = document.createElement('script');
script.src = 'http://jsonrates.com/convert/?'+
'from=USD'+
'&to=EUR'+
'&amount=2.99'+
'&callback=get';
document.head.appendChild(script);
Example 3
Get historical exchange rates between two currencies for three days
function get(json) {
for (var date in json['rates']) {
var histrate = json['rates'][date]['rate'];
}
}
var script = document.createElement('script');
script.src = 'http://jsonrates.com/historical/?'+
'from=USD'+
'&to=EUR'+
'&dateStart=2014-06-25'+
'&dateEnd=2014-06-27'+
'&callback=get';
document.head.appendChild(script);