heydoctor-js

Javascript library for the HeyDoctor Telehealth API.

Usage no npm install needed!

<script type="module">
  import heydoctorJs from 'https://cdn.skypack.dev/heydoctor-js';
</script>

README

HeyDoctor Javascript SDK

The HeyDoctor Javascript library provides convenient access to the HeyDoctor API for applications written in JavaScript.

Documentation

Check out our auto-generated docs using TypeDoc.

Installation

You can install heydoctor-js using your favorite package manager.

$ yarn add heydoctor-js
# or
$ npm install --save heydoctor-js

Usage

You'll first need to create a new client resource. If using the HeyDoctor SaaS platform, you'll need to provide the API key located in the EMR. The client contains the http module that will communicate with the HeyDoctor API, so we'll pass this client to the other resources you'll need.

Creating the client

import { Client, Environments, Visits, OAuth } from 'heydoctor-js';

// The client will hold all configuration options and the http module
// that communicates to our server
const client = new Client({
  env: Environments.SANDBOX | Environments.PRODUCTION, // determines the API and OAuth endpoints
});

// Create two resources passing both the client we've just created
// ** Some resources also take an optional options object as a second argument
const visits = new Visits(client;
const oauth = new OAuth(client, {
  clientId: 'your-provided-client-id',
  scope: '*',
});

Subscribing to HTTP events

The HeyDoctor client also doubles as an event emitter. The client extends from Mitt and complies with standard EventEmitter events.

interface HttpCallback {
  statusCode: number;
  data: any;
}

const client = new Client();

client.on('http:success', (data: HttpCallback) => void);
client.on('http:error', (data: HttpCallback) => void);

Authorizing via OAuth

Partnering clients will use the HeyDoctor JS SDK to create or link a HeyDoctor account. HeyDoctor conforms to the existing OAuth2 protocol. In the case that HeyDoctor already has the provided email on file, the SDK will open a dialog box, akin to Facebook or Google login, prompting the user to confirm their existing password. In all successful cases, an access token and refresh token will be returned. The resulting payload is outlined below.

We recommend to store the longstanding refresh token in a persisted data store. You’ll use this token, along with your unique client id and secret key to regenerate short lived access tokens used to authenticate requests to the HeyDoctor API. For security measures, the access token has a one hour TTL, after which the token will expire and new access token will need to be generated.

Upon successful authorization, all subsequent SDK calls will be authenticated using the access token. The access token can also be utilized server-side to call our API directly.

Example

import { Client, OAuth } from 'heydoctor-js';

const client = new Client({
  env: 'production',
});

const oauth = new OAuth(client, {
  clientId: 'your-provided-client-id',
});

// First attempt to create a new HeyDoctor user silently. The method will throw an error in the case that the email already exists,
// in which case you have the opportunity to present an unobtrusive dialog for the user to confirm their existing HeyDoctor password.
try {
  const { accessToken, refreshToken } = await oauth.createUser({
    email: 'frank@heydoctor.com',
    firstName: 'Frank',
    lastName: 'Sinatra',
    dob: '1915-12-12',
    gender: 'male',
    addressLine1: '123 Somewhere Street',
    city: 'Hoboken',
    state: 'NJ',
    zip: '07030',
  });
} catch (error) {
  if (error.code === 'account_exists') {
    const { accessToken, refreshToken } = await oauth.launchWebAuthFlow('frank@heydoctor.com');
  }
}

// Now that the client has been authorized, we can call call other methods available in the SDK
const visit = await visits.create({
  code: 'hair-loss',
});

// Excellent, we've just created our first visit. Now, we can upload photos to that visit.
await visit.uploadPhoto('photoId1234', 'base64photostring');