Exchange Rate API

Java Currency API



Our service is the easy to use, accurate and reliable currency API for Java that's perfect for both personal and professional use. Easily convert currencies in your Java project!

How to use our Java Currency Converter

Converting currencies using our PHP Exchange Rate API is as easy as posting a HTTP request. Your requests would need to follow this simple pattern:

http://www.exchangerate-api.com/FROM_CURRENCY/TO_CURRENCY/AMOUNT?k=YOUR_API_KEY


Here are some examples in Java:

1. Simple Currency Conversion (in Java):

To convert from 12.50 USD (US Dollar) to GBP (British Pound):

try {
URL convert = new URL("http://www.exchangerate-api.com/usd/gbp/12.50?k=API_KEY");
BufferedReader in = new BufferedReader(new InputStreamReader(convert.openStream()));
String answer = in.readLine();
in.close();
}
catch (MalformedURLException mue) {
System.exit(1);
}
catch (IOException ioe) {
System.exit(1);
}

Or to get how many Japanese Yen (JPY) there are to the South African Rand (ZAR):

try {
URL convert = new URL("http://www.exchangerate-api.com/zar/jpy?k=API_KEY");
BufferedReader in = new BufferedReader(new InputStreamReader(convert.openStream()));
String answer = in.readLine();
in.close();
}
catch (MalformedURLException mue) {
System.exit(1);
}
catch (IOException ioe) {
System.exit(1);
}

Error Codes:

The API returns the following error codes:
  -1 if an invalid amount is used (must be numeric floating point or integer numbers)
  -2 if either of the 3 letter currency codes are invalid (Check our supported currencies)
  -3 if an invalid key is used (Get an API key)
  -4 if your monthly API query limit is reached (Click here for extended usage)
  -5 if an unresolvable IP address is provided (This only applies for our "Automatic Country/Flag Information" query.)



We also offer two additional types of query:




2. Automatic Currency Detection/Conversion (in Java):

To convert from 12.50 USD (US Dollar) to your user's currency:

String ip = request.getRemoteAddr();

String result = "";
try {
URL con = new URL("http://www.exchangerate-api.com/auto/"+ip+"/12.50?k=API_KEY");
BufferedReader in = new BufferedReader(new InputStreamReader(con.openStream()));
result = in.readLine();
in.close();
}
catch (MalformedURLException mue) {
System.exit(1);
}
catch (IOException ioe) {
System.exit(1);
}

if(result != "") {
String[] result_array = result.split("|");
Float converted_amount = array[0]; //FINAL CONVERTED AMOUNT
String symbol_unicode = array[1]; //FINAL DECIMAL UNICODE FOR SYMBOL
String currency_code = array[2]; //FINAL 3-DIGIT CURRENCY CODE (EG: USD)

//If you want to display the currency's symbol in HTML:
String symbol_html = "";
String[] symbol_characters = symbol_html.split(",");
for(int i=0; i < symbol_characters.length; i++) {
    symbol_html = symbol_html+"&#"+symbol_characters[i]+";";
    }
}

Free 14-Day Trial

Sign Up Now!

Get started right away!



3. Automatic Country/Flag Information (in Java):

To retrieve your user's national flag and other country information:

String ip = request.getRemoteAddr();

String result = "";
try {
URL con = new URL("http://www.exchangerate-api.com/country_info/"+ip+"?k=API_KEY");
BufferedReader in = new BufferedReader(new InputStreamReader(con.openStream()));
result = in.readLine();
in.close();
}
catch (MalformedURLException mue) {
System.exit(1);
}
catch (IOException ioe) {
System.exit(1);
}

if(result != "") {
String[] result_array = result.split("|"); String country_name = result_array[0]; //FINAL COUNTRY NAME
String country_code_2 = result_array[1]; //FINAL 2-DIGIT COUNTRY CODE (EG: US)
String country_code_3 = result_array[2]; //FINAL 3-DIGIT COUNTRY CODE (EG: USA)
String currency_code = result_array[3]; //FINAL 3-DIGIT CURRENCY CODE (EG: USD)
String symbol_unicode = result_array[4]; //FINAL DECIMAL UNICODE FOR SYMBOL
String flag_url = result_array[5]; //FINAL URL FOR FLAG IMAGE

//If you want to display the currency's symbol in HTML:
String symbol_html = "";
String[] symbol_characters = symbol_html.split(",");
for(int i=0; i < symbol_characters.length; i++) {
    symbol_html = symbol_html+"&#"+symbol_characters[i]+";";
    }

//If you want to display an image of the flag in HTML:
String flag_image = "<img src='"+flag_url+"' />";
}

Free 14-Day Trial

Sign Up Now!

Get started right away!