Question

I am looking to find a API that I can use in JavaScript or Python to be able to eXchange the currency rates. I need the example of using those APIs too. Do you Guys have any Idea how to find it ? JavaScript or Python portfolio and positions example ?

Was it helpful?

Solution

To retrieve the current exchange rate, you can use the Yahoo finance API (http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s=USDINR=X).

For retrieving the exchange rate of a currency relative to USD for a particular historical date, you can use the Yahoo currency-converter-cache API (http://finance.yahoo.com/connection/currency-converter-cache?date=<YYYYMMDD>).

I made this simple currency converter module that uses Yahoo Finance API and supports historical date. https://github.com/iqbalhusen/currency_converter.

OTHER TIPS

Just try CurrencyFreaks API. It provides the latest currency exchange for 179 currencies worldwide in JSON/XML formats compatible with various programming languages including Javascript and PHP.

Currency conversion by using Javascript:

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function() {
  if(this.readyState === 4) {
    console.log(this.responseText);
  }
});

xhr.open("GET", "https://api.currencyfreaks.com/latest/convert
    ?apikey=YOUR_APIKEY
    &from=USD&to=EUR
    &amount=500");

xhr.send();

The JSON response will be:

{
    "date": "2020-10-16 15:08:00+00",
    "current_rates": {
        "USD": "1.0",
        "EUR": "0.8533"
    },
    "converted_amount": "426.6215",
    "query": {
        "given_amount": "500.0",
        "from": "USD",
        "to": "EUR"
    }
}

Currency conversion by using PHP:

setUrl('https://api.currencyfreaks.com/latest/convert
    ?apikey=YOUR_APIKEY
    &from=USD&to=EUR
    &amount=500');
$request->setMethod(HTTP_Request2::METHOD_GET);
$request->setConfig(array(
  'follow_redirects' => TRUE
));
try {
  $response = $request->send();
  if ($response->getStatus() == 200) {
    echo $response->getBody();
  }
  else {
    echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
    $response->getReasonPhrase();
  }
}
catch(HTTP_Request2_Exception $e) {
  echo 'Error: ' . $e->getMessage();
}

The JSON response will be the same.

I hope it will work for you.

The link of finance.yahoo.com is not working !

One can use : http://free.currencyconverterapi.com/api/v5/convert?q=EUR_USD&compact=y

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top