Saved queries and API

The Statbank can be used for more than manual look-ups.

Saved queries, API and advanced use of the Statbank.
Latest release: 2026-02-10Next release: 2027-02-09

Saved queries - API light

The Statbank can be used for more than manual look-ups. A table extract can be saved as a link and reused later, and data can be retrieved directly from programs through the Statbank API.

For many users, Saved queries are the simplest solution. No knowledge of API programming is needed, because the extract is first put together in the ordinary Statbank interface.

When Save your query is selected, the Statbank saves the definition of the current extract and creates a unique link.

A link can look like this:

https://bank.stat.gl:443/sq/4772114d-4c69-4023-bee3-399e84ab5382

The link can be stored in a document, a spreadsheet, a program or a bookmark and used again later.

We call this API light, because it gives many of the advantages of an API without the user having to construct an API call.

Save the link, not just the file

For a table extract that is used regularly, it is often better to save the link to the saved query than to save only an Excel or CSV file. The link can be used to retrieve a new extract when the Statbank is updated.

How to create a saved query

A typical workflow is:

  1. Find the table.
  2. Select variables and values.
  3. Retrieve the table.
  4. Make any calculations or changes to the presentation.
  5. Select Save your query.
  6. Specify how the selections are to be handled in future updates.
  7. Select the presentation or file format.
  8. Save the generated link.

It is particularly important to decide how the time variable is handled. An extract can be defined for specific periods or for the latest periods, so that new years or quarters are included automatically.

Retrieve the result in a specific format

The file format can be given directly in the link.

Excel:

https://bank.stat.gl:443/sq/4772114d-4c69-4023-bee3-399e84ab5382.xlsx

CSV:

https://bank.stat.gl:443/sq/4772114d-4c69-4023-bee3-399e84ab5382.csv

This makes it possible to use the same saved query both manually and from, for example, R, Python, Excel or another program.

Choose language

In table references, the letter after the subject code gives the language:

  • BEDSTA – Danish
  • BENSTA – Greenlandic
  • BEESTA – English

The three references point to the same file in the Statbank, BEXSTA.px. On look-up, BEDSTA, for example, is converted to BEXSTA with Danish as the language.

In saved queries, the language is given with the parameter lang:

  • Danish: ?lang=da
  • Greenlandic: ?lang=kl
  • English: ?lang=en

Example with Excel in Danish:

https://bank.stat.gl:443/sq/4772114d-4c69-4023-bee3-399e84ab5382.xlsx?lang=da

Here lang=da means that the table texts are returned in Danish.

See how a saved query is defined

To see information about the definition of the extract rather than the table itself, select can be added to the link:

https://bank.stat.gl:443/sq/4772114d-4c69-4023-bee3-399e84ab5382?lang=da&select

This is useful for troubleshooting, documentation and automation, because it shows how the extract is put together.

Document and share the table extract used

A saved query is also useful as documentation of which table extract a calculation, figure or table is based on.

The link can be used, for example:

  • as a source reference or footnote in a report
  • in the documentation of an analysis project
  • when a table or figure is passed on to others
  • when a colleague later needs to recreate the same extract

A source reference can be written like this:

