UNPKG

aiwebfinances

Version:

Display pricing that covers country inflation

25 lines (19 loc) 857 B
export async function fetchInflationData(countryCode: string) { const WORLD_BANK_INFLATION_API = `https://api.worldbank.org/v2/country/${countryCode}/indicator/FP.CPI.TOTL.ZG?format=json`; try { const response = await fetch(WORLD_BANK_INFLATION_API); // Check if the response is okay (status 200) if (!response.ok) { throw new Error(`Error fetching inflation data: ${response.statusText}`); } // Parse the JSON response const data = await response.json(); // The most recent data for Ireland is in data[1][0] const inflationData = data[1][0]; const inflationRate = inflationData.value; return inflationRate; // Return the inflation rate } catch (error) { console.error("Error fetching inflation data:", error); throw new Error("Failed to fetch inflation data"); } }