Skip to content

FHIR Connector Example

In this example the connector uses the FHIR REST API to interact with FHIR.

What you'll build

Given below is a sample API that illustrates how you can connect to a FHIR server and invoke operations. It exposes FHIR functionalities as a restful service. Users can invoke the API with HTTP/HTTPs with required information.

  1. /create : creates a new patient at FHIR server
  2. /read : retrieve information about the patient from FHIR server
  3. /readSpecificResourceById: map this to the scenario is it read patient by Id
  4. /update : update patient information from FHIR server.
  5. /delete : remove added patient information from FHIR server.

To know the further information about these operations please refer this link.

Note: If no ID element is provided, or the value is wrong, the server responds with a HTTP 400 error code and provides an operation outcome identifying the issue.

Before you start configuring the FHIR connector, you also need to download the relevant integration runtime of WSO2, and we refer to that location as <PRODUCT_HOME>.

Specific message builders/formatters configuration needs to be enabled in the product as shown below before starting the integration service.

If you are using EI7 or APIM 4.0.0, you need to enable this property by adding the following to the /conf/deployment.toml file. You can further refer to the Working with Message Builders and Formatters and Product Configurations documentations.

[[custom_message_builders]]
content_type = "application/fhir+json"
class = "org.wso2.micro.integrator.core.json.JsonStreamBuilder"

[[custom_message_formatters]]
content_type = "application/fhir+json"
class = "org.wso2.micro.integrator.core.json.JsonStreamFormatter"

If you are using EI 6, you can enable this property by doing the following Axis2 configurations in the \repository\conf\axis2\axis2.xml file.

messageFormatters

<messageFormatter contentType="application/fhir+json" 
class="org.wso2.carbon.integrator.core.json.JsonStreamFormatter"/>

messageBuilders

<messageBuilder contentType="application/fhir+json" 
class="org.wso2.carbon.integrator.core.json.JsonStreamBuilder"/>

The following diagram illustrates all the required functionality of the FHIR API service that you are going to build.

In here FHIR clients can invoke the API with HTTP/HTTPs with required information. The FHIR connector exposes each request to converting to the Health Level Seven International standards and then send to the resources available in the FHIR server.

This server is regularly loaded with a standard set of test data sets and also this server can store any data that related to administrative concepts such as patients, providers, organizations and devices, as well as a variety of clinical concepts including problems, medications, diagnostics, care plans and financial issues, among others.

FHIR Connector

Configure the connector in WSO2 Integration Studio

Follow these steps to set up the Integration Project and the Connector Exporter Project.

  1. Open WSO2 Integration Studio and create an Integration Project. Creating a new Integration Project

  2. Right-click the project that you created and click on Add or Remove Connector -> Add Connector. You will get directed to the WSO2 Connector Store.

  3. Search for the specific connector required for your integration scenario and download it to the workspace. Search Connector in the Connector Store

  4. Click Finish, and your Integration Project is ready. The downloaded connector is displayed on the side palette with its operations.

  5. You can drag and drop the operations to the design canvas and build your integration logic. Drag connector operations

  6. Right click on the created Integration Project and select New -> Rest API to create the REST API.

  7. Right click on the created Integration Project and select New -> Rest API to create the REST API.

  8. Specify the API name as SendisoTestAPI and API context as /resources. You can go to the source view of the XML configuration file of the API and copy the following configuration (source view).

