# healthkey-ai/fhir_importers architecture

> Architecture diagram of the GitHub repository healthkey-ai/fhir_importers, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/healthkey-ai/fhir_importers
- Repository: https://github.com/healthkey-ai/fhir_importers

## Overview

#### 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: [`app/service.py`](https://github.com/healthkey-ai/fhir_importers/blob/main/app/service.py) (Connection service), [`app/routers.py`](https://github.com/healthkey-ai/fhir_importers/blob/main/app/routers.py) (Connection and OAuth routes), [`app/main.py`](https://github.com/healthkey-ai/fhir_importers/blob/main/app/main.py) (API application), [`frontend/src/federation/MyChartProvider.tsx`](https://github.com/healthkey-ai/fhir_importers/blob/main/frontend/src/federation/MyChartProvider.tsx) (Provider UI contexts)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Federated frontend"]
  node_ui["Connection UI remote<br/>Vite Module Federation remote<br/>[main.tsx]"]
  node_ui_federation["Provider UI contexts<br/>React federation providers"]
end

subgraph group_backend["Backend service"]
  node_api["API application<br/>Python HTTP API<br/>[main.py]"]
  node_routes["Connection and OAuth routes<br/>route handlers<br/>[routers.py]"]
  node_auth["Caller authentication<br/>auth utilities<br/>[auth.py]"]
  node_oauth_security["OAuth state and token crypto<br/>security services<br/>[state_store.py]"]
  node_connection_service["Connection service<br/>application service<br/>[service.py]"]
  node_connections["Connection repository<br/>persistence service<br/>[connections.py]"]
end

subgraph group_providers["Provider integrations"]
  node_mychart_client["MyChart OAuth client<br/>provider client<br/>[client.py]"]
  node_healthex["HealthEx integration<br/>client and routes"]
  node_organizations["Organization catalog<br/>provider metadata<br/>[organizations.json]"]
end

subgraph group_storage["State and persistence"]
  node_database[("Connection database<br/>relational database<br/>[db.py]")]
  node_migrations["Schema migrations<br/>Alembic migrations"]
end

subgraph group_external["External infrastructure"]
  node_redis[("OAuth state store<br/>Redis")]
  node_provider_oauth{{"MyChart and HealthEx<br/>external OAuth providers"}}
  node_airflow{{"fhir_extract DAG<br/>external Airflow workflow<br/>[airflow.py]"}}
  node_deployment["Container deployment<br/>Docker Compose deployment"]
end

node_ui -->|"boots provider flows"| node_ui_federation
node_ui_federation -->|"calls API"| node_api
node_api -->|"serves"| node_routes
node_routes -->|"authenticates caller"| node_auth
node_routes -->|"delegates connection actions"| node_connection_service
node_connection_service -->|"creates and validates state"| node_oauth_security
node_oauth_security -->|"stores TTL state"| node_redis
node_connection_service -->|"runs MyChart OAuth"| node_mychart_client
node_connection_service -->|"runs HealthEx flow"| node_healthex
node_mychart_client -->|"authorization and token exchange"| node_provider_oauth
node_healthex -->|"provider OAuth"| node_provider_oauth
node_connection_service -->|"selects provider organization"| node_organizations
node_connection_service -->|"stores encrypted connection"| node_connections
node_connections -->|"reads and writes"| node_database
node_migrations -->|"evolves schema"| node_database
node_connection_service -->|"triggers connect and re-sync"| node_airflow
node_deployment -->|"runs service"| node_api

