Saved queries and API
The Statbank can be used for more than manual look-ups.
Saved queries - API light
Saved queries - API light
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.
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
How to create a saved query
A typical workflow is:
- Find the table.
- Select variables and values.
- Retrieve the table.
- Make any calculations or changes to the presentation.
- Select Save your query.
- Specify how the selections are to be handled in future updates.
- Select the presentation or file format.
- 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
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
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.
Saved queries or API?
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
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:
-
GETis used to retrieve information about databases, folders, tables and metadata. -
POSTis used to retrieve data from a table.
A program therefore does not need to open the Statbank interface.
Metadata
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
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
Variables not specified in the query
If a variable is not included in the query, the result depends on the metadata of the table:
- If the variable can be eliminated and has an elimination value, that value is selected.
- If the variable can be eliminated but has no separate elimination value, the aggregated sum is used.
- 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
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
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
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
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
topor 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.