Use Aidbox with Python
Follow these steps to set up a Python project with Aidbox
To use Aidbox with Python, we recommend to use our SDK generation library . It will generate static FHIR Client and the set of types for your FHIR package. You can read about the idea of it here.
Prerequisites
- Run Aidbox locally by following the instructions in the Run Aidbox locally guide
Steps
- 1.
Generate SDK package
npm install -g @fhirschema/codegen npx fscg generate -g typescript -p hl7.fhir.r4.core@4.0.1 -o aidbox --py-sdk-package aidbox --py-allow-extra-fields
- 2.
Init virtual env and install dependencies:
python3 -m venv venv source venv/bin/activate pip install -r aidbox/requirements.txt
- 3.
Create a Client in a new main.py file:
from typing import Any, Dict import requests from aidbox.client import Auth, AuthCredentials, Client from aidbox.hl7_fhir_r4_core import (CodeableConcept, HumanName, Identifier, Observation, ObservationComponent, Patient, Quantity, Reference) client = Client( base_url="http://localhost:8080/fhir", auth=Auth( method="basic", credentials=AuthCredentials( username="root", password="<secret>", # get actual value from docker-compose.yaml: BOX_ROOT_CLIENT_SECRET ), ), )
- 4.
Create Patient with id = "pt1" (PUT):
patient = Patient( id="pt1", identifier=[Identifier(system="http://org.io/id", value="0000-0000")], name=[HumanName(given=["John"], family="Doe")], gender="male", birth_date="1990-01-01", ) try: result = client.update(patient) print(result.model_dump_json(exclude_unset=True, exclude_none=True)) except requests.exceptions.RequestException as e: print("Error:", e) if e.response is not None: response_json: Dict[str, Any] = e.response.json() print(response_json)
- 5.
Create Observation that references the Patient:
observation = Observation( id="obs1", code=CodeableConcept(coding=[{"system": "http://loinc.org", "code": "85354-9"}]), status="final", effective_date_time="2025-07-08", subject=Reference(reference="Patient/pt1"), component=[ ObservationComponent( code=CodeableConcept( coding=[{"system": "http://loinc.org", "code": "8480-6"}] ), value_quantity=Quantity( value=120, unit="mmHg", system="http://unitsofmeasure.org", code="mm[Hg]", ), ), ObservationComponent( code=CodeableConcept( coding=[{"system": "http://loinc.org", "code": "8462-4"}] ), value_quantity=Quantity( value=80, unit="mmHg", system="http://unitsofmeasure.org", code="mm[Hg]" ), ), ], ) try: result = client.update(observation) print(result.model_dump_json(exclude_unset=True, exclude_none=True)) except requests.exceptions.RequestException as e: print("Error:", e) if e.response is not None: response_json: Dict[str, Any] = e.response.json() print(response_json)
- 6.
Run the project
python3 main.py
Next steps
- Learn more about Aidbox SDKs generation
- Learn more about Aidbox Access Control