Code Examples
Python and Javascript examples
The HDX DataStore API 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 API in your browser if you are signed in to HDX.
See Python specific examples per endpoint below.
Native Query
This code searches an HDX resource where the country field is exactly ‘Kenya’, 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 = "RESOURCE_ID" # Replace with actual resource id
url = "https://data.humdata.org/api/3/action/datastore_search"
params = {
"resource_id": resource_id,
"filters": {"country": "Kenya"},
"limit": 5
}
headers = {
"Authorization": api_token
}
# Native query: Get first 5 records where country is 'kenya'
response = requests.get(url, headers=headers, params=params)
data = response.json()
print("Total results:", data["result"]["total"])SQL Query
This code runs a SQL query on an HDX resource to return the first 5 rows where the country field contains “kenya” (case-insensitive search) and then prints how many rows were retrieved.
import requests
api_token = "API_TOKEN" # Replace with your actual token
resource_id = "RESOURCE_ID" # Replace with actual resource id
url = "https://data.humdata.org/api/3/action/datastore_search_sql"
# SQL query: Get first 5 records where country contains 'kenya'
sql = f'SELECT * FROM "{resource_id}" WHERE country ILIKE \'%kenya%\' LIMIT 5'
headers = {
"Authorization": api_token
}
response = requests.get(url, headers=headers, params={"sql": sql})
data = response.json()
print("Total results returned:", len(data["result"]["records"]))See Javascript specific examples per endpoint below.
Native Query
This code queries for rows in the given resource that contain the word “kenya”, limits the returned rows to 5, and then logs the total number of matches across the dataset to the console.
const resourceId = "RESOURCE_ID"; // Replace with actual resource id
const apiToken = "API_TOKEN"; // Replace with your actual token
fetch(`https://data.humdata.org/api/3/action/datastore_search?resource_id=${resourceId}&q=kenya&limit=5`, {
headers: {
"Authorization": apiToken
}
})
.then(response => response.json())
.then(data => {
console.log("Total results:", data.result.total);
});SQL Query
This code runs a SQL query against the HDX DataStore API to fetch up to 5 rows from the given resource where the country field contains the word “kenya”, and then logs how many rows were returned.
const resourceId = "RESOURCE_ID"; // Replace with actual resource id
const apiToken = "API_TOKEN"; // Replace with your actual token
const sql = `SELECT * FROM "${resourceId}" WHERE country ILIKE '%kenya%' LIMIT 5`;
const encodedSql = encodeURIComponent(sql);
fetch(`https://data.humdata.org/api/3/action/datastore_search_sql?sql=${encodedSql}`, {
headers: {
"Authorization": apiToken
}
})
.then(response => response.json())
.then(data => {
console.log("Total results returned:", data.result.records.length);
});Last updated
Was this helpful?