0.0.3 • Published 6 years ago

@madaket/provider-api-client-ts v0.0.3

Weekly downloads
-
License
SEE LICENSE IN LI...
Repository
-
Last release
6 years ago

Madaket Health Provider API Typescript Client

Typescript client for Madaket Health Provider API

RESTful JSON Endpoints by Madaket Health for clean, high-quality Provider Data

Installation

Inside your npm project:

npm install @madaket/provider-api-client-ts --save

Quickstart

import { AuthUtil } from '@madaket/provider-api-client-ts/auth';
import { ProviderApi, ProviderApiApiKeys, ProviderDataSourceQuery } from '@madaket/provider-api-client-ts/api';

const API_KEY = 'your-api-key';
const API_SECRET = 'your-api-secret'; // do not hard code this, load from ignored config or ENV variable

const providerApi = new ProviderApi();
providerApi.setApiKey(ProviderApiApiKeys.api_key, API_KEY);
providerApi.setApiKey(ProviderApiApiKeys.auth_token, AuthUtil.generateAuthToken(API_KEY, API_SECRET));


const query = new ProviderDataSourceQuery();
query.firstName = "James";
query.lastName = "Gibbs";

const res = providerApi.search(query)
    .then((res) => {
        console.log(JSON.stringify(res.body));
    })
    .error((err) => {
        console.log(JSON.stringify(err));
    });

Documentation for API Endpoints

Check out the swagger UI or the swagger spec

Documentation for Authentication

The above library pushes Authentication under the hood. However, if you'd like to pass over the library for your own client, the following details the method for authenticating with the server.

To Authenticate each request, include your issued API Key and a hash of your API Secret as query parameters:

?api_key=<your-api-key>&auth_token=<your-hashed-secret>

To hash the secret:

  1. Get the current date and format it as yyyy-mm-dd-HH-MM (in GMT)
  2. Remove the last character from the end (effectively resulting in yyyy-mm-dd-HH-M) to create the
  3. Concatenate <api_key><api_secret>
  4. Take the SHA-256 hash of the concatenated string
  5. Base64-SafeUrl encode the resulting bytes

The token will be valid for up to 10 minutes after it is created.

Typescript implementation of the previous steps:

import * as dateformat from 'dateformat';
import * as CryptoJS from 'crypto-js';

export class AuthUtil {

  public static generateAuthToken(api_key: string, api_secret: string) {
    const now = Date.now();
    let timestamp = dateformat(now, "GMT:yyyy-mm-dd-HH-MM");
    timestamp = timestamp.substring(0, timestamp.length - 1);
    const cleartext = api_key + api_secret + timestamp;

    // Take SHA-256 hash, Base64 encode, URL Safe
    return CryptoJS.SHA256(cleartext)
      .toString(CryptoJS.enc.Base64)
      .replace(/\+/g, '-')
      .replace(/\//g, '_')
      .replace(/=+$/, '');
  }

}