EPIC API Client including authentication flow and some wrapped API calls
Project description
EPIC API Client
If your favorite function is not yet implemented, let me know, I will do this for you!
Installation
use pip install EPIC-API-Client.
see example.py for usage and setup .env accordingly.
tip: use uv pip for faster pip (pip install uv)!
How to use
Testing on EPIC sandbox
To familiarize, play around and test out the functionality of this package, you can use the EPIC sandbox. To use this sandbox, you only need to register an app at vendor services of EPIC, enable the EPIC sandbox and you're done! In the environment file, use the url of the EPIC sandbox for your organisation.
Using this wrapper within Nebula
Nebula uses unix websockets to send web requests instead of simple http requests. This capability has been added to this wrapper which can be activated setting the use_unix_socket=True argument:
client = EPICClient(client_id=client_id, jwt_generator=jwt_generator, base_url=base_url, use_unix_socket=True)
Make sure to setup your Nebula environment as if you would use the Webcallout functionality from EPIC itself.
1. Using FHIR API functions
This wrapper provides a convenient way to interact with EPIC's FHIR APIs. All FHIR-related functions are accessible through the fhir attribute of an initialized EPICClient instance.
For example, to read a patient resource:
client = EPICClient(...) # Initialize as shown in example_public.py
client.obtain_access_token()
patient_data = client.fhir.patient_read(patient_id="some_patient_fhir_id")
client.print_json(patient_data)
The client supports common FHIR operations such as read, search, and create for various resources including Patient, Encounter, DocumentReference, Observation, and Condition. A comprehensive list of implemented FHIR functions can be found in the "Implemented functions" section below.
These functions are organized into:
- Base FHIR Functions: Direct interactions with FHIR resources (e.g.,
patient_read,encounter_search) provided by theFHIRBaseclass. - FHIR Extension Functions: Higher-level convenience functions (e.g.,
patient_search_MRN,mrn_to_FHIRid) built upon the base functions, provided by theFHIRExtensionclass.
2. Authentication Methods
The client exclusively uses OAuth 2.0 with JWTs (JSON Web Tokens) for authentication, employing the client credentials grant type.
Setup and Usage:
- Initialize a
JWTGeneratorinstance, providing the path to your PFX file and its password. You may also need to provide yourkid(Key ID).from epic_api_client import JWTGenerator jwt_generator = JWTGenerator(pfx_file_path="path/to/your.pfx", pfx_password="your_pfx_password", kid="your_key_id")
- Pass this
jwt_generatoralong with yourclient_idand thebase_urlfor the EPIC environment when creating anEPICClientinstance. - After initializing the client, you must call the
obtain_access_token()method. This method uses the JWT to request an access token from EPIC's OAuth 2.0 token endpoint.client = EPICClient(client_id="your_client_id", jwt_generator=jwt_generator, base_url="your_epic_base_url") client.obtain_access_token()
- Once
obtain_access_token()is successful, the client will automatically include the access token in the headers for all subsequent API calls.
Currently, "basic token" authentication or other methods are not supported by this client.
3. How this wrapper is organized and what are base vs extensions?
The EPIC-API-Client is designed with a modular and extensible architecture:
-
EPICClient: This is the main class you interact with. It serves as the primary entry point and orchestrates the different components. It inherits fromEPICHttpClient. -
EPICHttpClient: This class handles the underlying HTTP communication, including sending requests and processing responses. It manages the OAuth 2.0 authentication flow (obtaining and using access tokens) and also supports using Unix domain sockets for integration with environments like Nebula (via theuse_unix_socket=Trueparameter). -
Functional Modules (Base and Extensions):
-
FHIR Module:
FHIRBase: This class provides the foundational methods for interacting directly with FHIR resources. It implements generic functions likeget_resourceandpost_resource, as well as specific methods for common FHIR operations (e.g.,patient_read,condition_create).FHIRExtension: This class inherits fromFHIRBaseand builds upon its functionalities by adding higher-level, more specialized, or convenience functions related to FHIR. Examples includepatient_search_MRN(searching a patient by MRN) andmrn_to_FHIRid(converting an MRN to a FHIR ID). These are accessed viaclient.fhir.
-
Internal API Module (Optional Extension):
- This functionality is provided by a separate package,
epic_internal_extension, which can be installed if needed. InternalBase: Similar toFHIRBase, this class provides foundational methods for interacting with specific internal (non-FHIR) Epic APIs, such as those for SmartData elements or the Epic Cognitive Computing Platform (ECCP).InternalExtension: This class inherits fromInternalBaseand is intended to provide more specialized or composite functions for these internal Epic APIs. If theepic_internal_extensionpackage is installed, these functions are accessible viaclient.internal.
- This functionality is provided by a separate package,
-
In summary, the "base" classes (FHIRBase, InternalBase) offer core, direct API interaction capabilities. The "extension" classes (FHIRExtension, InternalExtension) inherit from their respective base classes to provide more advanced, specific, or user-friendly functionalities, making the client more powerful and easier to use for common tasks.
Troubleshooting
- To print full requests and response for debugging purposes, initialise the client with
debug_mode=True. - AttributeError: module 'jwt' object has no attribute 'encode'
uv pip uninstall jwt uv pip uninstall PyJWT uv pip install PyJWT
Implemented functions
- FHIR API functions
- get_metadata
- patient_read
- patient_search
- encounter_read
- encounter_search
- document_reference_read
- document_reference_search
- document_reference_create
- observation_create
- condition_read
- condition_search
- condition_create
- FHIR API extention functions
- patient_search_MRN
- mrn_to_FHIRid
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file epic_api_client-2025.1.12.tar.gz.
File metadata
- Download URL: epic_api_client-2025.1.12.tar.gz
- Upload date:
- Size: 15.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cee404c269582f5e9a11b88ba3ef1e25ba86fb948d2ce81e692a3b9587cc52f5
|
|
| MD5 |
8547b0d627b30c9e2e5ef1a7fbe7b20e
|
|
| BLAKE2b-256 |
420fed7f512e119a74173d3ff6cc9a7fd6d4d74bbb18877c5188217d8c369cb9
|
Provenance
The following attestation bundles were made for epic_api_client-2025.1.12.tar.gz:
Publisher:
publish-public-pypi.yml on AmsterdamUMC/epic_api_client
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
epic_api_client-2025.1.12.tar.gz -
Subject digest:
cee404c269582f5e9a11b88ba3ef1e25ba86fb948d2ce81e692a3b9587cc52f5 - Sigstore transparency entry: 438194863
- Sigstore integration time:
-
Permalink:
AmsterdamUMC/epic_api_client@118cad63dc0396a1c005b82021aa7df2c1bacea1 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/AmsterdamUMC
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-public-pypi.yml@118cad63dc0396a1c005b82021aa7df2c1bacea1 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file epic_api_client-2025.1.12-py3-none-any.whl.
File metadata
- Download URL: epic_api_client-2025.1.12-py3-none-any.whl
- Upload date:
- Size: 15.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
abcc401fdeb0d2289a18192e66276455c3891df9b5e6df041af70f2b8dcb72ca
|
|
| MD5 |
5f951733f31121111e270cd64040af5f
|
|
| BLAKE2b-256 |
9a6219874411cd644e61164e7a951d1815ccffb6988f12338de9a68f34129d28
|
Provenance
The following attestation bundles were made for epic_api_client-2025.1.12-py3-none-any.whl:
Publisher:
publish-public-pypi.yml on AmsterdamUMC/epic_api_client
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
epic_api_client-2025.1.12-py3-none-any.whl -
Subject digest:
abcc401fdeb0d2289a18192e66276455c3891df9b5e6df041af70f2b8dcb72ca - Sigstore transparency entry: 438194903
- Sigstore integration time:
-
Permalink:
AmsterdamUMC/epic_api_client@118cad63dc0396a1c005b82021aa7df2c1bacea1 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/AmsterdamUMC
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-public-pypi.yml@118cad63dc0396a1c005b82021aa7df2c1bacea1 -
Trigger Event:
workflow_dispatch
-
Statement type: