JavaScript Client

Introduction

Photon provides a Client SDK written in vanilla JavaScript, for use on the frontend. The client makes it easy to authenticate providers and write prescriptions from your clinical app.

Note if you're using React, check out our React Integration.

Installation

npm install @photonhealth/sdk

The code for the SDK can be found in this Github Repository.

Getting Started

To setup the Photon Client, you must initialize the constructor

import { PhotonClient } from "@photonhealth/sdk";

const client = new PhotonClient({
  domain: "auth.photon.health",
  clientId: "YOUR_CLIENT_ID_SECRET",
  redirectURI: window.location.origin,
  organization: "org_XXX"
});

πŸ“˜

Development Mode

While the SDK is tooled to authenticate with production out of the box, you can easily switch it to using the APIs and authentication from our sandbox environment, Neutron.

Simply call .setDevelopment() on your Client instance.

Authenticating Users

When the PhotonClient instance is instantiated, it exposes a method called handleRedirect. This method must be called after redirecting and before calling login().

const auth = client.authentication
if (auth.hasAuthParams()) auth.handleRedirect()

To enable a user to login, all you have to do is call the login method from the PhotonClient instance:

<button id="mybutton">Login</button>

document.getElementById("mybutton").onclick = () => client.authentication.login({})

After a user has logged in, you may access their user information using the getUser function from the PhotonClient instance:

const user = client.authentication.getUser()

A user can easily be logged out using the logout method of the PhotonClient instance:

<button id="mybutton">Logout</button>

document.getElementById("mybutton").onclick = () => client.authentication.logout({})

Creating a Prescription

const createPrescriptionMutation = client.clinical.prescription.createPrescription({});

//Create new prescription
const { data, errors } = await createPrescriptionMutation({
  variables: {
    patientId: "pat_123",
    treatmentId: "med_123",
    dispenseAsWritten: false,
    dispenseQuantity: 30,
    dispenseUnit: "Each",
    refillsAllowed: 0,
    daysSupply: 30,
    instructions: "Take this prescription once every day with water"
  }
});

Accessing Data

Our API provide data in two fashions:

  • Paginated
  • Non-Paginated

Below is an example of accessing our Patient API (paginated) and Prescription API. Non-paginated queries work in a similar fashion, but there is no need to keep track of and advanced the after parameter to the query.

// Retrieve patients in a paginated fashion
const { data, error, refetch } = await client.clinical.patient.getPatients({
  first: 25,
  name: "John Doe",
});

//Fetch Next Page
const { data, error, refetch } = await client.clinical.patient.getPatients({
  first: 25,
  after: "XXX",
  name: "John Doe",
});

const createPatientMutation = client.clinical.patient.createPatient({});

//Create new patient
const { data, errors } = await createPatientMutation({
  variables: {
    name: {
      first: "John",
      last: "Doe"
    },
    dateOfBirth: "1990-01-01",
    sex: "MALE",
    phone: "555-555-5555"
  }
});