Working with APIs for FHIR
If you're a developer, or just someone interested in coding with FHIR, then this is the page for you to read! In this tutorial we'll walk you through doing all of the tutorials you've been doing with Postman programatically.
Click the apropirate button below depending on which programming language you are intrested in. This will help this web site display the examples most relevant to your choice.
ContentsStep 1: Setup Step 2: Create a basic Patient Step 3: Upload your basic Patient Step 4: Search for a resource Step 5: Run an operation
Step 1: Setup
FHIR provides some official API code libaraies in Java and .Net to help you work with the standard.
HAPI FHIR library: To get started with HAPI, add the dependency via Maven or Gradle - or just download the library locally. If you've done the latter, include all non-hapi jars in your classpath, and only
hapi-fhir-validation-resources-dstu3-2.3.jar hapi ones if you're working with STU3.
The Java examples listed in this tutorial require Java 8 SDK installed, however HAPI itself supports Java all the way back to 6.Microsoft .Net
FHIR .NET API: The simplest way to get started with the .Net API is to search for it in the Nuget pacakge manager built into Visual Studio. Right click your Solution and select 'Manage Nuget Packages' and then search and install both 'Hl7.Fhir.STU3' and 'Hl7.FluentPath'. You may also need to select the 'Include prerelease' if you wish to have the latest STU3 release. You can find a C# project here on GitHub that contans all the C# examples seen below.
Step 2: Create a basic Patient
Upon creating a new project, you'll see a new blank class not much different from this:
Create a basic Patient resource and populate it with some information. Running this code will print the resource as XML for you:
Once run, you should see something like this:
If your IDE supports code completion, try using it to explore the Patient resource and populate it with some information such as the gender, birthday and phone number. Otherwise, consult the FHIR Patient page to see what can you populate your resource with (Javadoc is also available).
Step 3: Upload your basic Patient
Now that we can create a simple resource, let's upload it to a FHIR server:
Comparing the resource that we sent up to the resource we received back, you'll notice that the server generated a basic narrative for us from the data available and also added its logical ID
Step 4: Search for a resource
Now that you've uploaded a resource, try searching for it. Remember that you'll get a bundle back with many entries, one resource per entry.
Step 5: Run an operation
Lastly, let's execute an operation on the server. A simple operation we can do is the Patient/$everything - this'll fetch us the Patient resource and all related resources to it.
Firstly, lets upload the sample Patient we've explored before in the Patient with References tutorial:
Now we can run a query on the operation to get a Bundle with the Patient and associated resources. Here's how it looks in code. Make sure to update MY_PATIENT_ID with (press Upload first).
Try running the code and you should see the same IDs for related resources as on this page:
Patient/(press Upload first)
Encounter/(press Upload first)
Observation/(press Upload first)
Observation/(press Upload first)
DiagnosticReport/(press Upload first)
That's about it for the basics! Happy coding. Check out the HAPI documentation if you're using Java for more as well.