How to interact with FHIR REST APIs

In this tutorial, we will see how to POST the Patient resource we created in the last tutorial.

Which FHIR server to use?

The FHIR documentation lists the publicly available FHIR servers for testing. You can pick one from here or use the Medblocks Students Server.

How to POST a Patient resource?

To POST a resource, you will first need a tool to generate and execute HTTP requests. Tools like Insomnia or Postman can help you here.

You’ll notice that the resource we created doesn’t have an id, which is a mandatory field for the Patient resource. This is because the id will be generated by the server.

What to note when POST-ing your first resource:

  • Select ‘POST’ as your request method
  • Enter the base URL of the FHIR server you’re using, and append ‘/Patient’
  • In the Body of the HTTP request, select JSON format and enter the resource we created earlier.

When you hit Send, you will receive a 201 Created response, with the id populated.

Post fhir resource

You can use the id to retrieve your patient from the server, by changing your HTTP method to GET, and appending the id to the end of the URL, e.g. http://hapi.fhir.org/baseR4/Patient/48193284.

Next we’ll create an Observation that maps to this Patient, and POST that too.

How to POST an Observation Resource

Here we have a sample Observation resource that has been validated in the same way. Remember to replace the reference with the id of the Patient resource you just created.

{
    "resourceType": "Observation",
    "status": "final",
    "code": {
        "coding": [
            {
                "system": "http://loinc.org",
                "code": "15074-8",
                "display": "Glucose [Moles/volume] in Blood"
            }
        ]
    },
    "subject": {
        "reference": "Patient/48193284",
        "display": "John Doe"
    },
    "valueQuantity": {
        "value": 6.3,
        "unit": "mmol/L",
        "system": "http://unitsofmeasure.org",
        "code": "mmol/L"
    }
}

And POST this to the base URL with ‘/Observations’ appended. 
POST observation resource

Editing a FHIR resource

Another common function is to edit the resource, in our case, let’s edit the Observation resource to include an updated value.

Here are a few things to consider, Change the HTTP request method to PUT Add the resource ID to the body of the updated Observation resource Add the resource ID to the URL

Here is the updated observation,

{
    "resourceType": "Observation",
    "id": "48193345",
    "status": "corrected",
    "code": {
        "coding": [
            {
                "system": "http://loinc.org",
                "code": "15074-8",
                "display": "Glucose [Moles/volume] in Blood"
            }
        ]
    },
    "subject": {
        "reference": "Patient/48193284",
        "display": "John Doe"
    },
    "valueQuantity": {
        "value": 4.2,
        "unit": "mmol/L",
        "system": "http://unitsofmeasure.org",
        "code": "mmol/L"
    }
}

And here is the result, 
Edited observation

To see how the resource has changed over time, you can append _history to the URL.