Welcome to the new home of the HDX FAQ
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Code examples and Python notebook

Python and Javascript examples and a ready-to-run Python notebook

The HDX metadata endpoints can be used with many different coding languages and processes in the humanitarian sector. See specific code examples below.

Note that you can access the endpoints directly in your browser as well.

See Python specific examples below.

While these endpoints are designed for lightweight, token-free access, two Python libraries are available if you prefer a higher-level interface: ckanapi, a low-level library from CKAN, and the HDX Python API, which builds on it and is primarily intended for data contributors publishing data to HDX.

Search HDX for the most recently updated datasets of South Sudan for returnees data

import requests
response = requests.get(
    "https://data.humdata.org/api/action/package_search",
    params={
        "q": "returnees",
        "fq": "groups:ssd",
        "sort": "metadata_modified desc",
        "rows": 5
    }
)

data = response.json()
for dataset in data["result"]["results"]:
    print(dataset["title"])
    print(f"  Updated: {dataset['metadata_modified'][:10]}")

Search for datasets from UNFPA for population

Get all downloadable resources for a specific dataset

Python notebook

We provide a ready-to-run Python ipynb notebook showing how to query the metadata endpoints (package_search and package_show) for Python scripts and pipelines. You can open it directly in your browser via GitHub and test queries without needing to install anything locally.

Open the Python notebook here.

Important:

  • For the advanced section, you must provide your own HDX API token in the notebook.

  • Never commit your token or share it publicly! Treat it like a password.

The notebook covers:

  • Searching the HDX catalogue using package_search with q and fq filters

  • Aggregating catalogue stats using facet queries with rows=0

  • Extracting full dataset metadata via package_show

  • Automating shapefile downloads and loading into a GeoDataFrame

  • Querying tabular data via TDE (datastore_info, datastore_search)

Last updated

Was this helpful?