How healthkey-ai/fhir_importers works
Purpose and boundaries
fhir-importers is a Python backend plus a federated frontend remote for connecting patient MyChart/Epic and HealthEx accounts. It performs OAuth handshakes, encrypts and persists connection tokens, provides connection-management UI/API, and triggers downstream extraction syncs. It explicitly does not retrieve or parse FHIR resources or communicate with ctomop.
Core entry points:
• API application: app/main.py
• CLI commands: cli/__main__.py
• Federated frontend bootstrap: frontend/src/main.tsx
• Container deployment: Dockerfile, docker-compose.yml
HTTP API and authentication boundary
The backend is organized around route handlers, authentication middleware/utilities, and typed request/response models. API routes initiate OAuth, receive callbacks, manage saved connections, and request synchronization; auth separates caller identity from provider OAuth credentials.
• app/main.py
• app/routers.py
• app/auth.py
• app/schemas.py
OAuth callback state is transient and must be validated before a connection is stored. JWT/private-key support is isolated from provider-token encryption.
• app/jwt_utils.py
• app/crypto.py
• app/state_store.py
Connection persistence and ownership
The service is the sole writer for the mychart_connections state, including encrypted access/refresh token material, provider identity, and sync metadata. SQLAlchemy-style database access/models form the persistence boundary, while Alembic owns schema evolution.
• app/models.py
• app/connections.py
• app/db.py
• alembic/versions/0003_pushed_resources.py
• alembic/versions/0004_healthex_patient_links.py
The expected runtime dependencies are a relational database for durable connections and Redis for TTL-bound OAuth state.
Provider integration flow
Provider-specific logic is split between MyChart-oriented application services and a HealthEx client/route surface. The flow is: authenticated user selects an organization/provider → backend creates state and redirects to provider OAuth → callback exchanges authorization code → encrypted connection is persisted → sync is requested.
• app/service.py
• app/client.py
• app/organizations.py
• app/healthex_routers.py
• services/healthex_client.py
Organization metadata is supplied from a repository-managed catalog:
• organizations.json
• scripts/load_orgs.py
Downstream extraction trigger
Successful connect and explicit re-sync operations trigger the external fhir_extract Airflow DAG rather than extracting records locally. This is an important service boundary: the DAG/service consumes the persisted connection through shared integration contracts, while this repository retains connection/token ownership.
• app/airflow.py
• app/service.py
• app/connections.py
Airflow is therefore a material external infrastructure dependency; failures at this boundary should not imply local FHIR processing.
Federated connection-management UI
The frontend is a Vite-built Module Federation remote intended to be embedded by a host application. It exposes provider connection flows and contexts, calls the backend API, and handles OAuth return/callback views.
• frontend/vite.remote.config.ts
• frontend/src/federation/HealthExProvider.tsx
• frontend/src/federation/MyChartProvider.tsx
• frontend/src/federation/MyChartCallback.tsx
• frontend/src/federation/client.ts
Provider UI state is client-side; durable connection state remains backend-owned.
Deployment and operational shape
The service is containerized and deployed through Docker Compose and GitHub Actions. Configuration is environment-driven, including database, Redis, OAuth/provider credentials, encryption/JWT material, and Airflow connectivity.
• Dockerfile
• deploy/docker-compose.yaml
• .github/workflows/deploy.yml
• app/config.py
The repository documentation is explicitly a target-state contract; implementation should be evaluated against the specified system design, especially docs/DATA_FLOW.md and token/sync integration documents.
Main entry points: service.py Connection service · routers.py Connection and OAuth routes · main.py API application · MyChartProvider.tsx Provider UI contexts
Components 17
Federated frontend
Embeddable connection-management remote.
- Connection UI remote · Embedded UI for provider connection and callback flows.
frontend/src/main.tsx - Provider UI contexts
frontend/src/federation/MyChartProvider.tsx
Backend service
Owns OAuth, connection state, and sync requests.
- API application
app/main.py - Connection and OAuth routes · Initiates OAuth, receives callbacks, manages connections, and requests sync.
app/routers.py - Caller authentication · Separates caller identity from provider credentials.
app/auth.py - OAuth state and token crypto · Validates transient callback state and encrypts provider tokens.
app/state_store.py - Connection service · Coordinates provider connection lifecycle and re-syncs.
app/service.py - Connection repository · Sole writer for encrypted connection and sync state.
app/connections.py
Provider integrations
OAuth and provider-specific integration surfaces.
- MyChart OAuth client
app/client.py - HealthEx integration
app/healthex_routers.py - Organization catalog · Repository-managed provider and organization metadata.
organizations.json
State and persistence
Durable connections and transient OAuth state.
- Connection database · Durable mychart_connections state.
app/db.py - Schema migrations
alembic/versions/0003_pushed_resources.py
External infrastructure
Dependencies outside this repository.
- OAuth state store · TTL-bound OAuth callback state.
- MyChart and HealthEx
- fhir_extract DAG · Consumes persisted connections through shared integration contracts.
app/airflow.py - Container deployment · Environment-driven service deployment.
deploy/docker-compose.yaml
Connections 17
- Connection UI remote → Provider UI contexts: boots provider flows
- Provider UI contexts → API application: calls API
- API application → Connection and OAuth routes: serves
- Connection and OAuth routes → Caller authentication: authenticates caller
- Connection and OAuth routes → Connection service: delegates connection actions
- Connection service → OAuth state and token crypto: creates and validates state
- OAuth state and token crypto → OAuth state store: stores TTL state
- Connection service → MyChart OAuth client: runs MyChart OAuth
- Connection service → HealthEx integration: runs HealthEx flow
- MyChart OAuth client → MyChart and HealthEx: authorization and token exchange
- HealthEx integration → MyChart and HealthEx: provider OAuth
- Connection service → Organization catalog: selects provider organization
- Connection service → Connection repository: stores encrypted connection
- Connection repository → Connection database: reads and writes
- Schema migrations → Connection database: evolves schema
- Connection service → fhir_extract DAG: triggers connect and re-sync
- Container deployment → API application: runs service