— 5 min read

Uploading Synthetic Patient Data to a FHIR server using Synthea and Python

I created a small python script to upload patient resources from bundle json files into a fhir server. When prototyping fhir apps I have normally used public fhir servers which are widely available. Here is a good list of publicly available fhir servers. However, most of these servers reset their data periodically. They also might not be always available. I decided to deploy a local fhir server so I have more control and can experiment with writing data and persisting more complex patient examples.


HAPI on fhir is a great out of the box fhir server that you can host locally. You can find the github page here. It has great documentation. In addition to a fhir server it also provides a UI so you can interactively explore the data and perform CRUD operations.

Create Patient Data With Synthea

I quickly realized that manually creating patients and other fhir resources would be tedious and not clinically relevant. So I found Synthea which is a great open source tool to generate realistic patient data. The great part is that it outputs json files in fhir r4 bundle transaction format. These bundle resources allow for multiple transactions to be applied to the fhir server in a bulk fashion. For example this means by making just one api call I can upload patient name information but also all their observation and medication resources.

I used Synthea to generate 1000 patient examples. This is outputted in 1000 patient files. The next task was how I could upload all of these files to my local hapi fhir server. So I was exploring the hapi ui and found a place where bundle transactions could be uploaded. This worked great with uploading all the resources for one patient. However, I was not going to manually do this for each patient. There was not a way to do a bulk upload of all patients.

Pyhton Script to Upload Patients

Instead I created the below python script that would make a POST call for each patient file and upload the bundle patient data to my fhir server. All it does is make a POST call for a fhir bundle transaction for patient json file. Here is the code.