The request method returns a promise that will be resolved with different values, depending on the fhirOptions argument.

Fetch a resource

const client = new FHIR.client("https://r3.smarthealthit.org");
client.request("Patient/2e27c71e-30c8-4ceb-8c1c-5641e066c0a4");
function display(data) {
    const output = document.getElementById("output");
    output.innerText = data instanceof Error ?
        String(data) :
        JSON.stringify(data, null, 4);
}
        
const client = new FHIR.client("https://r3.smarthealthit.org");
client.request("Patient/2e27c71e-30c8-4ceb-8c1c-5641e066c0a4")
    .then(display)
    .catch(display);

Display the current patient

Authenticated clients will have patient.id. This example uses an open FHIR server so we pass a patient to manually specify which the current patient is.

const client = new FHIR.client({
    serverUrl: "https://r3.smarthealthit.org",
    tokenResponse: {
        patient: "2e27c71e-30c8-4ceb-8c1c-5641e066c0a4"
    }
});

client.request(`Patient/${client.patient.id}`);
function display(data) {
    const output = document.getElementById("output");
    output.innerText = data instanceof Error ?
        String(data) :
        JSON.stringify(data, null, 4);
}

const client = new FHIR.client({
    serverUrl: "https://r3.smarthealthit.org",
    tokenResponse: {
        patient: "2e27c71e-30c8-4ceb-8c1c-5641e066c0a4"
    }
});

client.request(`Patient/${client.patient.id}`)
    .then(display)
    .catch(display);

Display the current patient from EHR

// launch.html
FHIR.oauth2.authorize({
    "client_id": "my-client-id",
    "scope": "launch"
});

// index.html
FHIR.oauth2.ready(client => client.request(`Patient/${client.patient.id}`));

Display the current user

Properly launched and authenticated clients will have an user property. To get that, you need to request openid and fhirUser scopes. This example uses an open FHIR server so we pass an id_token to manually specify which the current user is.

const id_token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9." +
"eyJwcm9maWxlIjoiUHJhY3RpdGlvbmVyL3NtYXJ0LVByYWN0aXRpb2" +
"5lci03MjA4MDQxNiIsImZoaXJVc2VyIjoiUHJhY3RpdGlvbmVyL3Nt" +
"YXJ0LVByYWN0aXRpb25lci03MjA4MDQxNiIsInN1YiI6IjM2YTEwYm" +
"M0ZDJhNzM1OGI0YWZkYWFhZjlhZjMyYmFjY2FjYmFhYmQxMDkxYmQ0" +
"YTgwMjg0MmFkNWNhZGQxNzgiLCJpc3MiOiJodHRwOi8vbGF1bmNoLn" +
"NtYXJ0aGVhbHRoaXQub3JnIiwiaWF0IjoxNTU5MzkyMjk1LCJleHAi" +
"OjE1NTkzOTU4OTV9.niEs55G4AFJZtU_b9Y1Y6DQmXurUZZkh3WCud" +
"ZgwvYasxVU8x3gJiX3jqONttqPhkh7418EFssCKnnaBlUDwsbhp7xd" +
"WN4o1L1NvH4bp_R_zJ25F1s6jLmNm2Qp9LqU133PEdcRIqQPgBMyZB" +
"WUTyxQ9ihKY1RAjlztAULQ3wKea-rfe0BXJZeUJBsQPzYCnbKY1dON" +
"_NRd8N9pTImqf41MpIbEe7YEOHuirIb6HBpurhAHjTLDv1IuHpEAOx" +
"pmtxVVHiVf-FYXzTFmn4cGe2PsNJfBl8R_zow2n6qaSANdvSxJDE4D" +
"UgIJ6H18wiSJJHp6Plf_bapccAwxbx-zZCw";

const client = new FHIR.client({
    serverUrl: "https://r3.smarthealthit.org",
    tokenResponse: { id_token }
});

client.request(client.user.fhirUser);
const id_token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9." +
"eyJwcm9maWxlIjoiUHJhY3RpdGlvbmVyL3NtYXJ0LVByYWN0aXRpb2" +
"5lci03MjA4MDQxNiIsImZoaXJVc2VyIjoiUHJhY3RpdGlvbmVyL3Nt" +
"YXJ0LVByYWN0aXRpb25lci03MjA4MDQxNiIsInN1YiI6IjM2YTEwYm" +
"M0ZDJhNzM1OGI0YWZkYWFhZjlhZjMyYmFjY2FjYmFhYmQxMDkxYmQ0" +
"YTgwMjg0MmFkNWNhZGQxNzgiLCJpc3MiOiJodHRwOi8vbGF1bmNoLn" +
"NtYXJ0aGVhbHRoaXQub3JnIiwiaWF0IjoxNTU5MzkyMjk1LCJleHAi" +
"OjE1NTkzOTU4OTV9.niEs55G4AFJZtU_b9Y1Y6DQmXurUZZkh3WCud" +
"ZgwvYasxVU8x3gJiX3jqONttqPhkh7418EFssCKnnaBlUDwsbhp7xd" +
"WN4o1L1NvH4bp_R_zJ25F1s6jLmNm2Qp9LqU133PEdcRIqQPgBMyZB" +
"WUTyxQ9ihKY1RAjlztAULQ3wKea-rfe0BXJZeUJBsQPzYCnbKY1dON" +
"_NRd8N9pTImqf41MpIbEe7YEOHuirIb6HBpurhAHjTLDv1IuHpEAOx" +
"pmtxVVHiVf-FYXzTFmn4cGe2PsNJfBl8R_zow2n6qaSANdvSxJDE4D" +
"UgIJ6H18wiSJJHp6Plf_bapccAwxbx-zZCw";
                    
