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
$ 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';
const client = new Client({
env: Environments.SANDBOX | Environments.PRODUCTION,
});
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',
});
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');
}
}
const visit = await visits.create({
code: 'hair-loss',
});
await visit.uploadPhoto('photoId1234', 'base64photostring');