Wednesday, March 18, 2009

Basic Java Currency converter using Yahoo Finance API

Yahoo Finance is offering a very nice currency converter. It can show conversion rates for many currencies and even show historical currency information. Since yahoo converter is a web page, we are unable to access directly to the currency conversion rates. Yahoo does not offer a well defined API for accessing its currency information, but luckily, it does supply some basic capability for achieving this task. Yahoo claims that the data supplied is not “bank rates”, but I believe it is pretty much accurate data, that can be used for many applications that doesn’t demand exact “real time” information.

Yahoo API is very simple. The basic general request for getting the current currency rate between two currencies looks like:

http://download.finance.yahoo.com/d/quotes.csv?s=[From Currency][To Currency]=X&f=l1&e=.cs

For example, in order to get the current currency rate between US Dollars and Israeli Shekels, the following request should be constructed:

http://download.finance.yahoo.com/d/quotes.csv?s=USDILS=X&f=l1&e=.cs

As can be noticed, the parameter “s” contains the two currencies for which we would like to know the current rate. “UDS” is the currency symbol of US Dollars and “ILS” is the currency symbol for Israeli Shekels.

As was said before, Yahoo didn’t make a proper API for getting the currency information (like XML). The response for this request is a CSV file containing a single value, which is the currency rate.

Getting the currency rate information is pretty straight forward. I made a small Java class that does the job. It starts with a basic interface to define a general converter behavior:

public interface CurrencyConverter {
    public float convert(String currencyFrom, String currencyTo) throws Exception;
}

And the implementing class with a basic main application showing its usage:

public class YahooCurrencyConverter implements CurrencyConverter {
    public float convert(String currencyFrom, String currencyTo) throws IOException {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet("http://quote.yahoo.com/d/quotes.csv?s=" + currencyFrom + currencyTo + "=X&f=l1&e=.csv");
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httpGet, responseHandler);
        httpclient.getConnectionManager().shutdown();
        return Float.parseFloat(responseBody);
    }

    public static void main(String[] args) {
        YahooCurrencyConverter ycc = new YahooCurrencyConverter();
        try {
            float current = ycc.convert("USD", "ILS");
            System.out.println(current);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Note, that this class is making use of the Apache open source project: Http Client. You will need to put in your project the proper jar files for this class to work properly.

12 comments:

  1. Hi,
    thanks for posting this information.

    My question is, Is this yahoo currency conversion service is free or depending upon hit per day from IP.

    And is google provide similar kind of service ?

    Thanks,

    ReplyDelete
  2. Hi,
    Yahoo service is totally free. I have been taking currency information every 1 minute for a long time with no problem. Maybe in shorter intervals Yahoo will make problems, but it has to be checked. I don't know about currency service from Google, but if you will find some I will be happy to hear about it and post a Class that supports it.

    ReplyDelete
  3. I know this is a very late reply, but i just had to say thanks. this will really help on a project i'm working on.

    btw - i don't think google has a currency converter api available. but i know they have a finance api

    ReplyDelete
  4. can u help me.. where i can download the jar for this code? sory for to late reply..

    ReplyDelete
  5. Check the link at the bottom of the article

    ReplyDelete
  6. how is it possible to obtain historical exchange rate?

    ReplyDelete
  7. Sorry, I don't remember exactly, but I think yes....

    ReplyDelete
  8. it would be so good to know how... but at least it's encouraging... I'll keep searching the web :(
    If you would remember, could you please post it? THX

    ReplyDelete
  9. please specify that exact path to download the jar files
    so that it will helpfull for so many..
    Thank you

    ReplyDelete
  10. Hi Sai, I wrote this post long time ago. I do not have the jar, but the code is very simple and straight forward and short. You just have to copy and paste it.

    ReplyDelete
  11. hi
    i need api that create a table and update currency rates on daily bases in that table from which i will able to fetch required currency rate any time. currently i am using api that converts currency at real time and it is working fine but i need updated currency rates on daily bases.

    thnx,

    ReplyDelete