How to make FHIR API calls

After logging into your Epic system, click the "Copy Env" button to generate settings for several shell variables. Be sure to select the correct button depending on whether you're working on Linux or Windows. Afterwards, just open a bash/CMD window, paste in the variable assigments, then try executing the examples below.

bash/curl

Copy/paste the correct curl command into your terminal, and hit enter.

# Linux:
curl -H "Authorization: Bearer ${OAUTH_TOKEN}" -H "Accept: application/json+fhir" "${APIBASE}/Observation?_count=3&patient=${PATIENT}&category=vital-signs"

:: Windows:
curl -H "Authorization: Bearer %OAUTH_TOKEN%" -H "Accept: application/json+fhir" "%APIBASE%/Observation?_count=3&patient=%PATIENT%&category=vital-signs"

Python

Open up Python3 on your machine, and copy/paste the text below.

import os
import json
import requests

headers = {"Authorization" : "Bearer " + os.getenv("OAUTH_TOKEN"), "Accept" : "application/json+fhir"}
url = os.getenv("APIBASE") + "/Observation?_count=3&patient=" + os.getenv("PATIENT") + "&category=vital-signs"
rsp = requests.get(url, headers=headers)
print(json.dumps(rsp.json(), indent=4))

Python with FHIRPACK

FHIRPACK is a powerful package for reading and processing FHIR data in Python. It can be integrated in a Python script, or called directly via a CLI. The package reads an environment file on startup (.env) that contains the URL of the Epic server together with your access token.

cat - <<"EOF" >.env
AUTH_METHOD=oauth_token
APIBASE=${APIBASE}
PATIENT=${PATIENT}
OAUTH_TOKEN=${OAUTH_TOKEN}
EOF

fp -o "getObservations" -p "patient=${PATIENT},category=vital-signs"

Javascript

If you have node.js installed on your machine you can try executing the fetch command in that environment. Otherwise, it's just as easy to use the Javascript engine built into the browser. Copy/paste the text below into an html file, open it with a browser, populate the variables on the form, and click "Try It!".

<html>
<input id="apibase" type="text" placeholder="APIBASE" size=80><br>
<input id="patient" type="text" placeholder="PATIENT" size=80><br>
<textarea id="oauth_token" type="text" placeholder="OAUTH_TOKEN" cols=80></textarea><br>
<button onclick='getObservations();'>Try It!</button>

<div id="fhir_rsp"></div>
<script>
function getObservations() {
	fetch(apibase.value + '/Observation?_count=3&patient=' + patient.value + '&category=vital-signs',
		{headers: { "Authorization": "Bearer " + oauth_token.value, "Accept": "application/json+fhir"}}
	).then(response => response.json()).then(json => fhir_rsp.innerHTML = '<pre>' + JSON.stringify(json,null,4)+ '</pre>')
}
</script>
</html>