# Smile CDR Tutorial - Export a CDA document from the SmileCDR repository

The CDA Exchange+ module is a Premium Solution and therefore access requires a separate purchasable license. If you are interested
in adding this module, please contact [sales@smiledigitalhealth.com](mailto:sales@smiledigitalhealth.com)
for further assistance.

This step-by-step tutorial will guide you through the process of exporting CDA documents from Smile CDR, it starts from the beginning with installing SmileCDR and setting up the CDA module.

**1\. Install Smile CDR**

Install Smile CDR either [from a tarball](/content/docs/quickstart/smile_quickstart.html) or [using Docker](/content/docs/guide_installation/docker_container_installation.html).

**2\. Start Smile CDR**

Start Smile CDR by running the following command:

```bash
$ bin/smilecdr start
```

Copy

**3\. Access the Web Admin Console**

Access the Web Admin Console by navigating to the following URL in your web browser:
[http://localhost:9100](http://localhost:9100/)

Log in with the default credentials "admin", "password".

**4\. Install your license**

Navigate to `Configuration``Module Config` and click on `license` on the left hand side.

Paste your license key into `License JWT Text`.

Click `Save Config`.

Click `Restart` to restart the License module.

**5\. Add a CDA Exchange+ Module**

In the Web Admin Console, navigate to `Configuration``Module Config` and click the `Add Module` button.

Select the `CDA Exchange+` module and click `Add`.

Scroll to the `Dependencies` section at the bottom and set `FHIR Storage Module (any FHIR version)` to `persistence (FHIR Storage (R4 RDBMS))`

Click `Create Module`.

Click `Start` to start the CDA Exchange+ module.

Verify that the module is running by checking there is a green checkmark beside cda\_exchange\_plus on the bottom left. If the module failed to start, there will be an error displayed on the top of the `Module Config` page, e.g. you will see `InsufficientLicenseException` if your license doesn't grant access to the CDA Exchange+ module.

**6\. Grant Permissions**

In a proper setup, we'd add the following permissions to the user account that needs to export CDA documents:

- `CREATE_CDA_TEMPLATE`
- `USE_CDA_TEMPLATE`
- `DELETE_CDA_TEMPLATE`
- `VIEW_CDA_TEMPLATE`

But since we're just in a quickstart testing environment, we will simply grant superuser permissions to the anonymous user:

Navigate to `Users & Authorization``User Management`.

Beside the `ANONYMOUS` user, click on `Edit`.

Enable the `ROLE_FHIR_CLIENT_SUPERUSER` (FHIR Client (Superuser)) permission and click `Save User`

**7\. Creating a CDA template**

The `generateCdaExchangeComposition()` function is used to build the Composition upon which the CDA will be built. More information on properly building a Composition can be found under the [CDA Module Mappings for Export](/content/docs/cda_exchange_plus/available_types_and_mappings.html) as well as the [CDA JavaScript Templating System](/content/docs/cda_exchange_plus/javascript_templates.html).

**8\. Upload CDA export template**

_Note, this tutorial assumes there is already data in the repository, if no CDAs have been ingested yet, please consult the [CDA import tutorial](/content/docs/guide_tutorials/tutorial_cda_import.html) before returning to this page._

Upload the template of your preference to the template endpoint with the template in the body of the request.
The template will have to be converted to a JSON format put together, the full call should resemble the following structure:

```http
PUT http://localhost:9000/cda/{module_id}/template/{template_id}

{
    "id": "default-ccd-template",
    "script": "function generateCdaExchangeComposition(inputMap) {[...]}",
	 "params": ["Patient"],
    "description": "Aggregate CCD Template"
    }
```

Copy

`module_id`: is the id of the CDA Exchange Plus module in SmileCDR
`template_id`: is the id of the CDA template you intend to create, update, delete or view.

Just like before, special characters will require escaping.

An example CCD template JSON body can be found [here](/content/docs/guide_tutorials/tutorial_cda_export_json.html). Upload this to the endpoint as mentioned above.

The `default-ccd-template` id is special as this is the id that will be used when using the `$docref` endpoint.

Upon successful ingestion, a `200 OK` will be displayed.

**9\. Export a CDA**

There are three ways to create a new CDA document:

**Method 1: $sdh.generate-cda**

The `$sdh.generate-cda` endpoint allows a user to specify which CDA template they want to apply to the Patient via the `valueString` under the `"name": "scriptName"`.
More information can be found in [Endpoints for exporting CDA documents](/content/docs/cda_exchange_plus/rest_operations.html#endpoints-for-exporting-cda-documents/index.html).

```http
POST http://localhost:8000/$sdh.generate-cda

{
  "resourceType": "Parameters",
  "parameter": [\
    {\
      "name": "scriptName",\
      "valueString": "my_script_name"\
    },\
    {\
      "name": "scriptParameters",\
      "resource": {\
        "resourceType": "Parameters",\
        "parameter": [\
          {\
            "name": "patient",\
            "valueString": "Patient/123"\
          },\
          {\
            "name": "startDate",\
            "valueDate": "2024-01-01"\
          }\
        ]\
      }\
    }\
  ]
}
```

Copy

Upon successful creation of a CDA document, the return will be a `200 OK` along with the following body:

```json
{
	"resourceType": "Bundle",
	"type": "searchset",
	"entry": [\
		{\
			"resource": {\
				"resourceType": "DocumentReference",\
				"status": "current",\
				"subject": {\
					"reference": "Patient/1413"\
				},\
				"content": [\
					{\
						"attachment": {\
							"contentType": "text/plain",\
							"data": "PD94bWwgdmVyc2lvbj0nMS4wJOnhzaT0i[redacted]",\
							"url": "/Binary/1452",\
							"title": "Uri where the data can be found: [base]/Binary/1452"\
						}\
					}\
				]\
			}\
		}\
	]
}
```

Copy

Where the `data` will contain the generated CDA in base64 encoding and `url` will contain a `Binary` resource with the generated XML CDA document.

**Method 2: using a Bundle or Composition resource**

In the event a `Bundle` or `Composition` resource already exists for the generation of the CDA document, the resource itself can be converted into the CDA format.

For generating a CDA document via a `Bundle` resource the following API call is used. This method assumes the `Bundle` resource contains all the resources necessary to generate a CDA document:

```http
POST http://localhost:8000/Bundle/{bundle_id}/$sdh.generate-cda
```

Copy

For generating a CDA document via a `Composition` resource, the following API call is used. This method assumes the `Composition` resource defines the structure of a document and contains references to all the necessary resources to include in each section.

```http
POST http://localhost:8000/Composition/{composition_id}/$sdh.generate-cda
```

Copy

On success, the output will be similar to the output in `$sdh.generate-cda`.

**Method 3: DocumentReference/$docref**

_This method will be changing significantly in SmileCDR versions 2025.02 and upwards_

The `DocumentReference/$docref` operation uses the `default-ccd-template` template to generate a CDA document with. It requires the input parameter `patient` which corresponds with the id of the Patient for whom the document will be generated. No other input parameters will be accepted.

```http
POST http://localhost:8000/DocumentReference/$docref

{
   "resourceType": "Parameters",
   "parameter": [\
      {\
         "name": "patient",\
         "valueId": "Patient/1413"\
      }\
   ]
}
```

Copy

On success, the output will be similar to the output in `$sdh.generate-cda`.

**Putting it all together**

With this knowledge, identify a `Patient` in your repo via

```http
GET http://localhost:8000/Patient
```

Copy

Take its `id` and perform a CDA export first using `$sdh.generate-cda` with the id `default-ccd-template`.

In order to generate from a `Composition` find a `Composition` resource via

```http
GET http://localhost:8000/Composition
```

Copy

Use this `Composition` to generate a CDA document via the `Composition` based `$sdh.generate-cda` route.

For more information on the various CDA endpoints, visit [our documentation](/content/docs/cda_exchange_plus/rest_operations.html).

[CDA Import Tutorial](/content/docs/guide_tutorials/tutorial_cda_import.html)

CDA Export Tutorial

Tutorials

[Introduction](/content/docs/guide_tutorials/tutorial_intro.html)
[Integration Testing Tutorial](/content/docs/guide_tutorials/tutorial_integration_test.html)
[Custom Operations Tutorial](/content/docs/guide_tutorials/tutorial_custom_operations.html)
[HL7 v2.x Ingestion Tutorial](/content/docs/guide_tutorials/tutorial_processing_hl7_v2x_feeds.html)
[Federated OAuth2/OIDC Tutorial](/content/docs/guide_tutorials/tutorial_federated_oauth2.html)
[MDM Tutorial](/content/docs/guide_tutorials/tutorial_mdm.html)
[MDM UI Tutorial](/content/docs/guide_tutorials/tutorial_mdm_ui.html)
[MDM First Golden Resource Tutorial](/content/docs/guide_tutorials/first_golden_resource.html)
[MDM Advanced Rule Authoring Tutorial](/content/docs/guide_tutorials/advanced_rule_authoring.html)
[MDM Web UI Admin Setup Tutorial](/content/docs/guide_tutorials/mdm_web_ui_admin_setup.html)
[CDA Import Tutorial](/content/docs/guide_tutorials/tutorial_cda_import.html)
[CDA Export Tutorial](/content/docs/guide_tutorials/tutorial_cda_export.html)
[CDA Export - Custom Narrative Generation Tutorial](/content/docs/guide_tutorials/tutorial_cda_export_narrative_generation.html)
[CDA Import JSON example](/content/docs/guide_tutorials/tutorial_cda_import_json.html)
[CDA Export Template JSON example](/content/docs/guide_tutorials/tutorial_cda_export_json.html)
[Consent Tutorial](/content/docs/guide_tutorials/tutorial_consent.html)
[CMS-0057-F IG Setup Tutorial](/content/docs/guide_tutorials/tutorial_terminology_cms57f.html)

[CDA Export - Custom Narrative Generation Tutorial](/content/docs/guide_tutorials/tutorial_cda_export_narrative_generation.html)
