Export data for external analysis using API

Hi folks,

I want to export the HIV aggregated data from DHIS2 to our database for external analysis. This needs to be implemented using web APIs.

I am doing it in JAVA using the DHIS2 JAVA client.

Right now I am able to make the connection and list all the organizations.

But the part where I want to fetch the data is not working.

In the documentation it is mentioned I have to pass the datasetId. From where I will get the datasetId.

Either it is possible through Java client or not?

Any sort of help and guidance will be highly appreciated.

Regards

1 Like

Hi @abdulsubbooh,

Thank you for your question, and welcome to the community! :tada::tada:

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:


As for your situation, you’d need to get the dataSetId from the /api/dataSetID endpoint. Here’s an example where I specify the required fields to be dataSetId and dataSet name but I filter the results to only show the dataSetIds for a specific organisation unit:

[the instance URL]/api/dataSets?fields=[the fields to return]&filter=organisationUnits.id:eq:[the organisation unit ID]


Thanks and I hope this helps! :+1:

2 Likes

Thank you for your answer. Lemme check this out.

1 Like

Great, thanks! Let me know if it solves the issue and feel free to post back to the community with more questions or about your experience / use case.

Thank you!

1 Like

@Gassim thanks for your guide. The link you mentioned is working for me. I just want to ask whether there is some DHIS2 Java Client to handle this request or I need to call it by using Java HTTP Client.

You’re welcome! If I understand your question properly then yes, if your Java client works as an API and can authenticate between itself and the instance then it should be okay to use it to connect to the DHIS2 API endpoints.

1 Like

Working on a similar task too, retrieving the data via API for analysis using R

1 Like

That’s really cool @Mbevi! Thanks for sharing! Please feel free to make a topic about your experience on retrieving data from DHIS2 API using R. Other developers in the Development - Développement category will probably find it helpful too. Even if you share notes like why you chose R and what are the other options, what are the inputs/outcomes?

Good luck! And for sure post back if you need any support from the communityđź‘Ť

1 Like

Just hitting something like InstanceURL/api/dataSets?fields=id,name should give you a clean list of dataset names with their IDs.
If you’re using the DHIS2 Java client, it should work as long as it can handle authenticated API calls. If not, you can always fall back on Java’s HTTP client to make the request and process the response. I’ve had to mix and match tools before, so don’t stress too much if the client doesn’t do everything you need.
Also, if you’re dealing with a lot of sensitive data, tools like idanalyzer.com can be handy for adding an extra layer of validation. I’ve found stuff like that useful when managing large datasets or ensuring things stay secure.