Hi all,
I had some questions about API requests and asked other developers in the Developers Workspace for support. I got some good responses; therefore, I thought it’d be helpful to have a wiki with helpful questions & responses that the developers in the DHIS2 Community of Practice can contribute to. Thanks!
How to help/contribute?
This is a wiki post which means all community members are welcome to edit it, add a Q&A or helpful tips then tag their CoP usernames at the end of this post. See contributors list at the end of this post.
Helpful API Q&As - API tips:
Q - Can we query for information from two different API endpoints in one request?
@RModi I would suggest to use two different queries and concatenate the result. Might be less daunting.
@tzemp You might be able to structure a single query using nested references … but if you want to access completely different resources from the api (say messageConversations and dataElementOperands) you’d probably need to make multiple requests
Q - How do we structure nested queries?
@tzemp to structure a single query using nested references, e.g. if you want to see the names of categoryOptions associated with a given program, you could use [instanceURL]/api/programs?fields=id,name,categoryCombo[id,name,categories[id,name,categoryOptions[id,name]]]
Q - How do we create filters in API requests?
@tzemp you can put the filters at the end, like: [instanceURL]/2.37.2/api/programs?fields=id,name,categoryCombo[id,name,categories[id,name,categoryOptions[id,name]]]&filter=categoryCombo.categories.name:like:Implementing
Q - How do we create more than one filter in an API request?
@tzemp you’ll need to do concatenate the filter conditions with &
like &filter=object:like:something&filter=object2:eq:xyz
Q - Can we add logic to filters in API requests?
@tzemp you can specify a rootJunction if you don’t want to use AND, e.g. &filter=object:like:something&filter=object2:eq:xyz&rootJunction=OR
See Logical Operators section here:
https://docs.dhis2.org/en/full/develop/dhis-core-version-master/developer-manual.html#webapi_metadata_object_filter
Q - What is a ‘deprecated’ API end point?
@austin In general a deprecated api is set to be removed in a future version, so relying on it isn’t recommended.
Q - Why is it not recommended to use fields=*?
@austin you can specify explicit fields within the api end point so you shouldn’t need fields=*
(which can create a heavy request)
Q - How can I know if there are dataSets or programStages that contain empty custom forms?
Maybe you can reuse these example API calls
https://play.dhis2.org/2.36.12/api/dataSets.json?fields=id,name,dataEntryForm[id,htmlCode]&filter=dataEntryForm:!null&filter=dataEntryForm.htmlCode:null
https://play.dhis2.org/2.36.12/api/programStages.json?fields=id,program[name,id],name,dataEntryForm[id,htmlCode]&filter=dataEntryForm:!null&filter=dataEntryForm.htmlCode:null
Q - API response says I need to get an ID for a metadata object, how?
@Gassim Generally, to get an ID of a metadata object, you need to check its API endpoint, here are two important resources that will help you find your way through the API:
- /api/schemas - For example, InstanceURL/api/schemas
** See Schemas docs - /api/resources - For example, InstanceURL/api/resources
** See Resources (the Web API entry point) docs
After that you will request the ID from the end point and use fields and filters to return the required result.
Q - What’s the fastest way to get all the results from the Event Reports app?
Is there a way to retrieve TEIs from multiple programs?
Q - How do I get all sublevels of a specific org unit - using [nested objects]
If you want to list out a nested object you need to add a children
field in square brackets along with the fields you want to retrieve. Nest the children
field as deep as your maximum Org unit level is. The example below fetches the four sub-levels of the org unit with the Id ImspTQPwCqd
.
https://play.dhis2.org/40.0.1/api/40/organisationUnits/ImspTQPwCqd?fields=name,id,children[name,id,children[name,id,children[name,id]]]
Contributors:
@RModi
@tzemp
@Gassim
@austin
@ctejo
@redet
[add your username here after adding a contribution ]