Advanced Queries: Filtering, Inversion & Time-Specific FX Rates in Currencies API

Getting the latest exchange rate for a single currency pair is a common starting point, but many financial applications require more specific data. For example, a currency converter needs rates for many currencies at once, and a portfolio valuation tool might need the exchange rate from a specific historical moment.

This guide presents methods for making these kinds of advanced queries with the FinFeedAPI Currencies API. You will see how to filter for multiple currencies, get inverted rates, and retrieve the exchange rate at a precise point in time.

This guide covers:

  • Retrieving rates for one base currency against multiple quote currencies.

  • Getting the inverted rate for a currency pair (e.g., USD/EUR instead of EUR/USD).

  • Fetching the exchange rate at a specific historical date and time.

What you need:

  • Python 3.x.

  • The api-bricks-fx-api-rest library.

  • Your personal FinFeedAPI key.

1. Environment Setup

First, you need to install the FinFeedAPI client library if it is not already on your system.

pip install api-bricks-fx-api-rest

Next, prepare your Python script by importing the necessary libraries and configuring the API client with your key.

# Import libraries
from datetime import datetime
import api_bricks_fx_api_rest

# --- API Configuration ---
# IMPORTANT: Replace "YOUR_API_KEY_HERE" with your actual key.
API_KEY = "YOUR_API_KEY_HERE"
api_client_config = api_bricks_fx_api_rest.Configuration()
api_client_config.api_key['Authorization'] = API_KEY
api_client = api_bricks_fx_api_rest.ApiClient(configuration=api_client_config)

# Initialize the ExchangeRatesApi
exchange_rates_api = api_bricks_fx_api_rest.ExchangeRatesApi(api_client)

2. Filtering for Multiple Currencies

To build a tool like a currency converter, you often need to get the exchange rates for one base currency against a list of other currencies. Instead of making separate API calls for each pair, you can make a single request to the /v1/exchangerate/{base} endpoint and supply a list of quote currencies.

This example shows how to get the latest rates for the Euro (EUR) against the US Dollar (USD), British Pound (GBP), and Japanese Yen (JPY) in one call.

BASE_CURRENCY = "EUR"
QUOTE_CURRENCIES = ["USD", "GBP", "JPY"]

print(f"Fetching latest rates for {BASE_CURRENCY} against {', '.join(QUOTE_CURRENCIES)}...")

try:
    # The 'quote' parameter can accept a list of currencies
    filtered_rates = exchange_rates_api.v1_exchangerate_base_get(
        base=BASE_CURRENCY,
        quote=QUOTE_CURRENCIES
    )

    if filtered_rates:
        print("Successfully fetched rates:")
        for rate in filtered_rates:
            print(f"- 1 {rate.base} = {rate.rate} {rate.quote} (at {rate.time})")

except api_bricks_fx_api_rest.ApiException as e:
    print(f"API Exception occurred: {e}")

This method is more efficient than making individual requests for EUR/USD, EUR/GBP, and EUR/JPY.

3. Inverting Exchange Rates

By default, an API will return a rate in the format you request it (e.g., EUR/USD). Sometimes, your application may need the inverted pair (USD/EUR). The /v1/exchangerate/{base}/{quote} endpoint includes an invert parameter for this purpose.

This example gets both the direct and inverted rates for EUR/USD.

BASE_CURRENCY = "EUR"
QUOTE_CURRENCY = "USD"

print(f"\nFetching direct and inverted rates for {BASE_CURRENCY}/{QUOTE_CURRENCY}...")

try:
    # Get the direct rate (default behavior)
    direct_rate = exchange_rates_api.v1_exchangerate_base_quote_get(
        base=BASE_CURRENCY,
        quote=QUOTE_CURRENCY
    )

    # Get the inverted rate by setting invert=True
    inverted_rate = exchange_rates_api.v1_exchangerate_base_quote_get(
        base=BASE_CURRENCY,
        quote=QUOTE_CURRENCY,
        invert=True
    )

    if direct_rate and inverted_rate:
        print(f"Direct rate:   1 {direct_rate.base} = {direct_rate.rate} {direct_rate.quote}")
        print(f"Inverted rate: 1 {inverted_rate.base} = {inverted_rate.rate} {inverted_rate.quote}")

except api_bricks_fx_api_rest.ApiException as e:
    print(f"API Exception occurred: {e}")

The API handles the calculation for you, which is useful for displaying rates in different conventions.

4. Getting Rates at a Specific Time

For historical analysis or valuing assets on a specific date, you need the exchange rate at a particular moment in the past. The /v1/exchangerate/{base}/{quote} endpoint accepts a time parameter for this. The timestamp should be in ISO 8601 format.

This example retrieves the EUR/USD exchange rate from a specific time on January 15, 2024.

BASE_CURRENCY = "EUR"
QUOTE_CURRENCY = "USD"
TARGET_TIME = "2024-01-15T10:00:00"

print(f"\nFetching {BASE_CURRENCY}/{QUOTE_CURRENCY} rate at {TARGET_TIME}...")

try:
    # Use the 'time' parameter to specify a historical point
    time_specific_rate = exchange_rates_api.v1_exchangerate_base_quote_get(
        base=BASE_CURRENCY,
        quote=QUOTE_CURRENCY,
        time=TARGET_TIME
    )

    if time_specific_rate:
        print(f"Rate at {time_specific_rate.time}: 1 {time_specific_rate.base} = {time_specific_rate.rate} {time_specific_rate.quote}")

except api_bricks_fx_api_rest.ApiException as e:
    print(f"API Exception occurred: {e}")

This gives you the exchange rate as it was at that exact time, not just the daily closing price.

Final Thoughts

These advanced query methods allow for more specific and useful data retrieval from a currency API. By filtering for multiple currencies, inverting pairs, and requesting rates at exact times, you can build more detailed and functional financial applications. These techniques can also be combined; for example, you could request inverted rates for a list of currencies at a specific historical time.

0
Subscribe to my newsletter

Read articles from Maciej Józefowicz directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Maciej Józefowicz
Maciej Józefowicz