HDX Python library
Learn about the HDX library to contribute data to the platform.
Last updated
Was this helpful?
Was this helpful?
from hdx.api.configuration import Configuration
from hdx.data.dataset import Dataset
from hdx.data.resource import Resource
# Connect. Use hdx_site="stage" while testing; "prod" to go live.
Configuration.create(hdx_site="stage", user_agent="MyOrg_MyProject")
# Core metadata fields
dataset = Dataset({
"name": "my-dataset-name", # lowercase, no spaces; becomes the URL
"title": "My Dataset Title", # shown on the dataset page
"notes": "Description of the data.",
"dataset_source": "Where the data comes from",
"license_id": "cc-by",
"methodology": "Other",
"methodology_other": "How the data was collected.",
"private": False,
})
dataset.set_organization("my-org-id") # org the dataset belongs to
dataset.set_maintainer("my-user-id") # who to contact about it
dataset.set_expected_update_frequency("Every month")
dataset.set_time_period("2026-01-01", "2026-06-30") # dates the data covers
dataset.add_country_location("AFG") # ISO3 code or country name
dataset.add_tags(["displacement", "refugees"]) # from the approved list
# Attach a file, then create
resource = Resource({"name": "mydata.csv", "description": "The data file"})
resource.set_format("csv")
resource.set_file_to_upload("path/to/mydata.csv")
dataset.add_update_resource(resource)
dataset.create_in_hdx()dataset = Dataset.read_from_hdx("my-dataset-name")
resource = dataset.get_resource(0)
resource.set_file_to_upload("path/to/new-file.csv")
dataset.update_in_hdx()