fred
FRED — U.S. macroeconomic time-series
Fetch U.S. economic data from the Federal Reserve Bank of St. Louis FRED API: interest rates, CPI (inflation), unemployment, and GDP. Use a series ID such as UNRATE, CPIAUCSL, or FEDFUNDS. For other providers, see std::data/finance/dbnomics.
Returns a series as a list of { date, value } observations (a missing value is null), oldest-first, plus a fredSeriesInfo call for a series' title/display-units/frequency/coverage.
Setup
Requires a free API key. Get one at https://fred.stlouisfed.org/docs/api/api_key.html and set it as the FRED_API_KEY environment variable.
Usage
import { fredSeries } from "std::data/finance/fred"
node main() {
const series = fredSeries("UNRATE", "2024-01-01") catch { seriesId: "", units: "", observations: [] }
for (obs in series.observations) {
print("${obs.date}: ${obs.value}")
}
}Types
FredObservation
A single FRED observation. value is null when FRED reports the value as missing (".").
/** A single FRED observation. `value` is null when FRED reports the value as missing ("."). */
export type FredObservation = {
date: string;
value?: number
}(source)
FredSeries
A FRED series whose units is a transform code, such as "lin" or "pch". Use fredSeriesInfo for display units.
/** A FRED series whose `units` is a transform code, such as "lin" or "pch".
Use `fredSeriesInfo` for display units. */
export type FredSeries = {
seriesId: string;
units: string;
observations: FredObservation[]
}(source)
FredSeriesInfo
Metadata about a FRED series, with display units such as "Percent".
/** Metadata about a FRED series, with display units such as "Percent". */
export type FredSeriesInfo = {
id: string;
title: string;
units: string;
frequency: string;
observationStart: string;
observationEnd: string;
notes: string
}(source)
FredUnits
FRED units-transform codes (the units request parameter). null = native ("lin").
/** FRED units-transform codes (the `units` request parameter). `null` = native ("lin"). */
export type FredUnits =
| "lin"
| "chg"
| "ch1"
| "pch"
| "pc1"
| "pca"
| "cch"
| "cca"
| "log"(source)
FredFrequency
FRED frequency-aggregation codes (the frequency request parameter). null = native. Includes the weekly-ending variants (wef, weth, …).
/** FRED frequency-aggregation codes (the `frequency` request parameter). `null` = native.
Includes the weekly-ending variants (wef, weth, …). */
export type FredFrequency =
| "d"
| "w"
| "bw"
| "m"
| "q"
| "sa"
| "a"
| "wef"
| "weth"
| "wetu"
| "wew"
| "wetdt"
| "wem"
| "wesun"(source)
Effects
std::fred
effect std::fred {
seriesId: string
}(source)
Functions
fredSeries
fredSeries(
seriesId: string,
observationStart: string = "",
observationEnd: string = "",
frequency: FredFrequency | null = null,
units: FredUnits | null = null,
limit: number = 0,
): Result<FredSeries> raises <std::fred, std::env, std::http::fetchJSON>Fetch observations for a FRED economic series, ordered oldest first. Requires FRED_API_KEY. For the most recent data, set observationStart rather than relying on limit, which keeps the oldest observations.
Set the observation start date when you want recent data. A limit takes observations from the oldest end of the selected range. Missing source values become null.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| seriesId | string | A FRED series ID such as "UNRATE" or "CPIAUCSL". | |
| observationStart | string | "" | The earliest YYYY-MM-DD date, or empty for no lower bound. |
| observationEnd | string | "" | The latest YYYY-MM-DD date, or empty for no upper bound. |
| frequency | FredFrequency | null | null | The aggregation frequency, or null to keep the native frequency. |
| units | FredUnits | null | null | The units transform, such as "pch" for percent change, or null for native values. |
| limit | number | 0 | The maximum observations from the oldest, or zero for the API default. |
Returns: Result<FredSeries>
Throws: std::fred, std::env, std::http::fetchJSON
Needs: network, env
(source)
fredSeriesInfo
fredSeriesInfo(
seriesId: string,
): Result<FredSeriesInfo> raises <std::fred, std::env, std::http::fetchJSON>Fetch a FRED series' title, display units, frequency, and coverage dates. Requires FRED_API_KEY.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| seriesId | string | A FRED series ID such as "UNRATE". |
Returns: Result<FredSeriesInfo>
Throws: std::fred, std::env, std::http::fetchJSON
Needs: network, env
(source)