Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.


eHealth TRHC MedWise Risk API MedStmt Sample

node v8.17.0
version: 1.0.0
Here are some samples scripts using the TRHC MedWise Risk API , these samples are using test resources from the smarthealthit demo fhir server. Prerequisites: We are assuming the developer has obtained a Client ID and Secret from eHealth Services. These "keys" represent a developer application that is approved to use the MedWise Risk API. Get access token using OAuth2 Client Credentials. Warning! Be sure to keep the secret safe! Don't store your secrets in souce code control. Don't expose secrets within the code of a user agent such as a web browser or mobile client.
require('isomorphic-fetch') // authentication url const authUrl = process.env.AUTH_URL // create basic authentication with id and secret const basicAuth = Buffer.from(`${process.env.MW_RISK_API_TEST_APP_CLIENTID}:${process.env.MW_RISK_API_TEST_APP_SECRET}`).toString('base64') // get access token from authentication server const doc = await fetch(authUrl, { headers: { Authorization: `Basic ${basicAuth}`, 'Content-Type': 'application/json' }, method: 'POST' }).then(res => res.json()) const accessToken = doc.access_token console.log('access token', accessToken)
Now that we have the access token, we need to create a temporary profile to be used to generate the visualizations. We fetch a patient from the FHIR server using a patientId. Next, we grab an array of MedicationOrder FHIR resources for the patient. Since MedWise Risk API currently only accepts Medication, MedicationStatement, and MedicationDispense FHIR resources, we convert the MedicationOrders to simpler Medication resources. The patient and medications will be used to create a FHIR bundle.
const { pluck, prop, map } = require('ramda') const patientId = 'smart-1685497' const patient = await fetch('https://r2.smarthealthit.org/Patient/' + patientId) .then(res => res.json()) .then(patient => ({resource: patient})) const medicationStatement = { "resourceType": "MedicationStatement", "id": "example006", "text": { "status": "generated", "div": "<div xmlns=\"http://www.w3.org/1999/xhtml\"><p><b>Generated Narrative with Details</b></p><p><b>id</b>: example006</p><p><b>status</b>: active</p><p><b>medication</b>: Amoxicillin (product) <span>(Details : {SNOMED CT code '27658006' = 'p-Hydroxyampicillin', given as 'Amoxicillin (product)'})</span></p><p><b>effective</b>: 01/02/2014</p><p><b>dateAsserted</b>: 22/02/2014</p><p><b>informationSource</b>: <a>Peter Chalmers</a></p><p><b>subject</b>: <a>Donald Duck</a></p><p><b>taken</b>: n</p><p><b>note</b>: Father indicates they miss the occasional dose</p><p><b>dosage</b>: </p></div>" }, "status": "active", "medicationCodeableConcept": { "coding": [ { "system": "http://www.nlm.nih.gov/research/umls/rxnorm", "code": "308194", "display": "Amoxicillin 875 MG Oral Tablet" } ] }, "effectiveDateTime": "2014-02-01", "dateAsserted": "2014-02-22", "informationSource": { "reference": "RelatedPerson/peter", "display": "Peter Chalmers" }, "subject": { "reference": "Patient/pat1", "display": "Donald Duck" }, "taken": "n", "note": [ { "text": "Father indicates they miss the occasional dose" } ], "dosage": [ { "sequence": 1, "text": "5ml three times daily", "asNeededBoolean": false, "route": { "coding": [ { "system": "http://snomed.info/sct", "code": "260548002", "display": "Oral" } ] }, "doseQuantity": { "value": 5, "unit": "mL", "system": "http://unitsofmeasure.org", "code": "mL" }, "maxDosePerPeriod": { "numerator": { "value": 3 }, "denominator": { "value": 1, "system": "http://unitsofmeasure.org", "code": "d" } } } ] } //.then(bundle => pluck('resource', prop('entry', bundle))) //.then(pluck('medicationCodeableConcept')) //.then(map(med => ({resource: { resourceType: 'Medication', code: med }}))) const medications = [{resource: medicationStatement }] console.log(JSON.stringify(medications, null, 2))
Now that we have our access token and our patient and converted medications, we can create a bundle
const bundle = { resourceType: 'Bundle', type: 'collection', entry: [ patient, ...medications ] } JSON.stringify(bundle, null, 2)
and submit to MedWise Risk API. Note the absense of the risk score data in the response JSON. This has been moved to a new GET /profiles/{profile id}/scoredata endpoint which provide the risk score as JSON data. See call to scoredata toward the end of this runkit.
const result = await fetch(`${process.env.MEDWISE_RISK_API_URL}/profiles`, { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${accessToken}` }, body: JSON.stringify(bundle) }).then(res => res.json()) const profileId = result.id result
Above, you can see the returned object contains a profile Id. Now that we have a profileId, we can retrieve the 4 visualizations....
const score = await fetch(`${process.env.MEDWISE_RISK_API_URL}/profiles/${result.id}/riskscore`, { headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${accessToken}` } }) .then(res => res.json()) score.html
const matrix = await fetch(`${process.env.MEDWISE_RISK_API_URL}/profiles/${result.id}/matrix`, { headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${accessToken}` } }) .then(res => res.json()) matrix.html
const riskFactors = await fetch(`${process.env.MEDWISE_RISK_API_URL}/profiles/${result.id}/risk-factors`, { headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${accessToken}` } }) .then(res => res.json()) riskFactors.html
const adverseEffects = await fetch(`${process.env.MEDWISE_RISK_API_URL}/profiles/${result.id}/adverse-effects`, { headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${accessToken}` } }) .then(res => res.json()) adverseEffects.html
A new GET /profiles/{profile id}/scoredata endpoint provides the risk score as JSON data.
const scoreData = await fetch(`${process.env.MEDWISE_RISK_API_URL}/profiles/${result.id}/scoredata`, { headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${accessToken}` } }) .then(res => res.json()) scoreData
The bundle that was send in the prior POST to /profiles is cached for a limited time.
const cachedBundle = await fetch(`${process.env.MEDWISE_RISK_API_URL}/profiles/${result.id}`, { headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${accessToken}` } }) .then(res => res.json()) cachedBundle

no comments

    sign in to comment