(på dansk)  
Kilde: Grønlands Statistik, [Statistikbanken](https://bank.stat.gl/BEDSTA)  
tabel: https://bank.stat.gl:443/sq/4772114d-4c69-4023-bee3-399e84ab5382  

(på grønlandsk)  
Najoqqutaq: Naatsorsueqqissaartarfik, [Kisitsisaataasivik](https://bank.stat.gl/BENSTA)  
tabel: https://bank.stat.gl:443/sq/4772114d-4c69-4023-bee3-399e84ab5382?lang=kl  

(på engelsk)  
Source: Statistics Greenland, [Statbank Greenland](https://bank.stat.gl/BEESTA)  
tabel: https://bank.stat.gl:443/sq/4772114d-4c69-4023-bee3-399e84ab5382?lang=en  

If the recipient needs the table itself, the link to a specific file format can be passed on, for example:

https://bank.stat.gl:443/sq/4772114d-4c69-4023-bee3-399e84ab5382.xlsx?lang=da

This is an advantage over just sending a loose Excel file, because the link also points back to the defined extract in the Statbank.

Fixed or rolling periods

A saved query can be defined so that it either uses specific periods or automatically includes new periods when the table is updated.

If the link is used as documentation of a specific historical extract, the source reference should therefore also include the date on which the data were retrieved. This is particularly important if the query is defined for, for example, the latest periods.

Example:

Kilde: Grønlands Statistik, [Statistikbanken](https://bank.stat.gl/BEDSTA)  
tabel: https://bank.stat.gl:443/sq/4772114d-4c69-4023-bee3-399e84ab5382  
hentet: dag. måned 20xx.

Saved queries or API?

Saved queries and the API can both be used for automation, but they suit different needs.

Saved queries API
The extract is created In the Statbank interface In program code
Best for Fixed and repeated extracts Dynamic and flexible extracts
Programming Not needed Normally needed
Selection of data Defined in advance in the browser Can be changed by the program
Metadata Limited access Can be retrieved programmatically
R and Python Retrieve data from the saved link Retrieve data directly from the table

Rule of thumb: Use Saved queries when the extract can first be defined in the browser. Use the API when the program itself is to select table, values, periods or other parameters.

API

The Statbank provides a full API. An API call works directly with the statistical table and its metadata.

The API uses ordinary HTTP calls:

  • GET is used to retrieve information about databases, folders, tables and metadata.
  • POST is used to retrieve data from a table.

A program therefore does not need to open the Statbank interface.

Metadata

A GET call to a table returns metadata about the table. The metadata describe, among other things:

  • the title of the table
  • variables and their codes
  • display texts
  • possible values
  • value codes
  • which variable is time
  • whether a variable can be eliminated

Metadata are central when programming against the API, because the data query itself normally uses codes and not the displayed texts.

In R, metadata can be viewed with the package statgl:

library(statgl)

statgl_meta(
  statgl_url("BEXSTA", lang = "da")
)

Data extraction with POST

A data extract consists of a query and a requested response.

A simplified example can look like this:

{
  "query": [
    {
      "code": "gender",
      "selection": {
        "filter": "item",
        "values": ["M", "K"]
      }
    },
    {
      "code": "time",
      "selection": {
        "filter": "top",
        "values": ["5"]
      }
    }
  ],
  "response": {
    "format": "json-stat2"
  }
}

Here specific values are selected for gender, while top on the time variable selects the latest five periods.

Ways of selecting values

The API supports several types of selection.

item

Selects specific value codes:

"selection": {
  "filter": "item",
  "values": ["M", "K"]
}

all

Selects all values or values that match a pattern:

"selection": {
  "filter": "all",
  "values": ["*"]
}

A pattern such as 01* can be used to select all values beginning with 01.

top

Selects the first n values. For the time variable, top means that the latest n periods are selected:

"selection": {
  "filter": "top",
  "values": ["5"]
}

agg

Applies a defined aggregation:

"selection": {
  "filter": "agg:ageG5",
  "values": ["0-4", "5-9", "10-14"]
}

vs

Applies values from another value set:

"selection": {
  "filter": "vs:regionX",
  "values": ["01", "02"]
}

Aggregations and value sets must be defined in the Statbank.

Variables not specified in the query

If a variable is not included in the query, the result depends on the metadata of the table:

  1. If the variable can be eliminated and has an elimination value, that value is selected.
  2. If the variable can be eliminated but has no separate elimination value, the aggregated sum is used.
  3. If the variable cannot be eliminated, all values are selected.

The metadata should therefore be checked if an API result contains fewer or more dimensions than expected.

Output formats

The API can return data in several formats:

  • px, csv, csv2, csv3, json, xlsx, json-stat, json-stat2, sdmx

If response is omitted, the result is returned as (px).

For programming, json-stat2 is often a suitable format, while csv and xlsx are practical for further use in tables and spreadsheets.

Use the API from R with statgl

In ordinary use from R, it is normally not necessary to write the JSON call by hand. The package statgl converts R arguments into a PxWeb call and returns the result as a tibble.

library(statgl)

bef <- statgl_fetch(
  "BEXSTA",
  gender = c("M", "K"),
  time = 2020:2026,
  .lang = "da",
  .col_code = TRUE
)

The codes for variables and values can first be examined with:

statgl_meta(
  statgl_url("BEXSTA", lang = "da")
)

statgl_fetch() also supports helper functions for, among other things, all values and the latest periods:

bef <- statgl_fetch(
  "BEXSTA",
  gender = px_all(),
  time = px_top(5),
  .lang = "da",
  .col_code = TRUE
)

Example: fetch data and show a chart

An API extract can be passed directly on to statgl_plot(). Here the latest 15 years are retrieved from BEXSTA for women and men and shown as a time series.

library(statgl)

statgl_fetch(
  "BEXSTA",
  gender = c("K", "M"),
  time = px_top(15),
  .lang = "da",
  .col_code = TRUE
) |>
  statgl_plot(
    x = time,
    group = gender,
    title = "Befolkningen efter køn",
    subtitle = "Seneste 15 år"
)

Population by gender, latest 15 years

Good practice for automated use

When the Statbank is used from programs, it is recommended to:

  • retrieve only the values that are actually needed
  • use top or a limited selection of periods when the whole time series is not needed
  • save and reuse fixed queries when the extract does not need to be dynamic
  • retrieve metadata before developing code against a new table
  • use table and value codes in program code rather than depending on display texts
  • handle HTTP errors in automated production systems

The text on this page was translated with AI assistance. Graphs, tables and key figures are not AI-generated.

Lars Pedersen
  • Population
  • Access for researchers
  • Statbank
No matching items
Rendered