<?xml version="1.0" encoding="UTF-8"?>
<api context="/resources" name="SampleApi" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST" url-mapping="/create">
        <inSequence>
            <property expression="json-eval($.base)" name="base" scope="default" type="STRING"/>
            <property expression="json-eval($.resourceType)" name="type" scope="default" type="STRING"/>
            <property expression="json-eval($.format)" name="format" scope="default" type="STRING"/>
            <log level="custom">
                <property expression="get-property('transport','Content-Type')" name="base"/>
            </log>
            <fhir.init>
                <base>http://hapi.fhir.org/baseR4</base>
            </fhir.init>
            <switch source="get-property('transport','Content-Type')">
                <case regex="application/json">
                    <property name="format" scope="default" type="STRING" value="json"/>
                    <fhir.create>
                        <type>{$ctx:type}</type>
                        <format>{$ctx:format}</format>
                    </fhir.create>
                </case>
                <case regex="application/xml">
                    <property name="format" scope="default" type="STRING" value="xml"/>
                    <fhir.create>
                        <type>{$ctx:type}</type>
                        <format>{$ctx:format}</format>
                    </fhir.create>
                </case>
                <default/>
            </switch>
            <log level="full" separator=","/>
            <send/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
    <resource methods="POST" url-mapping="/read">
        <inSequence>
            <property expression="json-eval($.base)" name="base" scope="default" type="STRING"/>
            <property expression="json-eval($.resourceType)" name="type" scope="default" type="STRING"/>
            <property expression="json-eval($.format)" name="format" scope="default" type="STRING"/>
            <fhir.init>
                <base>http://hapi.fhir.org/baseR4</base>
            </fhir.init>
            <switch source="get-property('transport','Content-Type')">
                <case regex="application/json">
                    <property name="format" scope="default" type="STRING" value="json"/>
                    <fhir.readResource>
                        <type>{$ctx:type}</type>
                        <format>{$ctx:format}</format>
                    </fhir.readResource>
                </case>
                <case regex="application/xml">
                    <property name="format" scope="default" type="STRING" value="xml"/>
                    <fhir.readResource>
                        <type>{$ctx:type}</type>
                        <format>{$ctx:format}</format>
                    </fhir.readResource>
                </case>
                <default/>
            </switch>
            <log level="full" separator=","/>
            <send/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
    <resource methods="POST" url-mapping="/readSpecificResourceById">
        <inSequence>
            <property expression="json-eval($.base)" name="base" scope="default" type="STRING"/>
            <property expression="json-eval($.resourceType)" name="type" scope="default" type="STRING"/>
            <property expression="json-eval($.format)" name="format" scope="default" type="STRING"/>
            <property expression="json-eval($.id)" name="id" scope="default" type="STRING"/>
            <property expression="json-eval($.summary)" name="summary" scope="default" type="STRING"/>
            <fhir.init>
                <base>http://hapi.fhir.org/baseR4</base>
            </fhir.init>
            <switch source="get-property('transport','Content-Type')">
                <case regex="application/json">
                    <property name="format" scope="default" type="STRING" value="json"/>
                    <fhir.readSpecificResourceById>
                        <type>{$ctx:type}</type>
                        <id>{$ctx:id}</id>
                        <format>{$ctx:format}</format>
                        <summary>{$ctx:summary}</summary>
                    </fhir.readSpecificResourceById>
                </case>
                <case regex="application/xml">
                    <property name="format" scope="default" type="STRING" value="xml"/>
                    <fhir.readSpecificResourceById>
                        <type>{$ctx:type}</type>
                        <id>{$ctx:id}</id>
                        <format>{$ctx:format}</format>
                        <summary>{$ctx:summary}</summary>
                    </fhir.readSpecificResourceById>
                </case>
                <default/>
            </switch>
            <log level="full" separator=","/>
            <send/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
    <resource methods="POST" url-mapping="/update">
        <inSequence>
            <property expression="json-eval($.base)" name="base" scope="default" type="STRING"/>
            <property expression="json-eval($.resourceType)" name="type" scope="default" type="STRING"/>
            <property expression="json-eval($.format)" name="format" scope="default" type="STRING"/>
            <property expression="json-eval($.idToUpdate)" name="idToDelete" scope="default" type="STRING"/>
            <fhir.init>
                <base>http://hapi.fhir.org/baseR4</base>
            </fhir.init>
            <switch source="get-property('transport','Content-Type')">
                <case regex="application/json">
                    <property name="format" scope="default" type="STRING" value="json"/>
                    <fhir.update>
                        <type>{$ctx:type}</type>
                        <idToUpdate>{$ctx:idToUpdate}</idToUpdate>
                        <format>{$ctx:format}</format>
                    </fhir.update>
                </case>
                <case regex="application/xml">
                    <property name="format" scope="default" type="STRING" value="xml"/>
                    <fhir.update>
                        <type>{$ctx:type}</type>
                        <idToUpdate>{$ctx:idToUpdate}</idToUpdate>
                        <format>{$ctx:format}</format>
                    </fhir.update>
                </case>
                <default/>
            </switch>
            <log level="full" separator=","/>
            <send/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
    <resource methods="POST" url-mapping="/delete">
        <inSequence>
            <property expression="json-eval($.base)" name="base" scope="default" type="STRING"/>
            <property expression="json-eval($.resourceType)" name="type" scope="default" type="STRING"/>
            <property expression="json-eval($.format)" name="format" scope="default" type="STRING"/>
            <property expression="json-eval($.idToDelete)" name="idToDelete" scope="default" type="STRING"/>
            <fhir.init>
                <base>http://hapi.fhir.org/baseR4</base>
            </fhir.init>
            <switch source="get-property('transport','Content-Type')">
                <case regex="application/json">
                    <property name="format" scope="default" type="STRING" value="json"/>
                    <fhir.delete>
                        <type>{$ctx:type}</type>
                        <idToDelete>{$ctx:idToDelete}</idToDelete>
                    </fhir.delete>
                </case>
                <case regex="application/xml">
                    <property name="format" scope="default" type="STRING" value="xml"/>
                    <fhir.delete>
                        <type>{$ctx:type}</type>
                        <idToDelete>{$ctx:idToDelete}</idToDelete>
                    </fhir.delete>
                </case>
                <default/>
            </switch>
            <log level="full" separator=","/>
            <send/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>
