How to Get the Exchange Rate from a Source Currency into a Destination Currency using Java

Our powerful Currency Exchange API allows you to instantly list common currencies, convert prices between currencies, and, finally, list exchange rates between currencies. In this article, I’ll demonstrate how you can use this third & final iteration of our Currency Exchange API, which leverages the latest available data to supply up-to-date exchange rates for your financial applications. As always, it’s completely free to use — you’ll just need to register a free account on our website (which comes with 800 API calls per month & zero additional commitments) and copy from the Java code examples provided below to structure an API call in your environment.

Our first step is to install the Currency API client. We can do so by first adding a reference to the repository in pom.xml:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

And we can wrap up our installation step by adding a reference to the pom.xml dependency:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

Now we can go about calling the function. First, let’s include the imports at the top of our file:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.CurrencyExchangeApi;

Then, let’s make sure we have our parameters ready to go. We need to input both a source & destination currency code (three-digit codes like USD, EUR, JPY, etc.) and then supply our API key in the field indicated by the code comments:

ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
CurrencyExchangeApi apiInstance = new CurrencyExchangeApi();
String source = "source_example"; // String | Source currency three-digit code (ISO 4217), e.g. USD, EUR, etc.
String destination = "destination_example"; // String | Destination currency three-digit code (ISO 4217), e.g. USD, EUR, etc.
try {
ExchangeRateResult result = apiInstance.currencyExchangeGetExchangeRate(source, destination);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling CurrencyExchangeApi#currencyExchangeGetExchangeRate");
e.printStackTrace();
}

After that, you’re good to go. If you want to test this API before running it in your environment, just visit our API Console page and click “try it out” under the Exchange Rate API dropdown.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cloudmersive

Cloudmersive

436 Followers

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.