JS / TS project

Javascript and Typescript project wrapper

View full module reference here
View available functions here
View models here under models
View an example project here

The Terra Javascript client is a powerful wrapper for backends relying on JS / TS libraries such as ExpressJS and others. It encapsulates:

  • Models for data parsing
  • Data requests for a specific user
  • Authentication requests
  • Users management

Adding the module

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 the module

Start off by setting up a managing Terra instance

const terra = new Terra("DEV_ID", "API_KEY");

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(
      name,
      [
        "FITBIT",
        "OURA",
        "WITHINGS",
        "SUUNTO",
        "GARMIN",
        "GOOGLE",
        "POLAR",
        "IFIT",
      ],
      "EN",
      "success.com",
  		"fail.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);