To learn about supported operations and their parameters, please refer to FHIR connector reference.

  1. Now we can export the imported connector and the API into a single CAR application. The CAR application is what we are going to deploy during server runtime.

Exporting Integration Logic as a CApp

CApp (Carbon Application) is the deployable artifact on the integration runtime. Let us see how we can export integration logic we developed into a CApp along with the connector.

Creating Connector Exporter Project

To bundle a Connector into a CApp, a Connector Exporter Project is required.

  1. Navigate to File -> New -> Other -> WSO2 -> Extensions -> Project Types -> Connector Exporter Project.

    Add Connector Exporter Project

  2. Enter a name for the Connector Exporter Project.

  3. In the next screen select, Specify the parent from workspace and select the specific Integration Project you created from the dropdown.
    Naming Connector Exporter Project

  4. Now you need to add the Connector to Connector Exporter Project that you just created. Right-click the Connector Exporter Project and select, New -> Add Remove Connectors -> Add Connector -> Add from Workspace -> Connector

  5. Once you are directed to the workspace, it displays all the connectors that exist in the workspace. You can select the relevant connector and click Ok.

    Selecting Connector from Workspace

Creating a Composite Application Project

To export the Integration Project as a CApp, a Composite Application Project needs to be created. Usually, when an Integration project is created, this project can be created as part of that project by Integration Studio. If not, you can specifically create it by navigating to File -> New -> Other -> WSO2 -> Distribution -> Composite Application Project.

Exporting the Composite Application Project

  1. Right-click the Composite Application Project and click Export Composite Application Project.

    Export as a Carbon Application

  2. Select an Export Destination where you want to save the .car file.

  3. In the next Create a deployable CAR file screen, select both the created Integration Project and the Connector Exporter Project to save and click Finish. The CApp is created at the specified location provided at the previous step.

    Create a deployable CAR file

Get the project

You can download the ZIP file and extract the contents to get the project code.

Download ZIP

Deployment

Follow these steps to deploy the exported CApp in the integration runtime.

Deploying on Micro Integrator

You can copy the composite application to the <PRODUCT-HOME>/repository/deployment/server/carbonapps folder and start the server. Micro Integrator will be started and the composite application will be deployed.

You can further refer the application deployed through the CLI tool. See the instructions on managing integrations from the CLI.

Click here for instructions on deploying on WSO2 Enterprise Integrator 6
  1. You can copy the composite application to the <PRODUCT-HOME>/repository/deployment/server/carbonapps folder and start the server.

  2. WSO2 EI server starts and you can login to the Management Console https://localhost:9443/carbon/ URL. Provide login credentials. The default credentials will be admin/admin.

  3. You can see that the API is deployed under the API section.

Testing

Invoke the API as shown below using the curl command. Curl Application can be downloaded from here.

Add patient information

Sample Request

curl -v POST -d 
   '  {  "resourceType": "Patient",
           "name": [{"family": "Jhone","given": ["Winney","Rodrigo"]}]
      }' "http://localhost:8290/resources/create" -H "Content-Type:application/json" 
Expected Response:

