FHIR Client for Python

2.2.0 · active · verified Sat Apr 11

fhirpy is an asynchronous and synchronous FHIR client for Python 3. This library provides a high-level API for performing CRUD operations and complex searches over FHIR resources. It is actively maintained by beda.software, with version 2.2.0 released on October 7, 2025, and generally follows a regular release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic CRUD (Create, Read, Update, Delete) operations and a search query using both the `AsyncFHIRClient` and `SyncFHIRClient`. Remember to set the `FHIR_SERVER_URL` environment variable or replace the placeholder with your actual FHIR server endpoint.

import asyncio
import os
from fhirpy import AsyncFHIRClient, SyncFHIRClient

# Replace with your FHIR server URL
FHIR_SERVER_URL = os.environ.get('FHIR_SERVER_URL', 'http://hapi.fhir.org/baseR4')

# --- Async Client Example ---
async def async_example():
    async_client = AsyncFHIRClient(FHIR_SERVER_URL, fhir_version='4.0.0')

    # Create a Patient resource
    patient = await async_client.resource('Patient', **{
        'resourceType': 'Patient',
        'gender': 'female',
        'name': [{'family': 'Doe', 'given': ['Jane']}]
    })
    await patient.save()
    print(f"Async: Created Patient with ID: {patient.id}")

    # Read the Patient resource
    read_patient = await async_client.resources('Patient').get(id=patient.id)
    print(f"Async: Read Patient: {read_patient.serialize()['name'][0]['given'][0]} {read_patient.serialize()['name'][0]['family']}")

    # Search for all Patients
    patients_search = async_client.resources('Patient').search(gender='female')
    all_patients = await patients_search.fetch_all()
    print(f"Async: Found {len(all_patients)} female patients.")

    # Delete the created Patient
    await read_patient.delete()
    print(f"Async: Deleted Patient with ID: {patient.id}")

# --- Sync Client Example ---
def sync_example():
    sync_client = SyncFHIRClient(FHIR_SERVER_URL, fhir_version='4.0.0')

    # Create a Patient resource
    patient = sync_client.resource('Patient', **{
        'resourceType': 'Patient',
        'gender': 'male',
        'name': [{'family': 'Smith', 'given': ['John']}]
    })
    patient.save()
    print(f"Sync: Created Patient with ID: {patient.id}")

    # Read the Patient resource
    read_patient = sync_client.resources('Patient').get(id=patient.id)
    print(f"Sync: Read Patient: {read_patient.serialize()['name'][0]['given'][0]} {read_patient.serialize()['name'][0]['family']}")

    # Search for all Patients
    patients_search = sync_client.resources('Patient').search(gender='male')
    all_patients = patients_search.fetch_all()
    print(f"Sync: Found {len(all_patients)} male patients.")

    # Delete the created Patient
    read_patient.delete()
    print(f"Sync: Deleted Patient with ID: {patient.id}")

if __name__ == '__main__':
    print("Running Async Example...")
    asyncio.run(async_example())
    print("\nRunning Sync Example...")
    sync_example()

view raw JSON →