add 2nd address line to patient, employer, insurance_data (#4790)
[openemr.git] / FHIR_README.md
blob2c796cdfdd6a4cee3f4d3ff4181aad46e9c064b9
1 # OpenEMR FHIR API Documentation
3 ## FHIR API Table of Contents
4 - [Overview](FHIR_README.md#overview)
5 - [Prerequisite](FHIR_README.md#prerequisite)
6 - [Using FHIR API Internally](FHIR_README.md#using-fhir-api-internally)
7 - [Multisite Support](FHIR_README.md#multisite-support)
8 - [Authorization (in API_README.md)](API_README.md#authorization)
9     - [Scopes (in API_README.md)](API_README.md#scopes)
10     - [Registration (in API_README.md)](API_README.md#registration)
11         - [SMART on FHIR Registration (in API_README.md)](API_README.md#smart-on-fhir-registration)
12     - [Authorization Code Grant (in API_README.md)](API_README.md#authorization-code-grant)
13     - [Refresh Token Grant (in API_README.md)](API_README.md#refresh-token-grant)
14     - [Password Grant (in API_README.md)](API_README.md#password-grant)
15     - [Client Credentials Grant (in API_README.md)](API_README.md#client-credentials-grant)
16     - [Logout (in API_README.md)](API_README.md#logout)
17     - [More Details (in API_README.md)](API_README.md#more-details)
18 - [FHIR API Documentation](FHIR_README.md#fhir-api-documentation)
19     - [Capability Statement](FHIR_README.md#capability-statement)
20     - [Provenance](FHIR_README.md#Provenance-resources)
21     - [BULK FHIR Exports](FHIR_README.md#bulk-fhir-exports)
22         - [System Export](FHIR_README.md#bulk-fhir-exports)
23         - [Patient Export](FHIR_README.md#bulk-fhir-exports)
24         - [Group Export](FHIR_README.md#bulk-fhir-exports)
25 - [For Developers](FHIR_README.md#for-developers)
27 ## Overview
29 Easy-to-use JSON-based REST API for OpenEMR FHIR. See standard OpenEMR API docs [here](API_README.md).
31 ## Prerequisite
33 Enable the Standard FHIR service (/fhir/ endpoints) in OpenEMR menu: Administration->Globals->Connectors->"Enable OpenEMR Standard FHIR REST API"
35 ## Using FHIR API Internally
37 There are several ways to make API calls from an authorized session and maintain security:
39 -   See the script at tests/api/InternalApiTest.php for examples of internal API use cases.
41 ## Multisite Support
43 Multisite is supported by including the site in the endpoint. When not using multisite or using the `default` multisite site, then a typical path would look like `apis/default/fhir/patient`. If you were using multisite and using a site called `alternate`, then the path would look like `apis/alternate/fhir/patient`.
45 ## Authorization
47 OpenEMR uses OIDC compliant authorization for API. SSL is required and setting baseurl at Administration->Globals->Connectors->'Site Address (required for OAuth2 and FHIR)' is required.
49 See [Authorization](API_README.md#authorization) for more details.
51 ## FHIR API Documentation
53 The FHIR API is documented via Swagger. Can see this documentation (and can test it) by going to the `swagger` directory in your OpenEMR installation. The FHIR API is documented there in the `fhir` section. Can also see (and test) this in the online demos at https://www.open-emr.org/wiki/index.php/Development_Demo#Daily_Build_Development_Demos (clicking on the `API (Swagger) User Interface` link for the demo will take you there).
55 Standard FHIR endpoints Use `https://localhost:9300/apis/default/fhir as base URI.`
57 Note that the `default` component can be changed to the name of the site when using OpenEMR's multisite feature.
59 _Example:_ `https://localhost:9300/apis/default/fhir/Patient` returns a Patient's bundle resource, etc
61 The Bearer token is required for each OpenEMR FHIR request (except for the Capability Statement), and is conveyed using an Authorization header. Note that the Bearer token is the access_token that is obtained in the [Authorization](API_README.md#authorization) section.
63 Request:
65 ```sh
66 curl -X GET 'https://localhost:9300/apis/fhir/Patient' \
67   -H 'Authorization: Bearer eyJ0b2tlbiI6IjAwNnZ3eGJZYmFrOXlxUjF4U290Y1g4QVVDd3JOcG5yYXZEaFlqaHFjWXJXRGNDQUtFZmJONkh2cElTVkJiaWFobHBqOTBYZmlNRXpiY2FtU01pSHk1UzFlMmgxNmVqZEhcL1ZENlNtaVpTRFRLMmtsWDIyOFRKZzNhQmxMdUloZmNJM3FpMGFKZ003OXdtOGhYT3dpVkx5b3BFRXQ1TlNYNTE3UW5TZ0dsUVdQbG56WjVxOVYwc21tdDlSQ3RvcDV3TEkiLCJzaXRlX2lkIjoiZGVmYXVsdCIsImFwaSI6ImZoaXIifQ=='
68 ```
70 ---
72 ### Capability Statement
74 #### GET fhir/metadata
76 This will return the Capability Statement. Note this can be tested in the Swagger documentation linked to in the above `FHIR API Documentation`.
77     ```sh
78     curl -X GET 'https://localhost:9300/apis/default/fhir/metadata'
79     ```
81 ### Provenance Resources
83 Provenance resources are requested by including `_revinclude=Provenance:target` in the search of a resource. Is currently supported for the following resources:
84   - AllergyIntolerance
85       ```sh
86       curl -X GET 'https://localhost:9300/apis/default/fhir/AllergyIntolerance?_revinclude=Provenance:target'
87       ```
89 ### BULK FHIR Exports
90 An export operation that implements the [BULK FHIR Export ONC requirements](https://hl7.org/fhir/uv/bulkdata/export/index.html) can be requested by issuing a GET request to the following endpoints:
91  - System Export, requires the **system/\*.$export** scope.  Exports All supported FHIR resources
92     ```sh
93           curl -X GET 'https://localhost:9300/apis/default/fhir/$export'
94     ```
95  - Group Export, requires the **system/Group.$export** scope.  Exports all data in the [Patient Compartment](https://www.hl7.org/fhir/compartmentdefinition-patient.html) for the group.
96    There is only one group defined in the system currently.  If OpenEMR defines additional patient population groups you would change the Group ID in the API call.
97     ```sh
98           curl -X GET 'https://localhost:9300/apis/default/fhir/Group/1/$export'
99     ```
100  - Patient Export, requires the **system/Group.$export** scope.  Exports all data for all patients in the [Patient Compartment](https://www.hl7.org/fhir/compartmentdefinition-patient.html).
101     ```sh
102           curl -X GET 'https://localhost:9300/apis/default/fhir/Patient/$export'
103     ```
104 You will get an empty body response with a **Content-Location** header with the URL you can query for status updates on the export.
106 To query the status update operation you need the **system/\*.$bulkdata-status** scope.  An example query:
107  - Status Query
108     ```sh
109           curl -X GET 'https://localhost:9300/apis/default/fhir/$bulkdata-status?job=92a94c00-77d6-4dfc-ae3b-73550742536d'
110     ```
112 A status Query will return a result like the following:
115   "transactionTime": {
116     "date": "2021-02-05 20:48:38.000000",
117     "timezone_type": 3,
118     "timezone": "UTC"
119   },
120   "request": "\/apis\/default\/fhir\/Group\/1\/%24export",
121   "requiresAccessToken": true,
122   "output": [
123     {
124       "url": "https:\/\/localhost:9300\/apis\/default\/fhir\/Document\/97552\/Binary",
125       "type": "Patient"
126     },
127     {
128       "url": "https:\/\/localhost:9300\/apis\/default\/fhir\/Document\/105232\/Binary",
129       "type": "Encounter"
130     }
131   ],
132   "error": []
136 You can download the exported documents which are formatted in Newline Delimited JSON (NDJSON) by making a call to:
137     ```sh
138           curl -X GET 'https://localhost:9300/apis/default/fhir/Document/105232/Binary'
139     ```
141 ## For Developers
143 FHIR endpoints are defined in the [primary routes file](_rest_routes.inc.php). The routes file maps an external, addressable
144 endpoint to the OpenEMR FHIR controller which handles the request, and also handles the JSON data conversions.
146 ```php
147 "GET /fhir/Patient" => function () {
148     RestConfig::authorization_check("patients", "demo");
149     $return = (new FhirPatientRestController())->getAll($_GET);
150     RestConfig::apiLog($return);
151     return $return;
155 At a high level, the request processing flow consists of the following steps:
158 JSON Request -> FHIR Controller Component -> FHIR Validation -> Parsing FHIR Resource -> Standard Service Component -> Validation -> Database
161 The logical response flow begins with the database result:
164 Database Result -> Service Component -> FHIR Service Component -> Parse OpenEMR Record -> FHIR Controller Component -> RequestControllerHelper -> JSON Response