Skip to content

Instantly share code, notes, and snippets.

@henrik
Created December 28, 2009 22:48
Show Gist options
  • Save henrik/265014 to your computer and use it in GitHub Desktop.
Save henrik/265014 to your computer and use it in GitHub Desktop.
JavaScript to get currency exchange rates from Yahoo Finance as JSONP. No XHR!
Get exchange rate as JSONP via YQL.
YQL Console: http://developer.yahoo.com/yql/console
Query (USD to SEK): select rate,name from csv where url='http://download.finance.yahoo.com/d/quotes?s=USDSEK%3DX&f=l1n' and columns='rate,name'
Example code:
<script type="text/javascript">
function getRate(from, to) {
var script = document.createElement('script');
script.setAttribute('src', "http://query.yahooapis.com/v1/public/yql?q=select%20rate%2Cname%20from%20csv%20where%20url%3D'http%3A%2F%2Fdownload.finance.yahoo.com%2Fd%2Fquotes%3Fs%3D"+from+to+"%253DX%26f%3Dl1n'%20and%20columns%3D'rate%2Cname'&format=json&callback=parseExchangeRate");
document.body.appendChild(script);
}
function parseExchangeRate(data) {
var name = data.query.results.row.name;
var rate = parseFloat(data.query.results.row.rate, 10);
alert("Exchange rate " + name + " is " + rate);
}
getRate("SEK", "USD");
getRate("USD", "SEK");
</script>
@henrik
Copy link
Author

henrik commented Feb 7, 2018

@kasimvali786 Yahoo has discontinued this API I'm afraid. See the comment before yours.

@kasimvali786
Copy link

Hi Henrik,

Yes, I noticed that before comment, do you have any idea is there any link API URL to get the daily exchange rates.

Thanks in advanced.

@sheff3rd
Copy link

@kasimvali786
You can use this one

`https://finance.google.com/finance/converter?a=${amout}&from=${from}&to=${to}`

@leovarmak
Copy link

@sheff3rd

This might be too much to ask but can you post an example to convert USD to INR using the above URL ?

@gkucmierz
Copy link

gkucmierz commented Jan 19, 2023

Looks like both APIs are not working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment