Skip to main content

Overview

The ExchangeRates class provides functionality to retrieve current foreign exchange rates and convert amounts between different currencies using Flutterwave’s forex API.

Class: ExchangeRates

package com.github.theresasogunle;

public class ExchangeRates

Methods

forex()

Retrieves the exchange rate and converts an amount from one currency to another.
public JSONObject forex()
Returns: JSONObject - Exchange rate information and converted amount Description: This method fetches the current exchange rate between two currencies and calculates the converted amount. It uses Flutterwave’s forex endpoint to get real-time exchange rates.

Parameters Required

Before calling this method, set the following properties:
amount
String
required
The amount to convert
origin_currency
String
required
The source currency code (e.g., “USD”, “NGN”, “GHS”)
destination_currency
String
required
The target currency code to convert to

Example

ExchangeRates exchangeRates = new ExchangeRates();
exchangeRates.setAmount("100")
             .setOrigin_currency("USD")
             .setDestination_currency("NGN");

JSONObject result = exchangeRates.forex();
System.out.println("Exchange rate result: " + result.toString());

Response Fields

The response typically includes:
status
String
Status of the request (success/error)
data
Object
Contains the exchange rate details
data.rate
Number
The exchange rate between the two currencies
data.converted_amount
Number
The converted amount in the destination currency

Setter Methods

The ExchangeRates class uses a fluent interface pattern for easy configuration.

setAmount()

Sets the amount to be converted.
public ExchangeRates setAmount(String amount)
amount
String
required
The amount to convert as a string
Returns: ExchangeRates - Returns the instance for method chaining

setOrigin_currency()

Sets the source currency.
public ExchangeRates setOrigin_currency(String origin_currency)
origin_currency
String
required
The source currency code (e.g., “USD”, “EUR”, “GBP”, “NGN”)
Returns: ExchangeRates - Returns the instance for method chaining

setDestination_currency()

Sets the destination currency.
public ExchangeRates setDestination_currency(String destination_currency)
destination_currency
String
required
The destination currency code
Returns: ExchangeRates - Returns the instance for method chaining

Getter Methods

getAmount()

Retrieves the currently set amount.
public String getAmount()
Returns: String - The amount to convert

getOrigin_currency()

Retrieves the currently set origin currency.
public String getOrigin_currency()
Returns: String - The source currency code

getDestination_currency()

Retrieves the currently set destination currency.
public String getDestination_currency()
Returns: String - The destination currency code
This method requires your secret key to be configured in RaveConstant.SECRET_KEY.
Exchange rates are provided in real-time and may fluctuate. Always fetch fresh rates before processing currency conversions.