Manage FHIR Data from Android App with OHS and Google Cloud

1. Introduction

In this codelab, we will learn how to use OHS (Open Health Stack) and Google Cloud Healthcare API to create innovative healthcare solutions that are secure, scalable, and compliant. The combination of these powerful tools provide healthcare workers and developers with the ability to create data-driven solutions that can significantly improve patient care and outcomes.

We intend to leverage the power of Open Health Stack and Google Cloud Healthcare API in a mobile app that uses Android-FHIR SDK to enable users to manage FHIR patient records in Google Cloud.

Let's dive into the implementation steps.

What you'll build

In this implementation,

  • We will use the Structured Data Capture Library to render a Questionnaire, and the FHIR Engine library to store the FHIR content of the response
  • The data will then be uploaded to the Cloud FHIR Store using the Cloud Healthcare API
  • Prior to uploading we will first authenticate ourselves using Firebase


The diagram above represents the flow. Read the blog for a detailed explanation of each component.

2. Requirements

  • A browser, such as Chrome or Firefox
  • A Google Cloud project with billing enabled
  • A recent version of Android Studio
  • An Android Emulator set up (you can use your physical Android device as well)

Create your project

  1. In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.
  2. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.
  3. Enable the necessary APIs (BigQuery, Healthcare API)

Activate Cloud Shell

  1. You will use Cloud Shell, a command-line environment running in Google Cloud that comes pre-loaded with bq:

From the Cloud Console, click Activate Cloud Shell on the top right corner:


  1. Once connected to Cloud Shell, you should see that you are already authenticated and that the project is already set to your project ID, if your project is not set, use the following command to set it:
gcloud config set project <YOUR_PROJECT_ID>

Refer documentation for gcloud commands and usage.

3. Healthcare API Setup

  1. Make sure Healthcare API is enabled: Go to the Google Cloud Console API library, search for Healthcare API, click ENABLE and it will the API and add the Healthcare Service Account to to the project
  2. Granting BigQuery admin permissions to the Service Account Execute the below gcloud command in Cloud Shell terminal to grant permission:
gcloud projects add-iam-policy-binding <<YOUR_PROJECT_ID>> \
--member=serviceAccount:service-<<PROJECT_NUMBER>>  --role=roles/bigquery.admin

Note: You can find your PROJECT_ID and PROJECT_NUMBER from the console, refer doc for identification.

Create Healthcare Dataset

In Cloud Shell, run the below command to create the Healthcare dataset:

gcloud beta healthcare datasets create <<DATASET_ID>> --location=us-central1

Set the location to a region.

Create FHIR Datastore

In Cloud Shell, run the below command to create the FHIR datastore:

gcloud beta healthcare fhir-stores create <<FHIR_STORE>> \
  --dataset=<<DATASET_ID>> --location=us-central1 --version=r4

Set the location to a region.

4. BigQuery Setup and Streaming

This covers saving FHIR Store data in BigQuery Dataset so it can be queried, programmed and analyzed leveraging the power of BigQuery and BQML.

Create BigQuery Dataset

A BigQuery dataset is a collection of tables. All tables in a dataset are stored in the same data location. You can also attach custom access controls to limit access to a dataset and its tables.

In Cloud Shell, run the below command:

bq mk --location=us-central1 --dataset <<PROJECT_ID>>:<<BQ_DATASET>>

Set the location to a region.

Create BigQuery Streaming

Streaming is required in order to export resource changes to BigQuery each time a FHIR resource is created, updated, patched or deleted in an FHIR store. Each store is allowed to have up to 10 streaming configs.

  1. Go to Google Cloud Healthcare console, browser page
  2. Click the newly created DATASET
  3. Click the newly created DATA STORE 7acaa364337086aa.png


  1. Select the newly created BigQuery Dataset from the list, Schema type as "Analytics V2" and Resource Type "Patient" from the list (You can choose as many resource types) and click Done


That's it. You are all set to save FHIR store data and stream it to BigQuery.

5. Cloud Functions (Write R4 Data to FHIR Datastore using Healthcare API)

Cloud Functions allows you to conveniently write your code and deploy to the cloud in a serverless manner. It is scalable, pay-as-you-go, event-driven and open in terms of technology and language support. Refer to the documentation for more features.

The function we will write intends to authenticate and write data that is coming in the FHIR R4 format into the FHIR Data Store using the Cloud Healthcare API. To create the Cloud Function:

  1. Go to Cloud Functions and click CREATE FUNCTION
  2. Set the name to fhir-datastore-proxy, the region to us-central1 and the Authentication option to "Require Authentication"
  3. Expand the Runtime, build, connections, and security settings. You will add five Runtime environment variables:

Name: CLOUD_FUNCTIONS_ENDPOINT | Value: the URL endpoint of the Function You will see this in Authentication block above and will be of the form:

Name: PROJECT_ID | Value: your project id

Name: DATASET_LOCATION | Value: the location of your FHIR Datastore

Name: DATASET_ID | Value: the Healthcare Datastore ID

