Integrate DrChrono With MuleSoft Using OAuth 2.0

Author: Prince Kumar

Introduction to DrChrono

DrChrono is a leading electronic health record (EHR) platform that streamlines practice management and patient care. With its cloud-based EHR system, healthcare professionals can securely store and access patient records digitally, eliminating paper-based inefficiencies. DrChrono offers robust scheduling, e-prescribing, and billing features, optimizing resource utilization and improving medication accuracy. The platform integrates with third-party applications for enhanced functionality.

Use Case

In today’s use case, we will delve into the seamless integration of DrChrono, a cutting-edge electronic health record (EHR) platform, with MuleSoft, a robust integration platform. This integration will be accomplished using the industry-standard OAuth 2.0 authentication protocol, ensuring secure and efficient communication between the two systems.

Create a free DrChrono developer account

Set up a free developer account in DrChrono by visiting this link: https://www.drchrono.com/sign-up/

Set up your application

After completely setting up your account, let’s create the application.

Go to Account → API as shown below and click on ➕ New Application.

As you can see, I have already created an application by the name of ChronoTest. Don’t forget to add some redirect URIs (extremely important).

Scroll down the page and hit ‘Save Changes’.

Now you have the client ID and client secret of your application in your hand. Before jumping into the integration part, let’s play with DrChrono REST API in the Postman client.

Play with DrChrono REST API in Postman 

Start a new request in Postman for the list of doctors.

Method → GET

Request → https://app.drchrono.com/api/doctors 

Choose the Authorization tab and select Oauth 2.0.

Auth URL → https://drchrono.com/o/authorize/

Access Token URL → https://drchrono.com/o/token/

Client ID â†’ your client id

Client Secret → your client’s secret.

Before hitting the send button for this request, you should first scroll below in the Authorization tab. After selecting OAuth 2.0, there is a button called ‘Get New Access Token’. Click on it.

After that, a new window will appear, as shown below. You just need to authorize your app.

After authorization, a new pop-up window will appear, as shown below. You just need to click on ‘Open Postman’.

After that, Postman will show you the necessary information for the token it grabs from DrChrono. It contains the access token, refresh token, expiration time, and necessary scope.

Keep the refresh token somewhere. We will be using this refresh token at a later point in time.

Important Notes: Make sure that this callback URL of Postman (https://oauth.pstmn.io/v1/callback) is registered with the ChronoTest App that we have built above in DrChrono. After completing these necessary steps, you should now hit the send button, and you will get 200 OK responses like this.

Till now, we have learned how to create applications in DrChrono and how to make calls to its REST API through Postman. Now, we will go straight ahead in Anypoint Studio and create a Mule project.

Let’s Integrate DrChrono with MuleSoft

As you can see, I have created a Mule project and named it ‘drchrono-api’ that has three Mule configuration files, as shown below.

Also, create a YAML file in the config folder in src/main/resources.

http:
 port: "8081"

requesttoken:
  config:
    protocol: "HTTPS"
    host: "drchrono.com"
    port: "443"
    path: "/o/token/"
  grant_type: "refresh_token"
  refresh_token: "your_refresh_token"
  code_base_encoded: "your base encoded form of client id and client secret"
  
drchrono-api:
   protocol: "HTTPS"
   host:  "app.drchrono.com"
   port: "443"   

For the code base encoded form of the client ID and client secret, you should visit: https://www.base64encode.org/

Here, paste your client ID and client secret in this format.

The format for base encoded form

client_id:client_secret

Remember, there is a colon in between them. You can also do the same via your Git Bash by using this command.

echo -n 'your_client_id:your_client_secret' | base64

Let’s discuss and talk about the src/main/mule XML file.

  1. implementation.xml

Listener component: It has /patient in their path.

Flow reference: It actually refers to the access-token-flow that is in commons-service.xml. We’ll talk more about this later.

Requester component: It actually requests DrChrono for the creation of patients.

Requester Headers:

Don’t worry about vars.access_token here. It actually came from a commons-service.xml file. I will talk more about this later.

Transform Message:

Here, just convert the payload received from DrChrono to JSON form.

  • commons-service.xml file.

Let’s look at the overall implementation first.

Above is the overall view of the commons-service.xml file. Now we will see the components and their configurations one by one, and also explain wherever necessary.

Retrieve the component of the Object Store for the access token

Also, check out its Advanced section where I’m using the target variable name ‘access_TokenStore’.

Retrieve the component of an Object store for a refresh token

Check out its Advanced section too. Here, the target variable is ‘refresh_TokenStore’.

Transform Component:

Here we have two variables set up:

%dw 2.0
output application/json
---
"Basic " ++ Mule::p('requesttoken.code_base_encoded')
  • requestRefreshTokenBody
%dw 2.0
output application/json
---
if (vars.refresh_TokenStore != Mule::p('requesttoken.refresh_token')) vars.refresh_TokenStore
else Mule::p('requesttoken.refresh_token')

Now we are in the Choice section. Here, we are checking if my access token is expired.

Condition:

vars.access_TokenStore != "0" is true ?

If true, then we have a logger component which simply logs the message as ‘Access Token Available’.

Now, in the default section, we have a requester component inside the ‘Until Successful’ scope, and you can set it according to your needs, like setting the max entries. Let’s check out the requester component configuration.

Body

%dw 2.0
output application/x-www-form-urlencoded
---
{
    "grant_type" : Mule::p('requesttoken.grant_type'),
    "refresh_token" : vars.requestRefreshTokenBody
}

Headers

output application/java
---
{
	"Authorization" : vars.code_base64Encoded,
	"Accept" : "application/json",
	"Content-Type" : "application/x-www-form-urlencoded"
}

In the Advanced tab of the requester component, set the target value to ‘AccessTokenforDrChrono’, as specified in the screenshot.

Store functionality of the Object Store for the access token. Store a new access token that we have recently received.

Store functionality of the Object Store for a refresh token.

After that, we have a choice router just to check if the access token is still zero.

Condition:

vars.access_TokenStore != "0"

If the above condition is true, then I’m setting a variable named ‘access_token’, and its value is taken from the object store.

%dw 2.0
output application/java
---
"Bearer " ++ vars.access_TokenStore as String.

If the above condition is not true, then we have a default section in which again I’m setting the same variable ‘access_token’. This time, its value is taken from the variable ‘AccessTokenforDrChrono’.

%dw 2.0
output application/json
---
"Bearer " ++ vars.AccessTokenforDrChrono.access_token as String

And at last, we have a logger component which logs the message.

“Exit from post-access-token-flow”

  • global-config.xml file

Let’s look at the overall configuration.

HTTP request configuration for access token.

Object store configuration for a refresh token.

Object-store-for-Access-Token

Its configuration is the same as above.

HTTP request configuration for DrChrono API:

Configuration for configuration-properties:

Now, run the Mule application and go to Postman to create a new request for creating a patient in DrChrono.

Method → POST

Request → http://localhost:8081/patient

Request body in JSON format.

{
    "doctor" : "327507",
    "gender" : "Male",
    "first_name" : "Mulesoft",
    "last_name" : "User",
    "email" : "example@gmail.com"
}

Let’s hit the send button.

You will certainly get a response like below.

References:
  1. https://app.drchrono.com/api-docs/
  2. https://app.drchrono.com/api-docs-old/v4/documentation#

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.