<jsonObject>
       <resourceType>Patient</resourceType>
       <id>698021</id>
        <meta>
          <versionId>1</versionId>
          <lastUpdated>2020-03-24T07:57:14.506+00:00</lastUpdated>
        </meta>
         <text>
           <status>generated</status>
           <div>&lt;div xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;div class="hapiHeaderText"&gt;Winney Rodrigo &lt;b&gt;JHONE &lt;/b&gt;&lt;/div&gt;&lt;table class="hapiPropertyTable"&gt;&lt;tbody/&gt;&lt;/table&gt;&lt;/div&gt;</div>
         </text>    
         <name>
            <family>Jhone</family>
            <given>Winney</given>
            <given>Rodrigo</given>
        </name>
</jsonObject>

Read patient information

Sample Request

curl -v POST -d 
   '  {  
        "resourceType": "Patient"
      }' "http://localhost:8290/resources/read" -H "Content-Type:application/json"        

Expected Response:

It will retrieve all the existing resources in the FHIR server.

Read specific patient information

Sample Request

curl -v POST -d 
   '{
       "resourceType":"Patient",
       "id":"698021"
    }' "http://localhost:8290/resources/readSpecificResourceById" -H "Content-Type:application/json"            

Expected Response:

<jsonObject>
    <resourceType>Patient</resourceType>
    <id>698021</id>
    <meta>
        <versionId>1</versionId>
        <lastUpdated>2020-03-24T07:57:14.506+00:00</lastUpdated>
    </meta>
    <text>
        <status>generated</status>
        <div>&lt;div xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;div class="hapiHeaderText"&gt;Winney Rodrigo &lt;b&gt;JHONE &lt;/b&gt;&lt;/div&gt;&lt;table class="hapiPropertyTable"&gt;&lt;tbody/&gt;&lt;/table&gt;&lt;/div&gt;</div>
        </text>
               <name>
                  <family>Jhone</family>
                  <given>Winney</given>
                  <given>Rodrigo</given>
               </name>
</jsonObject>

Update patient information

Sample Request

curl -v POST -d 
         '{
             "resourceType":"Patient",
             "idToUpdate":"597079",
             "name":[
                {
                   "family":"Marry",
                   "given":[
                      "Samsong",
                      "Perera"
                   ]
                }
             ]
          }' "http://localhost:8290/resources/update" -H "Content-Type:application/json"   

Expected Response:

<jsonObject>
         <resourceType>Patient</resourceType>
         <id>597079</id>
         <meta>
            <versionId>1</versionId>
            <lastUpdated>2020-03-24T07:57:14.506+00:00</lastUpdated>
         </meta>
         <text>
            <status>generated</status>
            <div>&lt;div xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;div class="hapiHeaderText"&gt;Winney Rodrigo &lt;b&gt;JHONE &lt;/b&gt;&lt;/div&gt;&lt;table class="hapiPropertyTable"&gt;&lt;tbody/&gt;&lt;/table&gt;&lt;/div&gt;</div>
         </text>
         <name>
            <family>Marry</family>
            <given>Samsong</given>
            <given>Perera</given>
         </name>
</jsonObject>

Delete patient information

Sample Request

curl -v POST -d 
   '{
      "resourceType":"Patient",
      "idToDelete":"597079",
    }' "http://localhost:8290/resources/delete" -H "Content-Type:application/json" 

Expected Response:

<jsonObject>
         <resourceType>OperationOutcome</resourceType>
         <text>
            <status>generated</status>
            <div>&lt;div xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;h1&gt;Operation Outcome&lt;/h1&gt;&lt;table border="0"&gt;&lt;tr&gt;&lt;td style="font-weight: bold;"&gt;INFORMATION&lt;/td&gt;&lt;td&gt;[]&lt;/td&gt;&lt;td&gt;&lt;pre&gt;Successfully deleted 1 resource(s) in 46ms&lt;/pre&gt;&lt;/td&gt;


                            &lt;/tr&gt;
                        &lt;/table&gt;
                    &lt;/div&gt;</div>
         </text>
         <issue>
            <severity>information</severity>
            <code>informational</code>
            <diagnostics>Successfully deleted 1 resource(s) in 46ms</diagnostics>
         </issue>
</jsonObject>

This demonstrates how the WSO2 FHIR connector works.