to-fhir

Collection of utilities for mapping FHIR resources: deterministic resource ids derived from business identifiers, references, canonical system/coding/extension templates, and a fluent transaction Bundle builder with optional Provenance. Available for Java, C#, and Python with the same behaviour and an idiomatic API in each.

Install

Published to Maven Central, NuGet, and PyPI.

Java — Gradle

implementation "io.github.diz-uker:to-fhir:0.2.21"

// optional, for Spring Boot auto-configuration
implementation "io.github.diz-uker:to-fhir-starter:0.2.21"

Java — Maven

<dependency>
    <groupId>io.github.diz-uker</groupId>
    <artifactId>to-fhir</artifactId>
    <version>0.2.21</version>
</dependency>

C# — .NET

dotnet add package DizUker.ToFhir --version 0.2.21

Python

uv add to-fhir==0.2.21
# or
pip install to-fhir==0.2.21

Usage

Build a transaction Bundle with a deterministic resource id and an attached Provenance, in whichever language you're working in.

Java

import io.github.dizuker.tofhir.IdUtils;
import io.github.dizuker.tofhir.TransactionBuilder;
import org.hl7.fhir.r4.model.Device;
import org.hl7.fhir.r4.model.Identifier;
import org.hl7.fhir.r4.model.Patient;
import org.hl7.fhir.r4.model.Reference;

var patient = new Patient()
    .setId(IdUtils.fromIdentifier(
        new Identifier().setSystem("https://example.org/pid").setValue("12345")));

var bundle = new TransactionBuilder()
    .withId("my-bundle")
    .withFullUrlBase("https://example.org/fhir")
    .withProvenance(
        new Device().setId("my-etl-job"),
        new Reference().setDisplay("The source system"))
    .addEntry(patient)
    .build();

C#

using Hl7.Fhir.Model;
using ToFhir;

var patient = new Patient
{
    Id = IdUtils.FromIdentifier(new Identifier("https://example.org/pid", "12345")),
};

var bundle = new TransactionBuilder()
    .WithId("my-bundle")
    .WithFullUrlBase("https://example.org/fhir")
    .WithProvenance(
        new Device { Id = "my-etl-job" },
        new ResourceReference { Display = "The source system" })
    .AddEntry(patient)
    .Build();

Python

from fhir.resources.R4B.device import Device
from fhir.resources.R4B.identifier import Identifier
from fhir.resources.R4B.patient import Patient
from fhir.resources.R4B.reference import Reference

from to_fhir import TransactionBuilder, id_utils

patient = Patient(
    id=id_utils.from_identifier(Identifier(system="https://example.org/pid", value="12345"))
)

bundle = (
    TransactionBuilder()
    .with_id("my-bundle")
    .with_full_url_base("https://example.org/fhir")
    .with_provenance(Device(id="my-etl-job"), Reference(display="The source system"))
    .add_entry(patient)
    .build()
)

Each language's TransactionBuilder also has a build_with_separate_provenance variant (buildWithSeparateProvenance in Java/C#) that returns the data and Provenance resources as two bundles instead of one. See the language-specific C# and Python README sections, and the API references below, for the full helper set: FhirSystems/fhir_systems (canonical system URIs), FhirCodings/fhir_codings (system + version Coding templates), FhirExtensions/fhir_extensions, and ReferenceUtils/reference_utils.

API reference

Generated from the same doc comments shipped in each package (Javadoc, XML doc comments, and Sphinx-flavoured docstrings).

Java

Javadoc for io.github.dizuker.tofhir, built with Gradle.

Browse the Java reference

C#

DocFX reference for the ToFhir namespace.

Browse the C# reference

Python

Sphinx reference for the to_fhir package.

Browse the Python reference