12

Please advice API for currency converting which returns JSON or small size html. I use http://www.google.com/finance/converter?a=1&from=RUB&to=USD that returns HTML of 11 kb. I use it in my iOS app.

Thanks in advance!

kastet
  • 209
  • 1
  • 3
  • 6
  • possible duplicate of [How do I get currency exchange rates via an API such as Google Finance?](http://stackoverflow.com/questions/3139879/how-do-i-get-currency-exchange-rates-via-an-api-such-as-google-finance) – TRiG Jan 07 '15 at 02:16
  • See 5 APIs Comparison in [Free and Paid Currency Converter API Comparison](http://www.freecurrencyconverterapi.com) – Michael Freidgeim Jun 21 '16 at 05:04
  • https://fincharts.info/charts/exchange/?key=key&&from=USD&to=GBP,BWP,ZAR&amount=45854&date=2018-03-03&base=USD – Tino Costa 'El Nino' Jul 05 '18 at 17:54

8 Answers8

19

free.currencyconverterapi.com returns results in JSON format.

The web service also supports JSONP. The API is very easy to use, and it lets you convert one currency to another.

Disclaimer, I'm the author of the website.

A sample conversion URL is: http://free.currencyconverterapi.com/api/v6/convert?q=USD_PHP&compact=ultra&apiKey=sample-api-key which will return a value in json format, e.g. {"USD_PHP":51.459999}

Manny
  • 6,277
  • 3
  • 31
  • 45
12

As mentioned in the comments this service was shut down in Nov 2013.

Googles calulator API can do this;

Request:

http://www.google.com/ig/calculator?hl=en&q=100EUR=?USD

Response:

{lhs: "100 Euros",rhs: "145.67 U.S. dollars",error: "",icc: true}

(More info)

Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • I tried this one but it does not fully match with ISO currencies standard. For example Russian rubles are "RUB" by ISO but "RUR" for that system. So that makes it worthless. – kastet Apr 25 '11 at 15:40
  • Are there others? RUB & RUR seem interchangable on that system probably because RUR was the old ISO identifier – Alex K. Apr 25 '11 at 16:38
  • 5
    iGoogle is turned off, so now you can try yahoo http://bit.ly/1cPOH2F :) – Itsmeromka Nov 06 '13 at 08:02
  • @Manny hey, I need free api for conversion for all the currencies based on historical data! This looks that only has the latest currencies. can u suggest me something? – pm1359 Mar 29 '17 at 09:49
  • @ Nerfair This is perfect, dou know how to query based on date for the same query `bit.ly/1cPOH2F` – pm1359 Mar 29 '17 at 09:54
  • https://fincharts.info/charts/exchange/?key=api_key&&from=USD&to=GBP,BWP,ZAR&amount=45854&date=2018-03-03&base=USD – Tino Costa 'El Nino' Jul 05 '18 at 17:55
  • http://www.google.com/ig/calculator and http://query.yahooapis.com/v1/ both are not available. – Jayprakash Dubey Oct 07 '20 at 10:45
11

Yahoo is no longer working. See comment below

Yahoo Finance Currency Converter.

This url format could be used to fetch conversion rates in different formats.

http://download.finance.yahoo.com/d/quotes.csv?s=AUDUSD=X&f=nl1d1t1

Substitute quotes.csv with appropriate format and parameters with the required codes

EDIT: Added Example Url formats

Jan Derk
  • 2,552
  • 27
  • 22
suhair
  • 10,895
  • 11
  • 52
  • 63
  • Seems like it what I was looking for. Thank you! – kastet Apr 25 '11 at 15:43
  • 1
    This call and the https call appear to be shut down with response, "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." – Jacksonsox Nov 03 '17 at 01:54
10

Now iGoogle has been killed off, Alex K's solution no longer works sadly. In php, this is an alternative which works in the same way and is just as effective:

$amount = urlencode($amount);
$from_Currency = urlencode($from_Currency);
$to_Currency = urlencode($to_Currency);
$get = file_get_contents("https://www.google.com/finance/converter?a=$amount&from=$from_Currency&to=$to_Currency");
$get = explode("<span class=bld>",$get);
$get = explode("</span>",$get[1]);  
$converted_amount = preg_replace("/[^0-9\.]/", null, $get[0]);
hobailey
  • 861
  • 1
  • 12
  • 22
9

UPDATE: Yahoo API is not working anymore. Leaving this legacy answer just to provide information that this doesn't work anymore.


use yahoo api:

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDLTL%22)&format=json&env=store://datatables.org/alltableswithkeys&callback=

it will return json format like:

{
  query: {
  count: 1,
  created: "2013-12-04T13:52:53Z",
  lang: "en-US",
  results: {
    rate: {
        id: "USDLTL",
        Name: "USD to LTL",
        Rate: "2.5485",
        Date: "12/4/2013",
        Time: "8:52am",
        Ask: "2.5486",
        Bid: "2.5485"
      }
    }
  }
}

Check out in the URL there is USDLTL now, so just change to what you need.

Also sometime the rate is so low, that you don't see it even with 4 numbers it shows:

Rate: 0.0006

Do not panic just make a reversal query, flip your currencies and make some simple math.

e.g. you got that the rate is from KRW to EUR 0.0006 but the real rate is something like 0.00000125 so ask API again, just flip the currencies: what is the ratio from EUR to USD. then you will get huge number like 12500000.xxx so make math to get the ratio you need: 1/12500000 and you will get ratio = 0.00000125

Hope that helps ;)

