Skip to content

Hit ECB data API

ECB data portal is a wealth of macro data. Use cases include but not limited to scenario analysis, macro forecasting, and macro stress testing.

The portal has a data API which allows users to programmatically access the data. To use it, users need to find the flow ref (like a data domain) and the key.

This information can be found in the data portal UI. For example, the dataset "MFIs lending margins on loans for house purchase, France, Monthly" has the Series key: RAI.M.FR.LMGLHH.EUR.MIR.Z. The flow ref is RAI and the key is M.FR.LMGLHH.EUR.MIR.Z.

We see the country code FR in the key. A nifty trick is to use the empty space (see code example below) to get all countries. One can also add another country using the OR operator. For example, M.FR+LU.LMGLHH.EUR.MIR.Z.

Here is a utility function to fetch data:

import io
import requests
import polars as pl

def get_ecb_data(flow_ref, key, params=None):
    base_url = "https://data-api.ecb.europa.eu/service/data"
    request_url = f"{base_url}/{flow_ref}/{key}"

    headers = {'Accept': 'text/csv'}
    response = requests.get(request_url, params=params, headers=headers)
    response.raise_for_status()

    return pl.read_csv(io.BytesIO(response.content))

df = get_ecb_data(
    flow_ref="RAI", 
    key="M..LMGLHH.EUR.MIR.Z", # use empty space to get all countries
    params = {
    'startPeriod': '2023-06',
    'endPeriod': '2023-12',
} # leave as None to get all time periods
)