Connecting with Epic EMR systems
In this example we will look into the steps how can we use the Client Connector to connect with the Epic EMR systems.
Prerequisites¶
-
First we need to create Backend OAuth 2.0 App. To use the client_credentials OAuth 2.0 grant type to authorize the backend application's access to patient information we need 'Client ID' and the 'Public key'. Let's create a Public Private Key Pair using OpenSSL.
Creating the private key
openssl genrsa -out <path_to_key>/privatekey.pem 2048
Creating the public key
openssl req -new -x509 -key <path_to_key>/privatekey.pem -out <path_to_key>/publickey509.pem -subj '/CN=myapp'
-
Login to EPIC FHIR with your credentials.
- Navigate to 'Build Apps' tab to create an application.
- Provide the details to create the application.
- Application Name: myapp
- Application Audience: Backend Systems
- Incoming APIs: Patient.Read(R4)
- Production JWK Set URL: fhir.epic.com
- Upload your created public key in step 1.
You can further refer to the Epic Documentation on creating an OAuth 2.0 App.
Note
WSO2 Healthcare Solution offers two distinct solutions (Ballerina and Micro Integrator) each tailored to different architectural preferences. For systems that favor the Enterprise Service Bus (ESB) pattern, the Micro Integrator, enhanced with FHIR accelerators, can be utilized.
On the other hand, Ballerina, a programming language specifically designed for integrations, is ideal for implementing FHIR integrations structured as microservices.
Step 1: Set Up Ballerina¶
Before you begin, ensure you have Ballerina installed on your system. Follow the instructions in the Installation Steps to install Ballerina and set up the development environment.
Step 2: Implement the flow to connect with Epic EMR¶
-
Create a new ballerina project.
bal new epicapp
-
In the main.bal file provide the following code. Make sure to provide clientId and path to the private key file you obtained in the Prerequisites step.
import ballerina/http; import ballerinax/health.base.auth; import ballerinax/health.clients.fhir; configurable string base = "https://fhir.epic.com/interconnect-fhir-oauth/api/FHIR/R4"; configurable string tokenUrl = "https://fhir.epic.com/interconnect-fhir-oauth/oauth2/token"; configurable string clientId = "<CLIENT-ID>"; configurable string keyFile = "resources/privatekey.pem"; // Create PKJWTAuth configuration auth:PKJWTAuthConfig ehrSystemAuthConfig = { keyFile: keyFile, clientId: clientId, tokenEndpoint: tokenUrl }; fhir:FHIRConnectorConfig epicConfig = { baseURL: base, authConfig: ehrSystemAuthConfig }; final fhir:FHIRConnector fhirConnectorObj = check new (epicConfig); service http:Service / on new http:Listener(9090) { // Get resource by ID isolated resource function get fhir/r4/[string resType]/[string id]() returns http:Response { fhir:FHIRResponse|fhir:FHIRError fhirResponse = fhirConnectorObj->getById(resType, id); return fhir:handleResponse(fhirResponse); } }
-
Run the Ballerina service.
bal run
-
Test the service using the following curl command.
curl --location 'http://localhost:9090/fhir/r4/Patient/e63wRTbPfr1p8UW81d8Seiw3'
The following sample demonstrates how we can use the Epic Connector to connect with Epic Server and retrive specific patient's information.
Step 1: Set Up WSO2 Micro Integrator¶
Before you begin, download the WSO2 Micro Integrator and install by following the Installation Steps.
Step 2: Implement the flow to connect with Epic EMR¶
- Open VSCode, type CMD+Shift+P and type MI:Create New Project.
- To create an API, click on the API button, for URI Template provide '/getPatient/{patientId}' and select 'GET' as the resource.
- Download epic-connector zip from WSO2 Connector Store
- Go to Project Explorer and navigate to src->main-> wso2mi-> resources-> connectors. Add the downloaded connector zip file here.
- Go to Micro Integrator view and click on 'Add API'. For 'context' provide '/epic'
- Then you will be navigated to the Resource view. Click on the + button to add a connector operation.
- Select 'Connector' and search for 'Epic', and select the 'Epic Connector'.
- Click on the 'init' operation. Provide values for 'Base URL', 'Client ID', 'Token URL' and 'Private Key' feilds and click on the Submit button.
- Then click on 'ReadbyId' operation. Provide values for 'Type' and 'ID'. We are going to get the PatientId as a path parameter.
Type: Patient ID: {$ctx:uri.var.patientId}
- Next add a Respond mediator.
- The complete source looks as below.
<?xml version="1.0" encoding="UTF-8"?> <api context="/epic" name="epic" xmlns="http://ws.apache.org/ns/synapse"> <resource methods="GET" uri-template="/getPatient/{patientId}"> <inSequence> <epicFhirR4.init> <base>https://fhir.epic.com/interconnect-fhir-oauth/api/FHIR/R4</base> <clientId>[CLIENT-ID]</clientId> <tokenEndpoint>https://fhir.epic.com/interconnect-fhir-oauth/oauth2/token</tokenEndpoint> <privateKey>[PRIVATE-KEY-VALUE]</privateKey> <enableUrlRewrite>False</enableUrlRewrite> </epicFhirR4.init> <epicFhirR4.readById> <type>Patient</type> <id>{$ctx:uri.var.patientId}</id> </epicFhirR4.readById> <respond/> </inSequence> <faultSequence> </faultSequence> </resource> </api>
- Click on 'Build and Run' button in the top left corner. The Micro Integrator server should start running.
-
Clik on 'Try it' and provide the value for the Patient ID. A sample curl can be found below.
curl --location 'http://localhost:8290/epic/getPatient/e63wRTbPfr1p8UW81d8Seiw3'
-
You should be able to get a successful FHIR Patient payload.