In certain cases, such as regulatory requirements, legacy systems integration, or the need to support a specific workflow, it may be necessary to convert FHIR data to C-CDA format to seamlessly integrate systems.
A typical FHIR to C-CDA conversion process is usually divided into three main steps:
Gather required data in FHIR format: In this step, a number of GET requests are performed to retrieve the required data from the FHIR server or source systems.
Generate FHIR 'document' type Bundle: The gathered data is organized into a FHIR Document Bundle, with the FHIR Composition resource serving as the main document container.
Convert FHIR Document to C-CDA Document: In this step, the FHIR data is mapped and transformed into C-CDA structures, and divided into entries and sections according to the C-CDA document template requirements.
Aidbox simplifies this process by incorporating features such as document definitions, section narrative generators, and pseudo-code scripts to preprocess the document before the conversion.
In technical terms, to generate a C-CDA document from FHIR data, it is necessary to create a FHIR Document bundle containing a Composition resource that specifies the top-level document attributes, including the title, document type, author, subject (patient), and a list of document sections. Each section must be described by type, title and the FHIR resources to be included. Once the FHIR Document bundle is composed, it can be submitted to the /ccda/v2/to-ccda endpoint for conversion to a C-CDA document.
Below are some details about each of these features.
Document Definitions
To simplify the creation of Document bundles, Aidbox offers a feature called Document Definition, which enables the description of document contents using the FHIR Search API. The example below illustrates how to define a Document Definition:
Each resource attribute, such as :subject, :author, or :section/:entry, is specified as a HTTP request that returns a single FHIR resource or multiple FHIR resources. The full power of the FHIR Search API can be used to retrieve resources that meet specific criteria.
Parameters interpolation is also supported. For example, in the Vitals Signs section of the sample above:
Along with structured entries, CDA section contains human-readable narrative describing section data. This narrative can be automatically generated from entries (if specific section supports it) or it can be retrieved from Observation resource. To retrieve narrative from Observation, provide corresponding request under the :text key:
The best practice is to have Observation.code to be equal to section LOINC code for Observations containing section narratives. Narrative itself can be stored in either Observation.note[0].text or Observation.value.string.
Making modifications to the source FHIR Document
Quite often it's needed to make ad-hoc changes here and there in the CDA document to meet specific requirements. For example, one may want to generate section narrative from section entries or discard all patient identifiers except for SSN. To make this possible, you can get FHIR Document populated by Document Definition via /ccda/prepare-doc endpoint and then make modifications to it. When all modifications are in place, you can submit result to the /ccda/v2/to-cda endpoint to generate the final CDA document. Consider the following pseudo-code which removes all patient idenfitiers from the Patient resource except for SSN:
var docdef = { ... };var bundle =aidbox.post('/ccda/prepare-doc', docdef);var composition =bundle.entry[0].resource;var patient =bundle.findByRef(composition.subject);for (i =0; i <patient.identifier.length; i++) {var ident =patient.identifier[i];if (ident.system !='http://hl7.org/fhir/sid/us-ssn') {patient.identifier[i] =null; }}var cda =aidbox.post('/ccda/v2/to-cсda', bundle);
Another pseudo-code example on how to populate section narrative from section entries:
functiongenerateVitalSignsNarrative(section, bundle) {var result ='';for (i =0; i <section.entry.length; i++) {var vs =bundle.findByRef(section.entry[i]); result +='<paragraph>'+formatCode(vs.code) +" "+formatDate(vs.effective) +" "+formatValue(vs.value) +'</paragraph>'; }if (section.entry.length==0) { result ='No vital signs available'; }return result;}var docdef = { ... };var bundle =aidbox.post('/ccda/prepare-doc', docdef);var composition =bundle.entry[0].resource;for (i =0; i <composition.section.length; i++) {var section =composition.section[i];if (section.code.coding[0].code =='8716-3') {section.text = {// this function receives all the data it needs to populate narrative// it returns stringified HTML div:generateVitalSignsNarrative(section, bundle), status:'generated' }; }}var cda =aidbox.post('/ccda/v2/to-cсda', bundle);
Predefined Document Definitions
C-CDA / FHIR module provides ready-to-use Document Definitions for most frequently used document types.
docdef-id
Description
Supported Parameters
continuity-of-care
CCD
pid - Patient ID
start-dateend-date
Additionally to this list, you can put your own predefined Document Definitions via Aidbox Configuration Project.
/ccda/prepare-doc endpoint
This endpoint accepts Document Definition in request body or docdef-id and converts it into the FHIR Document bundle without applying C-CDA transformation. Resulting FHIR Document is returned. This endpoint is useful to debug Document Definition and check if all needed data is in place.
Endpoint uses query string to get values for Document Definition parameters. In the example below {{pid}} param will be replaced with 42 from the query string.
You can pass predefined Document Definition ID indocdef-id query-string parameter:
GET /ccda/prepare-doc?docdef-id=continuity-of-care&pid=42&start-date=2023-01-01&end-date=2023-02-01
Endpoint returns a FHIR Document or OperationOutcome resource in case of error.
// GET /ccda/prepare-doc?docdef-id=continuity-of-care?pid=xxx// HTTP/1.1 200 OK{"resourceType":"Bundle","type":"document","entry": [...]}
// GET /ccda/prepare-doc?docdef-id=continuity-of-care?pid=xxx// HTTP/1.1 404 Not found{"resourceType":"OperationOutcome","id":"processing","text": {"status":"generated", "div": "Transaction failed at entry[0]. Response status is 404. Response body is {\"resourceType\":\"OperationOutcome\",\"id\":\"not-found\",\"text\":{\"status\":\"generated\",\"div\":\"Resource Organization/42 not found\"},\"issue\":[{\"severity\":\"fatal\",\"code\":\"not-found\",\"diagnostics\":\"Resource Organization/42 not found\"}]}."
}, ....
You may set the mode of resolving references inmode query-string parameter, if you pass resolve-allall unresolved references will be resolved independently of FHIR Search query un Document Definition.
GET /ccda/prepare-doc?mode=resolve-all&docdef-id=continuity-of-care&pid=42&start-date=2023-01-01&end-date=2023-02-01
You may also pass :resolve instruction inside Document Definition. It will resolve references by path even if FHIR Search does not have such reference in search-params.
// GET /ccda/make-doc?docdef-id=continuity-of-care?pid=xxx// HTTP/1.1 404 Not found{"resourceType":"OperationOutcome","id":"processing","text": {"status":"generated", "div": "Transaction failed at entry[0]. Response status is 404. Response body is {\"resourceType\":\"OperationOutcome\",\"id\":\"not-found\",\"text\":{\"status\":\"generated\",\"div\":\"Resource Organization/42 not found\"},\"issue\":[{\"severity\":\"fatal\",\"code\":\"not-found\",\"diagnostics\":\"Resource Organization/42 not found\"}]}."
}, ....
Predefined Document Definition example:
GET /ccda/prepare-doc?docdef-id=continuity-of-care&pid=42&start-date=2023-01-01&end-date=2023-02-01
These Terms govern your access to and use of the Aidbox FHIR API module. By accessing or using the Aidbox FHIR API, the App Developer (“You”), agrees to be bound by these Terms.