Skip to content

API reference

Everything on this page is importable directly from pytuik. The module-level functions delegate to a process-wide default client; the identically named TuikClient methods let you control the session.

Statistical portal

pytuik.get_themes

get_themes(lang: str | None = None) -> pd.DataFrame

Return all statistical themes (theme_name, theme_id).

pytuik.get_resources

get_resources(theme: int | str, types: Iterable[str] | None = None, lang: str | None = None) -> pd.DataFrame

Return every resource leaf under theme, optionally filtered by types.

pytuik.get_tables

get_tables(theme: int | str, lang: str | None = None) -> pd.DataFrame

Return downloadable tables (dataflow + istab) under theme.

pytuik.get_databases

get_databases(theme: int | str, lang: str | None = None) -> pd.DataFrame

Return legacy database links under theme.

SDMX

pytuik.get_data

get_data(dataflow_id: str, key: str = 'ALL', *, start: str | None = None, end: str | None = None, detail: str = 'full', dimension_at_observation: str = 'TIME_PERIOD', lang: str | None = None, labels: bool = True, drop_invariant: bool = True) -> pd.DataFrame

Fetch observations for a dataflow as a tidy long DataFrame.

pytuik.get_data_structure

get_data_structure(dataflow_id: str, lang: str | None = None) -> DataflowStructure

Fetch the dimensions and codelists of a dataflow.

pytuik.DataflowStructure dataclass

DataflowStructure(dataflow_id: str, dimensions: list[str], label_maps: dict[str, dict[str, str]] = dict(), time_dimension: str = 'TIME_PERIOD')

The subset of a DSD that pytuik needs.

Attributes:

Name Type Description
dataflow_id str

The agency,flow,version id this structure describes.

dimensions list[str]

Non-time dimension ids in key order — needed to build a narrower key when key="ALL" is rejected.

label_maps dict[str, dict[str, str]]

dim -> {code: localized label} for each coded dimension.

time_dimension str

The time dimension id (usually TIME_PERIOD).

Geographic portal

pytuik.geo_variables

geo_variables(lang: str | None = None) -> pd.DataFrame

Return the geographic indicator catalog.

pytuik.geo_data

geo_data(var_num: int | str, level: int | None = None, lang: str | None = None) -> pd.DataFrame

Return indicator values in long form (code, date, <name>).

pytuik.geo_map

geo_map(level: int = 2, geometry: bool = True) -> pd.DataFrame

Return administrative boundaries for a level (2/3/4/9).

Client

pytuik.TuikClient

TuikClient(lang: str = 'en', *, timeout: float = 30.0, retries: int = 3, cache_ttl: float = 3600.0, user_agent: str | None = None)

A reusable session for all three TUIK services.

Parameters:

Name Type Description Default
lang str

Default language ("en" or "tr") for calls that omit one.

'en'
timeout float

Per-request timeout in seconds.

30.0
retries int

Number of retry attempts for idempotent GETs on connect errors, timeouts, and 5xx responses. Never retries on 4xx.

3
cache_ttl float

Seconds to cache the rarely-changing theme tree and sideMenu documents. 0 disables caching.

3600.0
user_agent str | None

Override the default browser-like User-Agent.

None

close

close() -> None

Close the underlying HTTP connection pool.

clear_cache

clear_cache() -> None

Empty the in-memory TTL cache.

get_portal_json

get_portal_json(path: str, *, lang: str, cache: bool = True) -> Any

GET a veriportali API endpoint, handling the cookie dance and envelope.

Parameters:

Name Type Description Default
path str

API path relative to the veriportali base, e.g. "/api/en/data/statistical-themes".

required
lang str

Language segment; also gates the session cookie.

required
cache bool

Whether to serve from / store in the TTL cache.

True

Returns:

Type Description
Any

The data payload of the portal envelope.

get_json

get_json(url: str, *, params: dict[str, Any] | None = None, cache: bool = False) -> Any

GET an arbitrary URL and decode JSON (used by the geo portal).

get_bytes

get_bytes(url: str, *, params: dict[str, Any] | None = None, headers: dict[str, str] | None = None) -> bytes

GET an arbitrary URL and return raw bytes (used for SDMX-ML).

get_themes

get_themes(lang: str | None = None) -> pd.DataFrame

Return all statistical themes (theme_name, theme_id).

get_resources

get_resources(theme: int | str, types: Iterable[str] | None = None, lang: str | None = None) -> pd.DataFrame

Return every resource leaf under theme, optionally filtered.

get_tables

get_tables(theme: int | str, lang: str | None = None) -> pd.DataFrame

Return downloadable tables (dataflow + istab) under theme.

get_databases

get_databases(theme: int | str, lang: str | None = None) -> pd.DataFrame

Return legacy database links under theme.

get_data

get_data(dataflow_id: str, key: str = 'ALL', *, start: str | None = None, end: str | None = None, detail: str = 'full', dimension_at_observation: str = 'TIME_PERIOD', lang: str | None = None, labels: bool = True, drop_invariant: bool = True) -> pd.DataFrame

Fetch observations for a dataflow as a tidy long DataFrame.

get_data_structure

get_data_structure(dataflow_id: str, lang: str | None = None) -> DataflowStructure

Fetch the dimensions and codelists of a dataflow.

geo_variables

geo_variables(lang: str | None = None) -> pd.DataFrame

Return the geographic indicator catalog.

geo_data

geo_data(var_num: int | str, level: int | None = None, lang: str | None = None) -> pd.DataFrame

Return indicator values in long form (code, date, <name>).

geo_map

geo_map(level: int = 2, geometry: bool = True) -> pd.DataFrame

Return administrative boundaries for a level (2/3/4/9).

pytuik.get_default_client

get_default_client() -> TuikClient

Return the process-wide default client, creating it on first use.

Thread-safe: the client is created under a lock so concurrent first calls do not race.

pytuik.set_default_client

set_default_client(client: TuikClient) -> None

Replace the process-wide default client (e.g. to reconfigure timeouts).

Exceptions

pytuik.TuikError

Bases: Exception

Base class for every error raised by pytuik.

pytuik.TuikHTTPError

TuikHTTPError(message: str, *, status_code: int | None = None, url: str | None = None)

Bases: TuikError

A non-2xx HTTP response survived the retry policy.

Parameters:

Name Type Description Default
message str

Human-readable description.

required
status_code int | None

The HTTP status code of the failing response, when available.

None
url str | None

The requested URL.

None

pytuik.TuikPortalError

TuikPortalError(message: str)

Bases: TuikError

The veriportali envelope reported isError=True.

Carries the portal-supplied message field.

pytuik.TuikSDMXError

Bases: TuikError

An SDMX request or parse failed (e.g. a rejected key or malformed XML).

pytuik.TuikValidationError

Bases: TuikError, ValueError

An argument failed validation, raised before any network I/O.

Subclasses :class:ValueError so untyped callers still catch it naturally.