Search notes:

Open Data: Zefix

Accessing the Zefix API on a shell with CURL

Variables

First, we set the variable zefix_rest_url_api to the URL of the endpoint root:
zefix_rest_url=https://www.zefix.admin.ch/ZefixREST
zefix_rest_url_api=$zefix_rest_url/api/v1

List of municipalites and BFS IDs

Among others, the /community endpoint produces a list of political communities, the BFS id which identifies each community and the id of the registry office that is responsible for the the municipality:
curl -s $zefix_rest_url_api/community
In order to view the result more nicely, we use jq's @csv' instruction to turn the returned JSON objects into CSV format and then further pipe it to pspg for user-friendly viewing:
curl -s $zefix_rest_url_api/community | jq -r '.[] | [.name, .bfsId, .registryOfficeId] | @csv' | pspg --csv

Find the BFS ID of a given municipality

We want to determine the BFS ID of a given municipality, here: Pfungen
municipality_id=$( curl -s  $zefix_rest_url_api/community | jq -r '.[] | select(.name == "Pfungen") | (.id)' )

List companies in a municipality

We use -d @- <<DATA … DATA to specify a here document that contains the data for the POST body:
curl -s -X POST $zefix_rest_url_api/firm/search.json \
-H 'Content-Type: application/json' \
-d @- <<DATA
{
  "languageKey": "de",
  "maxEntries" : 999,
  "offset"     : 0,
  "legalSeats" : [ $municipality_id ]
}
DATA
Again, we want to view the result in neatly aligned with pspg:
curl -s -X POST $zefix_rest_url_api/firm/search.json \
-H 'Content-Type: application/json' \
-d @- <<DATA | jq -r '.list | .[] | [.name, .status, .uidFormatted, .shabDate, .deleteDate]  | @csv ' | pspg --csv --csv-separator ,
{
  "languageKey": "de",
  "maxEntries" : 999,
  "offset"     : 0,
  "legalSeats" : [ $municipality_id ]
}
DATA
SA: Open Data Switzerland

Index