Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
103 lines (91 sloc) 5.87 KB

ihealth connector

About iHealth

iHealth is a company that connecected health devices. The device's data and services are exposed to developers as REST APIs.

Purpose of the connector for iHealth

The purpose of this connector is to simplify and streamline the way you access iHealth' APIs from, by providing you with a few native objects that you can directly integrate into your own scripts. This will hopefully allow you to create sophisticated connected health applications.


  • ihealth/user: this is the main object to interact with. It provides access to the data that is generated by the devices of a given user (the one for who you are passing an access token)
  • ihealth/mappings: configuration file used for internal purposes (constants and descriptions).
  • ihealth/httpclient: generic http client that handles the communication between and iHealth' APIs
  • ihealth/api/handleEvent: default callback invoked by iHealth when sending notifications.
  • ihealth/authorization/getRequestTokenUrl: This script implements steps 1 and 2 of the iHealth OAuth authorization process.
  • ihealth/authorization/getAccessToken: This script implements step 3 of the iHealth OAuth authorization process.
  • ihealth/test/tests.js: a list of examples on how to use the connectors' objects and corresponding methods.

How to use

Obtain access tokens from iHealth

Step 1

From a front-end application, send a request to the modules/ihealth/oauth2/getRequestTokenUrl script. The result returned by the aforementioned script should resemble the following:

>> curl -X POST  -F username=john.doe -H 'Authorization: bearer YOUR_SCRIPTRIO_TOKEN' ''
	"metadata": {
		"requestId": "13a75c1a-b9cd-41bb-811b-ae20dd59b92b",
		"status": "success",
		"statusCode": "200"
	"result": ""

Step 2

From the front-end, issue a request to the obtained URL. This redirects your end user to the iHealth login and authorization page, where he has to enter his credentials then authorize the application on the requested scope. Once this is done, iHealth automatically calls back the modules/ihealth/oauth2/getAccessToken script, providing it with access and secret tokens. These ared stored in your's global storage.

Important note

Because iHealth's authentication workflow involves many steps, you might obtain an error at the end of the authentication process such as the below:

{"response": {
  "metadata": {
    "requestId": "54c24cf0-380c-477b-a876-8dcbfe5734bb",
    "status": "success",
    "scriptLog": [
        "timestamp": "2017-02-02 08:26:01.982",
        "level": "log",
        "component": "ihealth/oauth2/getAccessToken",
        "message": "state ://oau"
    "statusCode": "200"
  },"result": {
 "status": "failure",
 "errorCode": "Inconsistency_Error",
 "errorDetail": "Could not find username undefined in store to store the tokens"

If this was the case, kindly execute Step1 and Step2 once again.

Use the connector

In order to use the connector, you need to import the main module: modules/ihealth/user, as described below:

var userModule = require("/modules/iheatlth/user");

Then create a new instance of the User class, defined in this module (we assume that we already otbained an access token for the given user):

var user = new userModule.User({username:"john.doe"});

The User class provides many methods to obtain data related to devices pertaining to the end user, such as:

data.userGetInfo = user.getInfo();
data.listSPO2Measures = user.listSPO2Measures();
data.listBloodPressureMeasures = user.listBloodPressureMeasures({from:new Date("2016-04-01").getTime()});
data.listActivities = user.listActivities();
data.getActivityAtDate = user.getActivityAtDate({date:new Date("2016-04-05").getTime()});
data.listWeightMeasures = user.listWeightMeasures({from:new Date("2016-04-05").getTime(), to:new Date().getTime()});
data.listFatFreeMassMeasures = user.listFatFreeMassMeasures();
data.listFatRatioMeasures = user.listFatRatioMeasures();
data.listFatMassMeasures = user.listFatMassMeasures();
data.listDiastolicBloodPressureMeasures = user.listDiastolicBloodPressureMeasures();
data.listWorkoutMeasures = user.listWorkoutMeasures();
data.listSleepMeasures = user.listSleepMeasures();

About notifications

As mentioned, you can subscribe to notifications that are emitted by a given device for the concerned user. By default, iHealth is instructed by the connector to send notifications to the API that resides in your account. This API's job is to trigger the handler that is configured to process the corresponding notification type.