4.5.1Client Examples


This page contains examples of how to use the client to perform complete tasks. If you have an example you could contribute, we'd love to hear from you!

4.5.2Transaction With Conditional Create


The following example demonstrates a common scenario: How to create a new piece of data for a Patient (in this case, an Observation) where the identifier of the Patient is known, but the ID is not.

In this scenario, we want to look up the Patient record and reference it from the newly created Observation. In the event that no Patient record already exists with the given identifier, a new one will be created and the Observation will reference it. This is known in FHIR as a Conditional Create.


  "resourceType": "Bundle",
  "type": "transaction",
  "entry": [ {
    "fullUrl": "urn:uuid:3bc44de3-069d-442d-829b-f3ef68cae371",
    "resource": {
      "resourceType": "Patient",
      "identifier": [ {
        "system": "http://acme.org/mrns",
        "value": "12345"
      } ],
      "name": [ {
        "family": "Jameson",
        "given": [ "J", "Jonah" ]
      } ],
      "gender": "male"
    "request": {
      "method": "POST",
      "url": "Patient",
      "ifNoneExist": "identifier=http://acme.org/mrns|12345"
  }, {
    "resource": {
      "resourceType": "Observation",
      "status": "final",
      "code": {
        "coding": [ {
          "system": "http://loinc.org",
          "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": "http://unitsofmeasure.org",
        "code": "10*12/L"
    "request": {
      "method": "POST",
      "url": "Observation"
  } ]


<Bundle xmlns="http://hl7.org/fhir">
   <type value="transaction"/>
      <fullUrl value="urn:uuid:47709cc7-b3ec-4abc-9d26-3df3d3d57907"/>
         <Patient xmlns="http://hl7.org/fhir">
               <system value="http://acme.org/mrns"/>
               <value value="12345"/>
               <family value="Jameson"/>
               <given value="J"/>
               <given value="Jonah"/>
            <gender value="male"/>
         <method value="POST"/>
         <url value="Patient"/>
         <ifNoneExist value="identifier=http://acme.org/mrns|12345"/>
         <Observation xmlns="http://hl7.org/fhir">
            <status value="final"/>
                  <system value="http://loinc.org"/>
                  <code value="789-8"/>
                  <display value="Erythrocytes [#/volume] in Blood by Automated count"/>
               <reference value="urn:uuid:47709cc7-b3ec-4abc-9d26-3df3d3d57907"/>
               <value value="4.12"/>
               <unit value="10 trillion/L"/>
               <system value="http://unitsofmeasure.org"/>
               <code value="10*12/L"/>
         <method value="POST"/>
         <url value="Observation"/>

The server responds with the following response. Note that the ID of the already existing patient is returned, and the ID of the newly created Observation is too.

<Bundle xmlns="http://hl7.org/fhir">
   <id value="dd1f75b8-e472-481e-97b3-c5eebb99a5e0"/>
   <type value="transaction-response"/>
      <relation value="self"/>
      <url value="http://fhirtest.uhn.ca/baseDstu2"/>
         <status value="200 OK"/>
         <location value="Patient/966810/_history/1"/>
         <etag value="1"/>
         <lastModified value="2015-10-29T07:25:42.465-04:00"/>
         <status value="201 Created"/>
         <location value="Observation/966828/_history/1"/>
         <etag value="1"/>
         <lastModified value="2015-10-29T07:33:28.047-04:00"/>

To produce this transaction in Java code:

// Create a patient object
Patient patient = new Patient();

// Give the patient a temporary UUID so that other resources in
// the transaction can refer to it

// Create an observation object
Observation observation = new Observation();
      .setDisplay("Erythrocytes [#/volume] in Blood by Automated count");
observation.setValue(new Quantity()
      .setUnit("10 trillion/L")

// The observation refers to the patient using the ID, which is already
// set to a temporary UUID
observation.setSubject(new Reference(patient.getIdElement().getValue()));

// Create a bundle that will be used as a transaction
Bundle bundle = new Bundle();

// Add the patient as an entry. This entry is a POST with an
// If-None-Exist header (conditional create) meaning that it
// will only be created if there isn't already a Patient with
// the identifier 12345

// Add the observation. This entry is a POST with no header
// (normal create) meaning that it will be created even if
// a similar resource already exists.

// Log the request
FhirContext ctx = FhirContext.forR4();

// Create a client and post the transaction to the server
IGenericClient client = ctx.newRestfulGenericClient("http://hapi.fhir.org/baseR4");
Bundle resp = client.transaction().withBundle(bundle).execute();

// Log the response

4.5.3Fetch all Pages of a Bundle


This following example shows how to load all pages of a bundle by fetching each page one-after-the-other and then joining the results.

// Create a context and a client
FhirContext ctx = FhirContext.forR4();
String serverBase = "http://hapi.fhr.org/baseR4";
IGenericClient client = ctx.newRestfulGenericClient(serverBase);

// We'll populate this list
List<IBaseResource> patients = new ArrayList<>();

// We'll do a search for all Patients and extract the first page
Bundle bundle = client.search()
patients.addAll(BundleUtil.toListOfResources(ctx, bundle));

// Load the subsequent pages
while (bundle.getLink(IBaseBundle.LINK_NEXT) != null) {
   bundle = client.loadPage().next(bundle).execute();
   patients.addAll(BundleUtil.toListOfResources(ctx, bundle));

System.out.println("Loaded " + patients.size() + " patients!");

4.5.4Create Composition and Generate Document


This example shows how to generate a Composition resource with two linked resources, then apply the server $document operation to generate a document based on this composition.

FhirContext ctx = FhirContext.forR4();
IGenericClient client = ctx.newRestfulGenericClient("http://hapi.fhir.org/baseR4");

Patient patient = new Patient();

Observation observation = new Observation();
observation.setSubject(new Reference("Patient/PATIENT-ABC"));

Composition composition = new Composition();
composition.setSubject(new Reference("Patient/PATIENT-ABC"));
composition.addSection().setFocus(new Reference("Observation/OBSERVATION-ABC"));

Bundle document = client.operation()

      "Document bundle: {}", ctx.newJsonParser().setPrettyPrint(true).encodeResourceToString(document));