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>
@emilandi
Copy link

emilandi commented Jan 9, 2015

Excelent! works :)

@m2pathan
Copy link

Excelent

@tomymacmillan
Copy link

Hi! Thanks a lot for the code, works perfectly. One question, how I can store each request as a variable to use it later? For example if i do getRate("SEK", "USD");, how I can store that rate so I can multiply it and get the currency conversion?

@vinayak519
Copy link

This always converts any currency with default amount.

1 is default where in code amount is given 10 to be converted.

@Harrymon12
Copy link

I had use this. It works great for me.
Exchange Rates in Excel

@ajikanfan
Copy link

Wonderful, thank you for your great work

@av-k
Copy link

av-k commented Nov 3, 2017

For example, request: 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%3DEURUSD%253DX%26f%3Dl1n'%20and%20columns%3D'rate%2Cname'&format=json

Doesn't return results, it's fail.

{"query":{"count":0,"created":"2017-11-03T04:01:34Z","lang":"ru-RU","results":null}}

Thanks Yahoo.
http://download.finance.yahoo.com/
It has come to our attention that this service is being used in violation of the Yahoo Terms of Service. As such, the service is being discontinued. For all future markets and equities data research, please refer to finance.yahoo.com.

Administratin' 'Hoo Nixon Administratin' 'Hoo
Re: Is Yahoo! Finance API broken?
Hi All - I'm blocking new replies to this thread since it refers to an older outage.
The new download issue which began 11/01/17 and returning an error 999 for most users is currently being investigated and we hope to have it resolved soon.

More info at Yahoo forum

@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