JavaScript client
Installation
Install the package using NPM or Yarn
npm i terra-api
yarn i terra-api
Then add the package to your code
const { default: Terra } = require("terra-api");
Setup
Start off by setting up a managing Terra instance
const terra = new Terra("DEV_ID", "API_KEY", "SECRET");
This instance can now be used to execute the different actions for the specified dev-id
Authentication
You can generate a widget session by specifying reference ID, providers, and optionally redirect URLs. This will return a TerraWidgetResponse promise, which contains status, session id, and URL.
terra
.generateWidgetSession({
referenceID: userid,
language: 'en',
providers: [
"Google",
"Garmin",
"Fitbit",
"Oura",
"Polar",
"Withings",
"Suunto",
"Coros",
],
showDisconnect: true,
authSuccessRedirectUrl: "success.com",
authFailureRedirectUrl: "failure.com",
})
.then((s) => {
// use the various response elements
if(s.status == "success")
console.log(s.url);
else
console.log(s.status);
});
You can list available providers using the providers function
terra
.getProviders()
.then((p) => {
if(p.status == "success")
console.log(p.providers);
else
console.log(p.status);
});
Manage Users
You can list your current subscribers or get the information of a particular user. The first returns a list of
terra.getUsers().then((r) => console.log(r.users));
terra.getUser("user_id").then((u) => console.log(u.user));
You can also deauthenticate a particular user
terra.deauthUser("user_id");
Data Getters
You can request data for a particular user, scope, and date range using the data getters. You can also specify if you'd like the response to your webhook or in the function promise
terra.getSleep("user_id", new Date(), undefined, false);
Updated about 1 year ago