How to Calculate Real Exchange Rates Programmatically

A developer guide to programmatically fetching historical rates and executing CPI inflation adjustments using Node.js and Python.

Adjusting currency values for inflation is essential for building financial dashboards, corporate accounting tools, and econometric software. Instead of manually downloading CSV files from central banks and mapping them to country CPI records, you can do it in a single API request.

In this tutorial, we will write scripts in JavaScript and Python to query the RealFX API and convert historical currency values into their present-day purchasing power equivalents.

Step 1: Get Your API Key

Before writing code, sign up on RapidAPI to retrieve your authorization key. By default, the free tier allows you to test the API with up to 100 requests.

Step 2: Implementation in JavaScript (Node.js)

Modern Node.js (v18+) supports the native global fetch function. Here is how you can write an asynchronous function to fetch the inflation-adjusted rate:

async function getRealExchangeRate() {
  const apiKey = "your-api-key-here";
  const baseUrl = "https://inflation-fx-api.pages.dev/api/v1/exchange";
  const params = new URLSearchParams({
    from: "USD",
    to: "THB",
    date: "2010-06-15",
    amount: "100"
  });

  try {
    const response = await fetch(`${baseUrl}?${params.toString()}`, {
      method: "GET",
      headers: {
        "x-api-key": apiKey,
        "Accept": "application/json"
      }
    });

    const result = await response.json();
    if (result.success) {
      console.log("Nominal amount:", result.data.nominal.converted_amount);
      console.log("Real value today:", result.data.inflation_adjusted.converted_amount);
      console.log("Explanation:", result.data.inflation_adjusted.explanation);
    } else {
      console.error("API Error:", result.error.message);
    }
  } catch (error) {
    console.error("Network Error:", error);
  }
}

getRealExchangeRate();

Step 3: Implementation in Python

In Python, the requests library is the standard choice for making HTTP requests. Install it using pip install requests if you haven't already:

import requests

def get_real_exchange_rate():
    url = "https://inflation-fx-api.pages.dev/api/v1/exchange"
    headers = {
        "x-api-key": "your-api-key-here",
        "Accept": "application/json"
    }
    params = {
        "from": "USD",
        "to": "THB",
        "date": "2010-06-15",
        "amount": 100
    }

    try:
        response = requests.get(url, headers=headers, params=params)
        response.raise_for_status()
        
        result = response.json()
        if result.get("success"):
            data = result["data"]
            print(f"Base: {data['from']} -> Target: {data['to']}")
            print(f"Nominal Value: {data['nominal']['converted_amount']:.2f}")
            print(f"Adjusted Value: {data['inflation_adjusted']['converted_amount']:.2f}")
            print(f"Explanation: {data['inflation_adjusted']['explanation']}")
        else:
            print("API Error:", result.get("error", {}).get("message"))
            
    except requests.exceptions.RequestException as e:
        print("HTTP Error:", e)

if __name__ == "__main__":
    get_real_exchange_rate()

Tip: The API responds with headers such as X-RateLimit-Remaining and X-RateLimit-Reset. You can inspect these response headers in your code to manage your API request budgeting dynamically.

Ready to Integrate?

Subscribe to the RealFX API on RapidAPI and get access to G10 and Asian regional currencies with 100ms response times.

Subscribe on RapidAPI