# API wiki post: developers, collaborate here with API Q&As & tips you find helpful for other developers

**URL:** <https://community.dhis2.org/t/api-wiki-post-developers-collaborate-here-with-api-q-as-tips-you-find-helpful-for-other-developers/46011>\
**Category:** Development - Développement\
**Tags:** api\
**Created:** [10 February 2022 15:23 UTC](https://community.dhis2.org/t/api-wiki-post-developers-collaborate-here-with-api-q-as-tips-you-find-helpful-for-other-developers/46011 "2022-02-10T15:23:22Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gassim](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/gassim/32/35347_2.png) [@Gassim](https://community.dhis2.org/u/Gassim)\
**Post date:** [10 February 2022 15:23 UTC](https://community.dhis2.org/t/api-wiki-post-developers-collaborate-here-with-api-q-as-tips-you-find-helpful-for-other-developers/46011/1 "2022-02-10T15:23:22Z")

</div>

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](https://community.dhis2.org/t//46011#:~:text=children%5Bname%2Cid%5D%5D%5D-,Contributors%3A,-%40RModi).

* * *

# 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]]]](https://play.dhis2.org/2.37.2/api/programs?fields=id,name,categoryCombo%5Bid,name,categories%5Bid,name,categoryOptions%5Bid,name%5D%5D%5D)

#### 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](https://play.dhis2.org/2.37.2/api/programs?fields=id,name,categoryCombo%5Bid,name,categories%5Bid,name,categoryOptions%5Bid,name%5D%5D%5D&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](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](https://play.dhis2.org/2.37.2/api/schemas)  
\*\* See Schemas [docs](https://docs.dhis2.org/en/develop/using-the-api/dhis-core-version-235/web-api.html#webapi_schema)
- /api/resources - For example, [InstanceURL/api/resources](https://play.dhis2.org/2.37.2/api/resources)  
\*\* See Resources (the Web API entry point) [docs](https://docs.dhis2.org/en/develop/using-the-api/dhis-core-version-master/metadata.html#webapi_browsing_the_web_api)

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?

> [@Best way to export data as CSV or excel format?](https://community.dhis2.org/t/best-way-to-export-data-as-csv-or-excel-format/46333/2):
>
> The fastest would be to use the API and you will also be able to choose whether you want JSON, CSV, xls, or even gzip! Please see the docs: [Tracker - Using the API | DHIS2 Documentation](https://docs.dhis2.org/en/develop/using-the-api/dhis-core-version-master/tracker.html)
> 
> Additionally, if you have the report ready in the Events Reports app then you can get the API request from the Network tab. I explain the process in video below but here are the written instructions too.
> 
> First, before you click on ‘update’, open the browser’s developers’ tools ( **F12** ) and then click on **Network**. You will see the Network tab empty at first. Second, click on the Update in the Event Reports app and you will see that a couple of requests were recorded in the Network tab. Look for the request that has query in it such as ([https://play.dhis2.org/2.37.3/api/29/analytics/events/query/eBAyeGv0exc.json?dimension=pe:THIS\_QUARTER;LAST\_QUARTER&dimension=ou:ImspTQPwCqd&stage=Zj7UnCAulEk&displayProperty=NAME&outputType=EVENT&desc=eventdate&pageSize=100&page=1](https://play.dhis2.org/2.37.3/api/29/analytics/events/query/eBAyeGv0exc.json?dimension=pe:THIS_QUARTER;LAST_QUARTER&dimension=ou:ImspTQPwCqd&stage=Zj7UnCAulEk&displayProperty=NAME&outputType=EVENT&desc=eventdate&pageSize=100&page=1)) . Third, copy the url and you can choose how many items to include by changing the **pageSize** parameter, and you can choose whether you want XML, JSON, CSV, or XLS by changing the format that appears before the question mark (’ **?** ’) in the URL.

[https://padlet-uploads.storage.googleapis.com/1227681803/f2fed736b8d4becdca1fdf00efe0e580/DHIS\_2\_Event\_Reports\_\_\_Google\_Chrome\_2022\_03\_09\_17\_32\_34.mp4](https://padlet-uploads.storage.googleapis.com/1227681803/f2fed736b8d4becdca1fdf00efe0e580/DHIS_2_Event_Reports___Google_Chrome_2022_03_09_17_32_34.mp4)

#### Is there a way to retrieve TEIs from multiple programs?

> [@API to retreiving TEIs from multiple programs](https://community.dhis2.org/t/api-to-retreiving-teis-from-multiple-programs/50314/1):
>
> @Pacifique_Hategekima: is there an API call I can use to retrieve TEIs from multiple programs?

> [@API to retreiving TEIs from multiple programs](https://community.dhis2.org/t/api-to-retreiving-teis-from-multiple-programs/50314/2):
>
> The trackedEntityInstances requires either the program name to be specified or the tracked entity type so if you want to retrieve all TEIs from multiple programs you could use the trackedEntityType parameter. You’d need to either specifiy a specific ou (and it’s children or decendants if required) or you could use ouMode=ALL to retrieve all TEIs from all OUs.
> 
> Here’s an example, [https://play.dhis2.org/2.39.0/api/trackedEntityInstances?trackedEntityType=nEenWmSyUEp&ouMode=ALL&fields=\*&pageSize=2&totalPages=true](https://play.dhis2.org/2.39.0/api/trackedEntityInstances?trackedEntityType=nEenWmSyUEp&ouMode=ALL&fields=*&pageSize=1&totalPages=true) note that I added pageSize=1 because I used fields=\* which is not recommended but I did that so you could choose which fields you want returned then you can remove (pageSize & totalPages)

**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 🙏]

---

<div class="post-metadata">

**Author:** ![german](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/german/32/33608_2.png) [@german](https://community.dhis2.org/u/german)\
**Post date:** [23 February 2022 13:04 UTC](https://community.dhis2.org/t/api-wiki-post-developers-collaborate-here-with-api-q-as-tips-you-find-helpful-for-other-developers/46011/2 "2022-02-23T13:04:20Z")

</div>

This is awesome. Thanks to all contributors! We should turn this into an API FAQ if it keeps growing 🙂

---

<div class="post-metadata">

**Author:** ![Gassim](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/gassim/32/35347_2.png) [@Gassim](https://community.dhis2.org/u/Gassim)\
**Post date:** [23 February 2022 13:07 UTC](https://community.dhis2.org/t/api-wiki-post-developers-collaborate-here-with-api-q-as-tips-you-find-helpful-for-other-developers/46011/3 "2022-02-23T13:07:59Z")

</div>

Yes it’d would be great to have a reviewed version and this can remain a collaborated wiki by the community! (: I hope it grows more and more, for me it feels like sharing notes that came in handy!

There a couple of new tips that I have from @plinnegan so I will be adding them within this week. 👍👍

---

<div class="post-metadata">

**Author:** ![mykbitz](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/mykbitz/32/40893_2.png) [@mykbitz](https://community.dhis2.org/u/mykbitz)\
**Post date:** [14 February 2023 18:54 UTC](https://community.dhis2.org/t/api-wiki-post-developers-collaborate-here-with-api-q-as-tips-you-find-helpful-for-other-developers/46011/4 "2023-02-14T18:54:21Z")

</div>

This resource is great! I use the API a lot, especially when working on data models in Power BI. Particularly, when wanting to pull SQL views to my data models - i.e the period structure, pulling comments made on tracked events, etc.

---

<div class="post-metadata">

**Author:** ![Gassim](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/gassim/32/35347_2.png) [@Gassim](https://community.dhis2.org/u/Gassim)\
**Post date:** [15 February 2023 15:17 UTC](https://community.dhis2.org/t/api-wiki-post-developers-collaborate-here-with-api-q-as-tips-you-find-helpful-for-other-developers/46011/5 "2023-02-15T15:17:11Z")

</div>

Very glad to hear that. (: Thanks for the comment @mykbitz! A good reminder to keep adding input. It’s a wiki so community members are welcome to add their contributions. 👍👍

> [@mykbitz](#):
>
> when wanting to pull SQL views to my data models - i.e the period structure, pulling comments made on tracked events, etc.

True! Recently, we were discussing a method to download the SQL view xls using the API

> [@Using SQL View for integration](https://community.dhis2.org/t/using-sql-view-for-integration/51534/6):
>
> Hi @Shapr0019 & @Gassim , I have made changes to the script you have provided. import requests url = "https://play.dhis2.org/2.39.0.1/api/sqlViews/jaChwoXyY1N/data.xls" username = "admin" password = "district" response = requests.request("GET", url, auth=(username,password)) open('SQLViewresult.xls','wb').write(response.content) This should work. Thanks, Rithvik

---

<div class="post-metadata">

**Author:** ![sele](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/sele/32/27970_2.png) [@sele](https://community.dhis2.org/u/sele)\
**Post date:** [22 June 2023 14:34 UTC](https://community.dhis2.org/t/api-wiki-post-developers-collaborate-here-with-api-q-as-tips-you-find-helpful-for-other-developers/46011/6 "2023-06-22T14:34:47Z")

</div>

I am having a challenge in creating Standard report pulling data using API (JSON format) as it ignore the blank fields. Is there any way that I can force the Aggregate API to include the blank values. I tried to use " fields=\* " as recommended above but it doesn’t work because it still ignore the blanks.

Any suggestion will be appreciated

---

<div class="post-metadata">

**Author:** ![Gassim](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/gassim/32/35347_2.png) [@Gassim](https://community.dhis2.org/u/Gassim)\
**Post date:** [3 August 2023 11:45 UTC](https://community.dhis2.org/t/api-wiki-post-developers-collaborate-here-with-api-q-as-tips-you-find-helpful-for-other-developers/46011/7 "2023-08-03T11:45:25Z")

</div>

> [@Gassim](#):
>
> &filter=categoryCombo.categories.name:like:Implementing

@tzemp this helped me again today! Thanks! 😊

I hope next time I wont forget that in **fields** we’d use brackets categories[id] where as in **filter** we use dot categories.id .

---

<div class="post-metadata">

**Author:** ![Gassim](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/gassim/32/35347_2.png) [@Gassim](https://community.dhis2.org/u/Gassim)\
**Post date:** [3 August 2023 11:46 UTC](https://community.dhis2.org/t/api-wiki-post-developers-collaborate-here-with-api-q-as-tips-you-find-helpful-for-other-developers/46011/8 "2023-08-03T11:46:59Z")

</div>

> [@sele](#):
>
> I tried to use " fields=\* " as recommended above but it doesn’t work because it still ignore the blanks.

Could you reproduce the API request in any of the [play.dhis2.org](http://play.dhis2.org) instances and share the request so I or anyone else from the community might be able to figure out the issue?

Thanks!

---

<div class="post-metadata">

**Author:** ![Gassim](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/gassim/32/35347_2.png) [@Gassim](https://community.dhis2.org/u/Gassim)\
**Post date:** [5 September 2023 14:54 UTC](https://community.dhis2.org/t/api-wiki-post-developers-collaborate-here-with-api-q-as-tips-you-find-helpful-for-other-developers/46011/9 "2023-09-05T14:54:12Z")

</div>

Thanks @redet! 🌞🙏

---

<div class="post-metadata">

**Author:** ![Gassim](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/gassim/32/35347_2.png) [@Gassim](https://community.dhis2.org/u/Gassim)\
**Post date:** [4 June 2024 14:01 UTC](https://community.dhis2.org/t/api-wiki-post-developers-collaborate-here-with-api-q-as-tips-you-find-helpful-for-other-developers/46011/10 "2024-06-04T14:01:20Z")

</div>

Check out this short and helpful API cheatsheet by @eyeseetea!

> [@DHIS2 Web API Cheatsheet bookmark\_tabs](https://community.dhis2.org/t/dhis2-web-api-cheatsheet/59091):
>
> Hello all, We have designed a DHIS2 Web API “cheatsheet” and we are sharing in case it is useful for any of you blush Would love to hear your feedback! Do you miss anything important? Would you add or remove anything? Also, we were wondering if it is something you would find useful to have printed, as we were thinking of printing some to take with us to Oslo to share them with you. [API Cheatsheet Definitive-5.pdf](https://community.dhis2.org/uploads/short-url/g5S7LFT1HOcbOJB6dCParsypfD7.pdf) (167.4 KB)

---

<div class="post-metadata">

**Author:** ![eyeseetea](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/eyeseetea/32/48702_2.png) [@eyeseetea](https://community.dhis2.org/u/eyeseetea)\
**Post date:** [5 June 2024 09:07 UTC](https://community.dhis2.org/t/api-wiki-post-developers-collaborate-here-with-api-q-as-tips-you-find-helpful-for-other-developers/46011/11 "2024-06-05T09:07:32Z")

</div>

Thanks so much @Gassim!

If any of you have feedback to improve the cheatsheet, please tell us! ☺

---

<div class="post-metadata">

**Author:** ![Matthew\_Boddie](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/matthew_boddie/32/34043_2.png) [@Matthew\_Boddie](https://community.dhis2.org/u/Matthew_Boddie)\
**Post date:** [5 June 2024 18:15 UTC](https://community.dhis2.org/t/api-wiki-post-developers-collaborate-here-with-api-q-as-tips-you-find-helpful-for-other-developers/46011/12 "2024-06-05T18:15:42Z")

</div>

This is really great! I personally get a lot of specific use out of:

1. “&attribute=XXX:in:YYY,ZZZ,QQQ” as a parameter were XXX is your attribute in the trackedEntityInstances endpoint and YYY,ZZZ,QQQ are values you want to have appear
2. rootJunction=OR allows you to put in multiple filters and then if any are true, it will pull the metadata related

---

<div class="post-metadata">

**Author:** ![Gassim](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/gassim/32/35347_2.png) [@Gassim](https://community.dhis2.org/u/Gassim)\
**Post date:** [6 June 2024 12:43 UTC](https://community.dhis2.org/t/api-wiki-post-developers-collaborate-here-with-api-q-as-tips-you-find-helpful-for-other-developers/46011/13 "2024-06-06T12:43:35Z")

</div>

@eyeseetea - you’re welcome! Thanks for sharing with the community 👏

@Matthew_Boddie , those are really great tips! Thanks for sharing 🙏🙏

---

<div class="post-metadata">

**Author:** ![eyeseetea](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/eyeseetea/32/48702_2.png) [@eyeseetea](https://community.dhis2.org/u/eyeseetea)\
**Post date:** [6 June 2024 13:09 UTC](https://community.dhis2.org/t/api-wiki-post-developers-collaborate-here-with-api-q-as-tips-you-find-helpful-for-other-developers/46011/14 "2024-06-06T13:09:49Z")

</div>

Thank you for sharing @Matthew_Boddie! Noted to see how to add them 😃
