Java

Java Exchange Rate API

Fetch live and historical exchange rates in Java. Use HttpClient with Gson to build a currency converter in minutes.

Quick Start

Add Gson to your project and fetch your first exchange rate with Java's built-in HttpClient.

Maven Dependency

<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.11.0</version>
</dependency>

Gradle Dependency

implementation 'com.google.code.gson:gson:2.11.0'
import java.net.http.*;
import java.net.URI;

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create("https://exchange-rateapi.com/api/v1/rates?source=USD&target=EUR"))
    .header("Authorization", "Bearer era_live_YOUR_API_KEY")
    .build();

HttpResponse<String> response = client.send(request,
    HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
// Output: [{"rate":0.9215,"source":"USD","target":"EUR","time":"2026-05-15T12:00:00Z"}]
Get your free API keySign up here to get an API key that starts with era_live_. The free plan includes 300 requests per month.

Complete Java Example with Gson

Here is a full example that fetches exchange rates and parses the JSON response into Java objects using Gson.

import java.net.http.*;
import java.net.URI;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import java.util.List;

public class ExchangeRateClient {

    record Rate(double rate, String source, String target, String time) {}

    private static final String API_KEY = "era_live_YOUR_API_KEY";
    private static final String BASE_URL = "https://exchange-rateapi.com/api/v1/rates";
    private static final HttpClient client = HttpClient.newHttpClient();
    private static final Gson gson = new Gson();

    public static List<Rate> getRates(String source, String target) throws Exception {
        HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create(BASE_URL + "?source=" + source + "&target=" + target))
            .header("Authorization", "Bearer " + API_KEY)
            .build();

        HttpResponse<String> response = client.send(request,
            HttpResponse.BodyHandlers.ofString());

        if (response.statusCode() == 401) {
            throw new RuntimeException("Invalid API key");
        }
        if (response.statusCode() == 429) {
            throw new RuntimeException("Rate limit exceeded");
        }
        if (response.statusCode() != 200) {
            throw new RuntimeException("HTTP " + response.statusCode());
        }

        return gson.fromJson(response.body(),
            new TypeToken<List<Rate>>(){}.getType());
    }

    public static void main(String[] args) throws Exception {
        // Single pair
        List<Rate> rates = getRates("USD", "EUR");
        System.out.printf("1 USD = %.4f EUR%n", rates.get(0).rate());

        // Multiple targets
        List<Rate> multi = getRates("USD", "EUR,GBP,JPY");
        for (Rate r : multi) {
            System.out.printf("1 USD = %.4f %s%n", r.rate(), r.target());
        }
    }
}

Async Requests

// Use sendAsync for non-blocking calls
HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create("https://exchange-rateapi.com/api/v1/rates?source=USD&target=EUR"))
    .header("Authorization", "Bearer era_live_YOUR_API_KEY")
    .build();

client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
    .thenApply(HttpResponse::body)
    .thenAccept(body -> {
        List<Rate> rates = gson.fromJson(body,
            new TypeToken<List<Rate>>(){}.getType());
        System.out.printf("1 USD = %.4f EUR%n", rates.get(0).rate());
    })
    .join();

Without HttpClient (Legacy HttpURLConnection)

For Java 8 or older versions without the java.net.http module, use HttpURLConnection.

import java.net.*;
import java.io.*;

URL url = new URL("https://exchange-rateapi.com/api/v1/rates?source=USD&target=EUR");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", "Bearer era_live_YOUR_API_KEY");

BufferedReader reader = new BufferedReader(
    new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
    sb.append(line);
}
reader.close();

System.out.println(sb.toString());
// Parse with Gson as shown above

Response Format

Single Target Response

[
  {
    "rate": 0.9215,
    "source": "USD",
    "target": "EUR",
    "time": "2026-05-15T12:00:00Z"
  }
]

Multiple Targets Response

[
  { "rate": 0.9215, "source": "USD", "target": "EUR", "time": "2026-05-15T12:00:00Z" },
  { "rate": 0.7891, "source": "USD", "target": "GBP", "time": "2026-05-15T12:00:00Z" },
  { "rate": 151.42, "source": "USD", "target": "JPY", "time": "2026-05-15T12:00:00Z" }
]
Java 11+ recommended — The examples above use java.net.http.HttpClient introduced in Java 11. For Java 8, use the HttpURLConnection example or a library like OkHttp.

Start building with Java

Get your free API key and start fetching exchange rates in Java in under a minute.

Get Free API Key →