Skip to main content

fhirstarter

tests FHIR STU3 | R4B | R5 Ruff

An ASGI FHIR API framework built on top of FastAPI and FHIR Resources.

Supports FHIR sequences:

Installation

pip install fhirstarter

Features

  • Automatic, standardized API route creation
  • Automatic validation of inputs and outputs through the use of FHIR Resources Pydantic models
  • Automatically-generated capability statement that can be customized, and a capability statement API route
  • An exception-handling framework that produces FHIR-friendly responses (i.e. OperationOutcomes)
  • Automatically-generated, integrated documentation generated from the FHIR specification
  • Custom search parameters for search endpoints

Background

FHIRStarter uses a provider-decorator pattern. Developers can write functions, or handlers, that implement FHIR interactions and plug them into the framework. FHIRStarter then automatically creates FHIR-compatible API routes from these developer-provided functions. Handlers that are supplied must use the resource classes defined by the FHIR Resources Python package, which is a collection of Pydantic models for FHIR resources.

In order to stand up a FHIR server, all that is required is to create a FHIRStarter and a FHIRProvider instance, register a FHIR interaction with the provider, add the provider to the FHIRStarter instance, and pass the FHIRStarter instance to an ASGI server. An example is provided below.

Usage

Currently-supported functionality

FHIRStarter will automatically generate the capability statement endpoint at /metadata. In addition, it supports the following instance- and type-level interactions:

Interaction handlers can be written as coroutines with async/await syntax, or as plain functions. FastAPI supports both, as does FHIRStarter.

Using uvloop can improve performance of the underlying event loop on supported platforms. FHIRStarter does not mandate the use of uvloop, but it may be enabled by importing uvloop and adding a snippet like this to your application startup script:

asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

Configuration for specific FHIR sequences

FHIRStarter will work out of the box as an R5 server. If a different sequence is desired, it must be specified with an environment variable:

FHIR_SEQUENCE=R4B

Model imports are affected by which version of fhir.resources is installed. For STU3 and R4B, model imports will look like this:

from fhir.resources.STU3.patient import Patient
from fhir.resources.R4B.patient import Patient

For R5, model imports will look like this:

from fhir.resources.patient import Patient

Example

A detailed example is available here: example.py.

import uvicorn
from fhir.resources.patient import Patient

from fhirstarter import FHIRProvider, FHIRStarter, InteractionContext
from fhirstarter.exceptions import FHIRResourceNotFoundError

# Create the app
app = FHIRStarter()

# Create a provider
provider = FHIRProvider()


# Register the patient read FHIR interaction with the provider
@provider.read(Patient)
async def patient_read(context: InteractionContext, id_: str) -> Patient:
    # Get the patient from the database
    patient = ...

    if not patient:
        raise FHIRResourceNotFoundError

    return Patient(
        **{
            # Map patient from database to FHIR Patient structure
        }
    )


# Add the provider to the app
app.add_providers(provider)


if __name__ == "__main__":
    # Start the server
    uvicorn.run(app)

Custom search parameters

Custom search parameters can be defined in a configuration file that can be passed to the app on creation.

[search-parameters.Patient.nickname]
type = "string"
description = "Nickname"
uri = "https://hostname/nickname"
include-in-capability-statement = true

Adding a custom search parameter via configuration allows this name to be used as an argument when defining a search-type interaction handler and also adds this search parameter to the API documentation for the search endpoint.

Capability statement

It is possible to customize the capability statement by setting a capability statement modifier:

def amend_capability_statement(
    capability_statement: MutableMapping[str, Any], request: Request, response: Response
) -> MutableMapping[str, Any]:
    capability_statement["version"] = "1.0"
    return capability_statement

app.set_capability_statement_modifier(amend_capability_statement)

FastAPI dependency injection

FastAPI's dependency injection system is exposed at various levels:

  • application: the __init__ method on the FHIRStarter class
  • provider: the __init__ method on the FHIRProvider class
  • handler: the read, update, create, or search_type decorator used to add a handler to a provider

Dependencies specified at the application level will be injected into all routes in the application.

Dependencies specified at the provider level will be injected into all routes that are added to the application from that specific provider.

Dependencies specified at the handler level only apply to that specific FHIR interaction.

Metadata

Release files for fhirstarter 4.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fhirstarter 4.2.2
File Size Uploaded Provenance Actions
fhirstarter-4.2.2.tar.gz 1.5 MB Without provenance Details for fhirstarter-4.2.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fhirstarter 4.2.2
File Interpreter ABI Platform Actions
fhirstarter-4.2.2-py3-none-any.whl Python 3 none any Details for fhirstarter-4.2.2-py3-none-any.whl

Total release size: 3.1 MB

Release files / fhirstarter-4.2.2.tar.gz

Download URL fhirstarter-4.2.2.tar.gz
Size 1.5 MB
Tags Source
SHA-256 checksum
How to use checksums
3e43700f11e639b68ab021aaf5f18f765d5f7b7860aeb86d53b43eae4a8452a7
BLAKE2b-256 checksum
How to use checksums
606f0294437e676ad902eeb1c8b4dc29925a7096c4e126f1df78b0d6a5783a91
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / fhirstarter-4.2.2-py3-none-any.whl

Download URL fhirstarter-4.2.2-py3-none-any.whl
Size 1.6 MB
Tags Python 3
SHA-256 checksum
How to use checksums
6eb73e0006c40b36812fcec041c388c49a1e0f03318f41be7c007675b0e9ab81
BLAKE2b-256 checksum
How to use checksums
2e5da8e00a3225c4bff498b9a516786413499bff2fb99e8a0f9fa199defb509d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for fhirstarter 4.2.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

4.2.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page