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

Python and Javascript examples

The HDX Tabular Data endpoints can be used with many different coding languages and processes in the humanitarian sector. Be mindful of query limits and performance, especially when working with large datasets. See specific code examples below.

Note that you can access the endpoints directly in your browser if you are signed in to HDX.

See Python specific examples per endpoint below.

These examples are tailored to the structure and field names of a specific resource, so when applying it to other resources, the filters and/or SQL will need to be adjusted to match their available columns and schema.

Native query

This code searches an HDX resource where the location_code field is exactly ‘AFG’, retrieves up to 5 results, and prints the total number of matches in the dataset.

import requests

API_TOKEN = "API_TOKEN" # Replace with your actual token
RESOURCE_ID = "45036735-305b-42ae-9aef-b941d6dcb6d6"
URL = "https://data.humdata.org/api/3/action/datastore_search"


headers = {
    "Authorization": API_TOKEN,
    "Content-Type": "application/json"
}

response = requests.post(
    URL,
    headers=headers,
    json={
        "resource_id": RESOURCE_ID,
        "filters": {"location_code": "AFG"},
        "limit": 5
    }
).json()

# Native query: Get first 5 records where location is 'AFG'
print("Total results:", response["result"]["total"])
for record in response["result"]["records"]:
    print(record)

SQL query

This code runs a SQL query on an HDX resource to return the first 5 rows where the location_field contains “AF” (case-insensitive search) and then prints how many rows were retrieved.

Last updated

Was this helpful?