function display(data) {
    const output = document.getElementById("output");
    output.innerText = data instanceof Error ?
        String(data) :
        JSON.stringify(data, null, 4);
}

const client = new FHIR.client({
    serverUrl: "https://r3.smarthealthit.org",
    tokenResponse: { id_token }
});

client.request(client.user.fhirUser).then(display).catch(display);

Display the current user from EHR

Properly launched and authenticated clients will have userId. To get that, you need to request openid and fhirUser scopes.

// launch.html
FHIR.oauth2.authorize({
    "client_id": "my-client-id",
    "scope": "openid fhirUser"
});

// index.html
FHIR.oauth2.ready(client => client.request(client.user.fhirUser));

Display patient medications

const client  = new FHIR.client("https://r3.smarthealthit.org");
const getPath = client.getPath;

client.request(`/MedicationRequest?patient=smart-1642068`, {
    resolveReferences: "medicationReference"
}).then(data => data.entry.map(item => getMedicationName(
    getPath(item, "resource.medicationCodeableConcept.coding") ||
    getPath(item, "resource.medicationReference.code.coding")
)));
const rxnorm  = "http://www.nlm.nih.gov/research/umls/rxnorm";
const client  = new FHIR.client("https://r3.smarthealthit.org");
const getPath = client.getPath;

function display(data) {
    const output = document.getElementById("output");
    output.innerText = data instanceof Error ?
        String(data) :
        JSON.stringify(data, null, 4);
}

function getMedicationName(medCodings = []) {
    var coding = medCodings.find(c => c.system === rxnorm);
    return coding && coding.display || "Unnamed Medication(TM)";
}

client.request(`/MedicationRequest?patient=smart-1642068`, {
    resolveReferences: "medicationReference"
}).then(data => data.entry.map(item => getMedicationName(
    getPath(item, "resource.medicationCodeableConcept.coding") ||
    getPath(item, "resource.medicationReference.code.coding")
))).then(display, display);

Resolve references

You can pass one or more reference paths as resolveReferences option. The final result will contain the references replaced with resolved resources.

const client = new FHIR.client("https://r3.smarthealthit.org");
client.request("/Encounter/3d1ae6a9-b8c1-4c12-a8ba-b44d96ddfe24", {
    resolveReferences: ["serviceProvider", "subject"]
});
const client = new FHIR.client("https://r3.smarthealthit.org");

function display(data) {
    const output = document.getElementById("output");
    output.innerText = data instanceof Error ?
        String(data) :
        JSON.stringify(data, null, 4);
}

client.request("/Encounter/3d1ae6a9-b8c1-4c12-a8ba-b44d96ddfe24", {
    resolveReferences: ["serviceProvider", "subject"]
}).then(display).catch(display);

Fetch references to external object

In most cases having the resolved references mounted on the result structure is very convenient. However, sometimes you might want to keep the original response as is, or you may want to have a map of resolved references that can be re-used later. To do so, you can use the same resolveReferences option but combine it with graph: false. Then, the result will contain two properties - data and references. Example:

const client = new FHIR.client("https://r3.smarthealthit.org");
client.request("/Encounter/3d1ae6a9-b8c1-4c12-a8ba-b44d96ddfe24", {
    resolveReferences: ["serviceProvider", "subject"],
    graph: false
});
function display(data) {
    const output = document.getElementById("output");
    output.innerText = data instanceof Error ?
        String(data) :
        JSON.stringify(data, null, 4);
}

const client = new FHIR.client("https://r3.smarthealthit.org");

client.request("/Encounter/3d1ae6a9-b8c1-4c12-a8ba-b44d96ddfe24", {
    resolveReferences: ["serviceProvider", "subject"],
    graph: false
}).then(display).catch(display);

Get multiple pages

This shows how to fetch 2 pages. Not that the result is an array of page bundles.

const client = new FHIR.client("https://r3.smarthealthit.org");
client.request("/Patient", { pageLimit: 2 });
function display(data) {
    const output = document.getElementById("output");
    output.innerText = data instanceof Error ?
        String(data) :
        JSON.stringify(data, null, 4);
}

const client = new FHIR.client("https://r3.smarthealthit.org");
client.request("/Patient", { pageLimit: 2 }).then(display).catch(display);

Get all pages

To get all available pages use pageLimit: 0:

const client = new FHIR.client("https://r3.smarthealthit.org");
client.request("/Claim", { pageLimit: 0 });
function display(data) {
    const output = document.getElementById("output");
    output.innerText = data instanceof Error ?
        String(data) :
        JSON.stringify(data, null, 4);
}

const client = new FHIR.client("https://r3.smarthealthit.org");
client.request("/Claim", { pageLimit: 0 })
    .then(display).catch(display);