Skip to content

pycensus.census.countries.germany.api

API client for Destatis GENESIS-Online (Germany census/population data).

Technical aspects

See :mod:pycensus.germany.destatis.README for the full verification log. In short: GENESIS-Online is a real RESTful/JSON API (base https://genesis.destatis.de/genesisWS/rest/2020 -- note: no "www-" prefix; the old www-genesis.destatis.de host 307-redirects here), and a registered account (username/password, or a personal API token used in place of username with password omitted, per GENESIS' "Anwenderdokumentation Webservice/API" v5.1) is required to authenticate. api_keys.json's "destatis_genesis" entry is a single token string, resolved here as (token, "") by :func:resolve_genesis_credentials.

Verified live 2026-08-21: the token currently configured in api_keys.json DOES authenticate and DOES retrieve real data, once two things are fixed relative to the 2026-08-20 attempt: (1) the current host (above, no "www-" prefix -- the old host's REST path had started 307/302-redirecting to a generic "announcement" page, which is what made this look like a dead endpoint rather than a URL staleness issue), and (2) per GENESIS' real spec ("Zugangsdaten: Felder im HTTP-Header"), username/password must be sent as literal HTTP request headers, not as fields in the POST body -- the 2026-08-20 attempt only tried body fields, HTTP Basic Auth, and a bearer header, never a plain username/password header pair, which is the one shape GENESIS actually documents and accepts. A real helloworld/logincheck call with the configured token as the username header (no password) returned {"Status":"Sie wurden erfolgreich an- und abgemeldet! ...", "Username":"<token>"} -- a real successful login, not the GAST guest-account rejection.

GenesisCredentialsError

Bases: RuntimeError

Raised when no GENESIS-Online credentials (token or username/password) are configured.

resolve_genesis_credentials(api_key=None, username=None, password=None)

Resolve GENESIS-Online credentials.

Resolution order
  1. Explicit username/password args, if either is given.
  2. A personal API token, resolved via :func:pycensus.api_keys.resolve_api_key with key name :data:pycensus.germany.constants.GENESIS_TOKEN_KEY (api_keys.json's "destatis_genesis" entry) or the :data:pycensus.germany.constants.GENESIS_TOKEN_ENV env var, or api_key if passed explicitly -- used as (token, "") per GENESIS' token-in-place-of-username convention.
  3. A real separate username/password pair, if configured under :data:pycensus.germany.constants.GENESIS_USERNAME_KEY / :data:pycensus.germany.constants.GENESIS_PASSWORD_KEY.

Returns:

Type Description
Tuple[str, str]

(username, password) tuple to send to GENESIS-Online.

Raises:

Type Description
GenesisCredentialsError

If none of the above resolve.

Parameters:

Name Type Description Default
api_key Optional[str]

Explicit GENESIS token.

None
username Optional[str]

Explicit user name.

None
password Optional[str]

Explicit password.

None

fetch_table(table_code, area=None, startyear=None, endyear=None, regionalvariable=None, regionalkey=None, api_key=None)

Fetch one real GENESIS table (semicolon-delimited flat text) by table code.

Parameters:

Name Type Description Default
table_code str

GENESIS table code, e.g. "12411-0010".

required
area Optional[str]

GENESIS area parameter ("all"/"Alle" for the full table).

None
startyear Optional[int]

First Stichtag/Jahr column to return; equal to endyear restricts to a single year -- verified live to return a single-year column instead of the table's full multi-year matrix.

None
endyear Optional[int]

Last Stichtag/Jahr column to return (see startyear).

None
regionalvariable Optional[str]

The table's regional dimension code (e.g. "DLAND" for states, "KREISE" for districts).

None
regionalkey Optional[str]

A region code (or "<prefix>*" wildcard) to filter to -- e.g. "09" (Bavaria's AGS code) filters 12411-0010 to one row, "09*" filters 12411-0015 to every district within Bavaria.

None

Raises:

Type Description
GenesisCredentialsError

If no credentials are configured.

list_datasets(year=2022)

List the Destatis datasets pyCensus knows about (metadata only, no network call).

Parameters:

Name Type Description Default
year int

Reference year.

2022

Returns:

Type Description
List[dict]

Dataset descriptions.