Sending a Patient Bundle to HapiFHIR

We are currently working on a functionality to exchange data between #UgandaEMR and a central server to facilitate Case Based Surveillance for HIV patients. We are opting to use Hapi FHIR servers for this implementation, however data must be sent as a bundle, i.e, Every Person/Patient record must be sent with all the corresponding client information.

This includes:

  1. Patient Names.
  2. Patient Numbers
  3. Encounter information.

We are looking for a workaround to achieve this. Thank you. @ssmusoke @slubwama

hello @solemabrothers

are you looking into some thing like this

  "resourceType": "Bundle",
  "type": "transaction",
  "entry": [ {
    "fullUrl": "urn:uuid:3bc44de3-069d-442d-829b-f3ef68cae371",
    "resource": {
      "resourceType": "Patient",
      "identifier": [ {
        "system": "",
        "value": "12345"
      } ],
      "name": [ {
        "family": "Jameson",
        "given": [ "J", "Jonah" ]
      } ],
      "gender": "male"
    "request": {
      "method": "POST",
      "url": "Patient",
      "ifNoneExist": "identifier=|12345"
  }, {
    "resource": {
      "resourceType": "Observation",
      "status": "final",
      "code": {
        "coding": [ {
          "system": "",
          "code": "789-8",
          "display": "Erythrocytes [#/volume] in Blood by Automated count"
        } ]
      "subject": {
        "reference": "urn:uuid:3bc44de3-069d-442d-829b-f3ef68cae371"
      "valueQuantity": {
        "value": 4.12,
        "unit": "10 trillion/L",
        "system": "",
        "code": "10*12/L"
    "request": {
      "method": "POST",
      "url": "Observation"
  } ]

we can replace the observation resource with the encounter , though i doubt if the fhir2 module fully supports posting encounter yet(work is under-way to support this)…

Just out of curiosity are you going to use hapifhir PLAIN or JPA server to achieve this ?


Hi @solemabrothers , you would have to construct a fhir query with include param , to include the related resources on every patient search from FHIR2 module.

If its hard that way , you would create some kind of FHIR client to construct the bundle Resource before exporting it to the Central Server .this could be the CBR module (i gues its what your using)

1 Like

Are there examples to construct a FHIR bundle within code, selecting the appropriate encounters and concepts for observations that are to be added?

@ruhanga in our LIMS integration, on the way back to OpenMRS, didn’t we build a FHIR bundle with encounter and obs?

If yes, can you point to some code that puts the bundle together?

1 Like

Encounter creation via fhir was not support by then which still is the case now I believe, so that we still depend on the ordinary rest webservices api for now. However one could query for a bundle(s) following the pattern shared by @mozzy. Below is an example to query some possible observation bundles (Observation, Encounter, Patient bundle)

or for granularity to exactly one of the observation bundles, providing a _id param would be useful.

Here are a number of resources/api endpoints supported by the openmrs fhir2 module.

1 Like

@ssmusoke we done some thing related to constructing FHIR Bundles with in the Analytics engine and in the HAPI FHIR for PLIR .

i hope that can give a crue

@ruhanga We are not trying to create resources, rather create a FHIR bundle with data for a patient and send it to an external end-point

@ssmusoke ,
@ruhanga is also trying to explain constructing FHIR bundles using the _include param

when the _include param is appended to the fhir query , it returns a FHIR bundle inclusive of the related resources

to constrain the results to only a single patient , you add the _id=patient_uuid

@mozzy you are not getting me, I want to build the FHIR bundle in code so that I can submit it to an external API, doing that via the API end-points IMO is not good since I have to do it once per patient

If I can do it in code, I can be creative with how I handle it

1 Like

Thats true , ideally i woud also prefer building it in code :100:

@ssmusoke so the data is not coming from OpenMRS?

So to my mind the ask here is a little underspecified. If the need is to get all information associated with a patient, than the best way to go about it would be to build support for the Patient/$everything operation as this is a standard way of requesting all the data related to a patient. If you want specific information, then it’s a matter of figuring out what specific data you’re interested in and how that is stored.

Similarly a “Bundle” in FHIR is a concept that can represent a number of different things. So the operation I pointed to above (and the operations to search for patients) all return a “searchset” bundle, which is the correct type of bundle to send for a search result. But bundles can also represent many other things (see Bundle.type). If you’re sending a Bundle to an external resource then you almost certainly don’t just want a searchset, but most likely either a “transaction” or “batch” type Bundle (or possibly a “message”, depending on how things are specified on the the other end). These types of Bundles differ from “searchsets” because they require an Bundle.entry.request for each entry in the bundle that specifies how it is to be handled (is it a POST? A PUT? etc.).

In the analytics engine, we do this to translate between a search bundle and a transaction bundle (at least when pushing to another FHIR store).

Not necessarily:

/ws/fhir2/R4/Patient?_id=<comma separated uuids here>&_revinclude=Encounter:patient,Observation:patient

would get you a set of patients plus the encounters and observations associated with a patient.

On the other hand, if you just have a bunch of OMRS objects and what to convert them into FHIR objects, then you should be able to get instances of any of the translator classes and use them, e.g.:

PatientTranslator patientTranslator;

// ...

List<org.openmrs.Patient> patients = ...
List<Patient> fhirPatients =;

Of course, you’ll need to handle things similarly for the other types you have available, but the translation layer of the FHIR2 module is intended to be used in this way.

From there, constructing a bundle can be handled similarly to the code from the analytics engine shared above.


@mksd The data is coming from OpenMRS, however architecturally it does not make sense for me to build a bundle via WebServices REST, then submit externally for thousands of patients

I am hoping to leverage the underlying Java API to build a bundle and submit it

@ibacher Thanks!! this is exactly what we were looking for

@solemabrothers over to you