click node_ui "https://github.com/healthkey-ai/fhir_importers/blob/main/frontend/src/main.tsx"
click node_ui_federation "https://github.com/healthkey-ai/fhir_importers/blob/main/frontend/src/federation/MyChartProvider.tsx"
click node_api "https://github.com/healthkey-ai/fhir_importers/blob/main/app/main.py"
click node_routes "https://github.com/healthkey-ai/fhir_importers/blob/main/app/routers.py"
click node_auth "https://github.com/healthkey-ai/fhir_importers/blob/main/app/auth.py"
click node_oauth_security "https://github.com/healthkey-ai/fhir_importers/blob/main/app/state_store.py"
click node_connection_service "https://github.com/healthkey-ai/fhir_importers/blob/main/app/service.py"
click node_connections "https://github.com/healthkey-ai/fhir_importers/blob/main/app/connections.py"
click node_mychart_client "https://github.com/healthkey-ai/fhir_importers/blob/main/app/client.py"
click node_healthex "https://github.com/healthkey-ai/fhir_importers/blob/main/app/healthex_routers.py"
click node_organizations "https://github.com/healthkey-ai/fhir_importers/blob/main/organizations.json"
click node_database "https://github.com/healthkey-ai/fhir_importers/blob/main/app/db.py"
click node_migrations "https://github.com/healthkey-ai/fhir_importers/blob/main/alembic/versions/0003_pushed_resources.py"
click node_airflow "https://github.com/healthkey-ai/fhir_importers/blob/main/app/airflow.py"
click node_deployment "https://github.com/healthkey-ai/fhir_importers/blob/main/deploy/docker-compose.yaml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_ui,node_ui_federation toneBlue
class node_api,node_routes,node_auth,node_oauth_security,node_connection_service,node_connections toneAmber
class node_mychart_client,node_healthex,node_organizations toneMint
class node_database,node_migrations toneRose
class node_redis,node_provider_oauth,node_airflow,node_deployment toneIndigo
```

## Components

### Federated frontend

Embeddable connection-management remote.

- [Connection UI remote](https://github.com/healthkey-ai/fhir_importers/blob/main/frontend/src/main.tsx): Vite Module Federation remote · Embedded UI for provider connection and callback flows. · `frontend/src/main.tsx`
- [Provider UI contexts](https://github.com/healthkey-ai/fhir_importers/blob/main/frontend/src/federation/MyChartProvider.tsx): React federation providers · `frontend/src/federation/MyChartProvider.tsx`

### Backend service

Owns OAuth, connection state, and sync requests.

- [API application](https://github.com/healthkey-ai/fhir_importers/blob/main/app/main.py): Python HTTP API · `app/main.py`
- [Connection and OAuth routes](https://github.com/healthkey-ai/fhir_importers/blob/main/app/routers.py): route handlers · Initiates OAuth, receives callbacks, manages connections, and requests sync. · `app/routers.py`
- [Caller authentication](https://github.com/healthkey-ai/fhir_importers/blob/main/app/auth.py): auth utilities · Separates caller identity from provider credentials. · `app/auth.py`
- [OAuth state and token crypto](https://github.com/healthkey-ai/fhir_importers/blob/main/app/state_store.py): security services · Validates transient callback state and encrypts provider tokens. · `app/state_store.py`
- [Connection service](https://github.com/healthkey-ai/fhir_importers/blob/main/app/service.py): application service · Coordinates provider connection lifecycle and re-syncs. · `app/service.py`
- [Connection repository](https://github.com/healthkey-ai/fhir_importers/blob/main/app/connections.py): persistence service · Sole writer for encrypted connection and sync state. · `app/connections.py`

### Provider integrations

OAuth and provider-specific integration surfaces.

- [MyChart OAuth client](https://github.com/healthkey-ai/fhir_importers/blob/main/app/client.py): provider client · `app/client.py`
- [HealthEx integration](https://github.com/healthkey-ai/fhir_importers/blob/main/app/healthex_routers.py): client and routes · `app/healthex_routers.py`
- [Organization catalog](https://github.com/healthkey-ai/fhir_importers/blob/main/organizations.json): provider metadata · Repository-managed provider and organization metadata. · `organizations.json`

### State and persistence

Durable connections and transient OAuth state.

- [Connection database](https://github.com/healthkey-ai/fhir_importers/blob/main/app/db.py): relational database · Durable mychart_connections state. · `app/db.py`
- [Schema migrations](https://github.com/healthkey-ai/fhir_importers/blob/main/alembic/versions/0003_pushed_resources.py): Alembic migrations · `alembic/versions/0003_pushed_resources.py`

### External infrastructure

Dependencies outside this repository.

- **OAuth state store**: Redis · TTL-bound OAuth callback state.
- **MyChart and HealthEx**: external OAuth providers
- [fhir_extract DAG](https://github.com/healthkey-ai/fhir_importers/blob/main/app/airflow.py): external Airflow workflow · Consumes persisted connections through shared integration contracts. · `app/airflow.py`
- [Container deployment](https://github.com/healthkey-ai/fhir_importers/blob/main/deploy/docker-compose.yaml): Docker Compose deployment · Environment-driven service deployment. · `deploy/docker-compose.yaml`

## Connections

- 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