Name: FHIR_STORE_ID | Value: the FHIR Store ID

  1. Press Next to go to the next page, where we will add our code
  2. An inline editor should now be displayed, select Java 17 as the language, and go to the class. Rename it to Don't forget to also rename the entry point to gcfv2.FhirDatastoreProxy
  3. Copy the code from the repo and paste it in the inline editor
  4. Go to the pom.xml file, and copy the pom file in the repo into the inline editor
  5. Click DEPLOY, and your function will get created and run soon


  1. The Cloud Healthcare API used in this function uses the Application Default Credential to authenticate the requests coming to the Healthcare API
  2. Save the deployed function URL to be able to invoke from the Android application So far we have created all the pieces required for the Android App to write FHIR data to BigQuery using Healthcare API independently. Now let's make sure all the pieces are connected and the API is invoked on a questionnaire submission.

6. Android Project and Firebase Setup

We will be using a recent version of Android Studio and an Android Emulator set up (you can use your physical Android device as well). Once it's ready follow the steps below:

  1. Clone the FHIR App Examples repo:
  2. Open Android Studio, select Import Project (Gradle, Eclipse ADT, etc.) and choose the cloudfunction/ folder from the source code that you have downloaded earlier.Open app/google-services.json. It is empty and needs to be filled.

Generate a SHA-1 key using ./gradlew signingReport, and take note of the SHA1 field under the debug variant

Add the app to Firebase using the Firebase Console (follow Steps 1 and 2) using the Firebase Console. In the "Debug signing certificate SHA-1 " field, fill in the value from the previous step

Download google-services.json and replace the content from Firebase into the empty file in the app folder

The Firebase SDK has already been added in the build Gradle files

  1. Open the FhirApplication.kt, and set the baseUrl field to the URL of your Cloud Function
  2. Select "Sync your project with Gradle files" from the Android Studio toolbar

We have now completed the set up and dependency inclusion portion of the implementation.

7. Questionnaire Setup and Response

We are already set up with the Questionnaire setup in the repo. But let's go over the code:

  1. Make sure the FragmentContainerView is present inside the ConstraintLayout in the app/src/main/res/layout/activity_main.xml file
  2. Make sure the QuestionnaireFragment has a JSON encoded FHIR Questionnaire to render the fragment

In this case we will be using the json - " new-patient-registration-paginated.json" to render the fragment. Check files MainActivity.kt, AddPatientFragment.kt and AddPatientViewModel.kt

Now that we have verified the fragment, json and the model, go ahead and run the app and try out entering answers for the fields in the questionnaire

You will notice the validation of string, date, numeric fields and other conditions

You can run the app and check the log to see your record created


You can learn more about using fragments here. That's pretty much it for the questionnaire fragment and response setup.

8. FHIRStore and BigQuery Dataset Result Display

Now that we are all set with setup and updates, click "Sync Project with Gradle Files". Once complete,

  1. Run the app on the Android Emulator and watch your questionnaire load
  2. Answer the questions and click submit on top
  3. You should see the message "Patient is saved" on the app

View FHIR Store Result

Navigate to Google Cloud Healthcare FHIRViewer console


Select your FHIR Store, query and hit RUN SEARCH and you should see the search results right below. 37a070480a06362c.png

View BigQuery Dataset Result

Navigate to Google Cloud BigQuery console and on the Explorer pane, go to the dataset that you created for this project.


Click Preview and you should see the same number and resulting data you found in the Healthcare FHIR Store in the BigQuery Dataset.


You can now perform BigQuery SQL, Analytics and ML on the FHIR data you just saved into the cloud dataset.

9. Search and Offline Capability

To ensure offline capability on the OHS powered Android FHIR SDK app, do make sure to follow the design guideline that needs to be enabled in the implementation we discuss here.

For searching FHIR resources, we have two ways:

  1. FHIR Viewer in the Google Cloud Console
  2. FHIR Search Method using GET or POST requests

10. App Demo

Now that our app is developed, let's play with it and see the result in the cloud.

11. Clean up

To avoid incurring charges to your Google Cloud account for the resources used in this post, follow these steps:

  1. In the Google Cloud console, go to the Manage resources page
  2. In the project list, select the project that you want to delete, and then click Delete
  3. In the dialog, type the project ID, and then click Shut down to delete the project

12. Congratulations

Congratulations! In this project, we have successfully created an Android application to store and query Patient FHIR data on cloud healthcare FHIR Store and BigQuery dataset using Cloud Healthcare API in just 7 steps:

  1. Setup the Android App
  2. Set up Google Cloud Healthcare API
  3. Created Healthcare Dataset and FHIR Datastore
  4. Created BigQuery Dataset
  5. Configured a BigQuery Stream to write FHIR Datastore data to BigQuery Dataset
  6. Deployed a Cloud Function to write R4 data to FHIR Datastore
  7. Triggered Cloud Functions from the Android App on questionnaire response submit

Now that the steps are clear, feel free to try out the same steps for the Patient FHIR edit sync to cloud.