P.S. decoded URL which is easier to read looks like this:

http://query.yahooapis.com/v1/public/yql
?q=select * from yahoo.finance.xchange where pair in ("USDLTL")
&format=json
&env=store://datatables.org/alltableswithkeys
&callback=
Lukas Liesis
  • 24,652
  • 10
  • 111
  • 109
6

I use a php-class to convert currency rates:

/**
 * Yahoo currency rate import class
 *
 * @author     Felix Geenen (http://www.geenen-it-systeme.de)
 * @version    1.0.3
 */
class Yahoofinance {
    public static $_url = 'http://download.finance.yahoo.com/d/quotes.csv?s={{CURRENCY_FROM}}{{CURRENCY_TO}}=X&f=l1&e=.csv';
    public static $_messages = array();
 
    /*
     * converts currency rates
     *
     * use ISO-4217 currency-codes like EUR and USD (http://en.wikipedia.org/wiki/ISO_4217)
     *
     * @param currencyFrom String base-currency
     * @param currencyTo String currency that currencyFrom should be converted to
     * @param retry int change it to 1 if you dont want the method to retry receiving data on errors
     */
    public static function _convert($currencyFrom, $currencyTo, $retry=0)
    {
        $url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, self::$_url);
        $url = str_replace('{{CURRENCY_TO}}', $currencyTo, $url);
 
        try {
            $handle = fopen($url, "r");
 
            if($handle !== false) {
                $exchange_rate = fread($handle, 2000);
     
                # there may be spaces or breaks
                $exchange_rate = trim($exchange_rate);
                $exchange_rate = (float) $exchange_rate;
     
                fclose($handle);
     
                if( !$exchange_rate ) {
                    echo 'Cannot retrieve rate from Yahoofinance';
                    return false;
                }
                return (float) $exchange_rate * 1.0; // change 1.0 to influence rate;
            }
        }
        catch (Exception $e) {
            if( $retry == 0 ) {
                # retry receiving data
                self::_convert($currencyFrom, $currencyTo, 1);
            } else {
                echo 'Cannot retrieve rate from Yahoofinance';
                return false;
            }
        }
    }
}
Felix Geenen
  • 2,465
  • 1
  • 28
  • 37
  • Thanks, I was using igoogle like many others and got screwed by the service going down. This answer is the only solution I could find so far that returns exactly what you want without the other unnecessary junk. – Dieter Gribnitz Aug 29 '14 at 07:22
5

Here is a simple adaptation of Felix Geenen's answer to use curl instead of fopen since a lot of servers have fopen turned off by default.

( I cleaned up some code and added a decrement value to retry. )

( Also remember to update the retry self reference depending on the scope you drop the function in to eg. static:: or $this-> )

function convert($from, $to, $retry = 0)
{
    $ch = curl_init("http://download.finance.yahoo.com/d/quotes.csv?s=$from$to=X&f=l1&e=.csv");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_NOBODY, false);
    $rate = curl_exec($ch);
    curl_close($ch);
    if ($rate) {
        return (float)$rate;
    } elseif ($retry > 0) {
        return convert($from, $to, --$retry);
    }
    return false;
}
Dieter Gribnitz
  • 5,062
  • 2
  • 41
  • 38
4

I was using iGoogle until it just went belly up, serves me right.

Thanks to Nerfair tho in his comment in response to hobailey's comment above, this works AWESOME. I thought I would post it here so you can fully see how it works!

http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.xchange where pair in ("USDCNY")&format=json&env=store://datatables.org/alltableswithkeys&callback=

Here is the link url encoded: http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20%28%22USDCNY%22%29&format=json&env=store://datatables.org/alltableswithkeys&callback=

Super nice, just change the currency pair. Thanks Nerfair!

c0d3p03t
  • 1,675
  • 2
  • 16
  • 14