Code examples
Ready-made snippets showing how to call the APIs and download datasets. For interactive per-endpoint documentation, see the API reference.
Replace YOUR_KEY with a real key from the front page.
The URLs point to the test gateway. On the production portal, api.test.data.regjeringen.no is replaced with api.data.regjeringen.no.
Call an API with your key
APIs that require a key are read with the Ocp-Apim-Subscription-Key header. Get a key on the front page and replace YOUR_KEY below. language is required.
curl
curl -H "Ocp-Apim-Subscription-Key: YOUR_KEY" \
"https://api.test.data.regjeringen.no/content/GetDocList?language=nb&pageSize=10"JavaScript
const res = await fetch(
"https://api.test.data.regjeringen.no/content/GetDocList?language=nb&pageSize=10",
{ headers: { "Ocp-Apim-Subscription-Key": "YOUR_KEY" } },
);
const docs = await res.json();
console.log(docs);Python
import requests
res = requests.get(
"https://api.test.data.regjeringen.no/content/GetDocList",
params={"language": "nb", "pageSize": 10},
headers={"Ocp-Apim-Subscription-Key": "YOUR_KEY"},
)
res.raise_for_status()
docs = res.json()
print(docs)Download a dataset (no key)
The nightly exports are open and served gzipped. Use --compressed so curl decompresses automatically.
curl
curl --compressed -o content.json \
https://api.test.data.regjeringen.no/exports/content.jsonJavaScript
// fetch() transparently decodes gzip.
const res = await fetch("https://api.test.data.regjeringen.no/exports/content.json");
const documents = await res.json();
console.log(documents.length, "documents");Python
import requests
# requests transparently decodes gzip.
res = requests.get("https://api.test.data.regjeringen.no/exports/content.json")
res.raise_for_status()
documents = res.json()
print(len(documents), "documents")Paginate and aggregate documents
A fuller example: fetch every press release in a date range by following the pagination (page/pageSize), then count them per ministry. Use showOwner=true to include the owner.
curl
#!/usr/bin/env bash
# Fetch every press release from 2024, page by page (needs jq).
key="YOUR_KEY"
base="https://api.test.data.regjeringen.no/content/GetDocList"
query="language=nb&type=pressemelding&fromDate=2024.01.01&toDate=2024.12.31&showOwner=true&pageSize=100"
page=1
while :; do
batch=$(curl -s -H "Ocp-Apim-Subscription-Key: $key" "$base?$query&page=$page")
# Documents live under data.items; the owner is an object (use .owner.depName).
echo "$batch" | jq -c '.data.items[] | { title, date, ministry: .owner.depName }'
[ "$page" -ge "$(echo "$batch" | jq '.data.pager.totalPages')" ] && break
page=$((page + 1))
doneJavaScript
const KEY = "YOUR_KEY";
const BASE = "https://api.test.data.regjeringen.no/content/GetDocList";
// Follow the pager until the last page is reached.
async function fetchAll(params) {
const all = [];
for (let page = 1; ; page++) {
const url = new URL(BASE);
url.search = new URLSearchParams({ ...params, page, pageSize: "100" });
const res = await fetch(url, {
headers: { "Ocp-Apim-Subscription-Key": KEY },
});
if (!res.ok) throw new Error(`HTTP ${res.status}`);
// Documents are under data.items; data.pager.totalPages drives pagination.
const { data } = await res.json();
all.push(...data.items);
if (page >= data.pager.totalPages) break;
}
return all;
}
const docs = await fetchAll({
language: "nb",
type: "pressemelding",
fromDate: "2024.01.01",
toDate: "2024.12.31",
showOwner: "true",
});
// Count press releases per owning ministry (owner is an object).
const byMinistry = {};
for (const doc of docs) {
const ministry = doc.owner?.depName ?? "(ukjent)";
byMinistry[ministry] = (byMinistry[ministry] ?? 0) + 1;
}
console.table(byMinistry);Python
import requests
from collections import Counter
KEY = "YOUR_KEY"
BASE = "https://api.test.data.regjeringen.no/content/GetDocList"
def fetch_all(**params):
"""Follow pagination until a short (final) page is returned."""
documents = []
page = 1
while True:
res = requests.get(
BASE,
params={**params, "page": page, "pageSize": 100},
headers={"Ocp-Apim-Subscription-Key": KEY},
)
res.raise_for_status()
# Documents are under data.items; data.pager.totalPages drives pagination.
data = res.json()["data"]
documents.extend(data["items"])
if page >= data["pager"]["totalPages"]:
break
page += 1
return documents
docs = fetch_all(
language="nb",
type="pressemelding",
fromDate="2024.01.01",
toDate="2024.12.31",
showOwner="true",
)
# Count press releases per owning ministry (owner is an object).
by_ministry = Counter(
(doc.get("owner") or {}).get("depName", "(ukjent)") for doc in docs
)
for ministry, count in by_ministry.most_common():
print(f"{count:4} {ministry}")