Terra Docs
Dashboard
  • Docs
  • API Reference
  • Changelog
  • Health & Fitness API
    • REST API Endpoints
    • Supported integrations
    • Event Types
    • Data models
    • Samples
    • Core concepts
    • Destinations
    • Mobile SDK
      • iOS (Swift)
      • Android (Kotlin)
      • React Native
      • Flutter
  • Streaming API
    • REST API Endpoints
    • Supported Integrations
    • Core Concepts
    • Websocket Reference
    • Mobile SDK
      • iOS (Swift)
      • Android (Kotlin)
      • Flutter
      • React Native
  • Teams API - Beta
    • Supported Integrations
    • Core Concepts
    • API Endpoints
    • Event types
Powered by GitBook
On this page
  • Webhook
  • Security
  • Payload signing
  • IP Whitelisting
  • Retries
  • SQL Database
  • Setup
  • Data Structure
  • Supabase
  • Setup
  • Data Structure
  • AWS Destinations
  • Setup
  • GCP Destinations
  • Buckets: AWS S3/GCP GCS
  • Data Structure
  • AWS SQS (Simple Queue Service)
  • Data Structure

Was this helpful?

  1. Health & Fitness API

Destinations

PreviousCore conceptsNextMobile SDK

Was this helpful?

Webhook

Webhooks are the most basic to set up, and involve Terra making a POST request to a predefined callback URL you pass in when setting up the Webhook.

They are automated messages sent from apps when something happens.

Terra uses webhooks to notify you whenever new data is made available for any of your users. New data, such as activity, sleep, etc will be normalised and sent to your webhook endpoint URL where you can process it however you see fit.

After a user authenticates with your service through Terra, you will automatically begin receiving webhook messages containing data from their wearable..

Security

Exposing a URL on your server can pose a number of security risks, allowing a potential attacker to

  • Launch denial of service (DoS) attacks to overload your server.

  • Tamper with data by sending malicious payloads.

  • Replay legitimate requests to cause duplicated actions.

among other exploits.

In order to secure your URL, Terra offers two separate methods of securing your URL endpoint

Payload signing

Every webhook sent by Terra will include HMAC-based signature header terra-signature , which will take the form:

t=1723808700,v1=a5ee9dba96b4f65aeff6c841aa50121b1f73ec7990d28d53b201523776d4eb00

In order to verify the payload, you may use one of Terra's SDKs as follows:

Terra requires the raw, unaltered body of the request to perform signature verification. If you’re using a framework, make sure it doesn’t manipulate the raw body (many frameworks do by default)

Any manipulation to the raw body of the request causes the verification to fail.

from terra.base_client import Terra

terra = Terra(api_key='YOUR API KEY', dev_id='YOUR DEV ID', secret='YOUR TERRA SECRET')

# Code to receive a webhook using Django
@csrf_exempt
def my_webhook_view(request):
    payload = request.body
    sig_header = request.META['TERRA_SIGNATURE']
    if not check_terra_signature(payload, sig_header):
        # Invalid signature
        print("Error verifying webhook signature")
        return HttpResponse(status=400)
const { default: Terra } = require("terra-api");

const terra = new Terra(devId: string, apiKey: string, secret: string);

async function handleWebhook(req) {
  // webhook handler
  console.log(req);
}

// using the Express framework
// expose POST /hook endpoint on the server
// this is where Terra will send webhooks
app.post('/hook', (req, res) => {
  // verify the signature of the payload
  if (!terra.checkTerraSignature(req.headers['terra-signature'], req.rawBody)) res.sendStatus(401);
  res.sendStatus(200);
  handleWebhook(req);
});
import co.tryterra.terraclient.TerraClientFactory;
import co.tryterra.terraclient.api.TerraApiResponse;
import co.tryterra.terraclient.api.TerraClientV2;
import co.tryterra.terraclient.api.User;
import co.tryterra.terraclient.exceptions.TerraRuntimeException;
import co.tryterra.terraclient.models.Athlete;
import co.tryterra.terraclient.WebhookHandlerUtility
```

// Using the Spark framework (http://sparkjava.com)
public Object handle(Request request, Response response) {
  String payload = request.body();
  String sigHeader = request.headers("terra-signature");
  
  
  // Find your secret on https://dashboard.tryterra.co/dashboard/connections
  WebhookHandlerUtility handlerUtility = WebhookHandlerUtility("SIGNING_SECRET");
  
  Bool validSignature = handlerUtility.verifySignature(sigHeader, payload);
  
  if (!validSignature) {
    // the signature is invalid
    response.status(401);
    return "";
  }
  
  // Deserialize the object inside the event & handle the event
  // ...
  response.status(200);
  return "";
}

We recommend that you use our official libraries to verify webhook event signatures. You can however create a custom solution by following this section.

The terra-signature header included in each signed event contains a timestamp and one or more signatures that you must verify.

  • the timestamp is prefixed by t=

  • each signature is prefixed by a scheme. Schemes start with v, followed by an integer. (e.g. v1)

terra-signature:
t=1492774577,
v1=5257a869e7ecebeda32affa62cdca3fa51cad7e77a0e56ff536d0ce8e108d8bd,
v0=6ffbb59b2300aae63f272406069a9788598b792a944a07aba816edb039989a39

To create a manual solution for verifying signatures, you must complete the following steps:

Step 1: Extract the timestamp and signatures from the header

Split the header using the , character as the separator to get a list of elements. Then split each element using the = character as the separator to get a prefix and value pair.

The value for the prefix t corresponds to the timestamp, and v1 corresponds to the signature (or signatures). You can discard all other elements.

Step 2: Prepare the signed_payloadstring

The signed_payload string is created by concatenating:

  • The timestamp (as a string)

  • The character .

  • The actual JSON payload (that is, the request body)

Compute an HMAC with the SHA256 hash function. Use the endpoint’s signing secret as the key, and use the signed_payload string as the message.

Compare the signature (or signatures) in the header to the expected signature. For an equality match, compute the difference between the current timestamp and the received timestamp, then decide if the difference is within your tolerance.

To protect against timing attacks, use a constant-time-string comparison to compare the expected signature to each of the received signatures.

IP Whitelisting

IP Whitelisting allows you to only allow requests from a preset list of allowed IPs. An attacker trying to reach your URL from an IP outside this list will have their request rejected.

The IPs from which Terra may send a Webhook are:

  • 18.133.218.210

  • 18.169.82.189

  • 18.132.162.19

  • 18.130.218.186

  • 13.43.183.154

  • 3.11.208.36

  • 35.214.201.105

  • 35.214.230.71

  • 35.214.252.53

Retries

If your server fails to respond with a 2XX code (either due to timing out, or responding with a 3XX, 4XX or 5XX HTTP code), requests to it will be retried with exponential backoff around 8 times over the course of just over a day.

SQL Database

SQL databases are easy to set up and often the go-to choices for less abstracted storage solutions. Terra currently supports Postgres & MySQL.

Setup

Next, create a user with enough permissions to create tables & have read & write access within those tables. You can execute the scripts below based on your database

CREATE USER terra_user WITH PASSWORD 'your_password';
GRANT CONNECT ON DATABASE your_database_name TO terra_user;
GRANT USAGE ON SCHEMA public TO terra_user;
GRANT CREATE ON SCHEMA public TO terra_user;
ALTER DEFAULT PRIVILEGES IN SCHEMA public 
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO terra_user;
CREATE USER 'terra_user'@'%' IDENTIFIED BY 'your_password';
GRANT CREATE, SELECT, INSERT, UPDATE, DELETE ON your_database_name.* TO 'terra_user'@'%';
REVOKE ALL PRIVILEGES ON your_database_name.existing_table FROM 'terra_user'@'%';

Data Structure

Data will be stored in tables within your SQL database following the structure below:

Supabase

Setup

You'll then need to enter the above details (host, bucket name, and API key) into your Terra Dashboard when adding the Supabase destination

Data Structure

AWS Destinations

All AWS-based destinations follow the same authentication setup.

Setup

IAM User Access Key

Role-based access

In order to use role-based access, attach the following policy to your bucket:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::760292141147:role/EC2_Instance_Perms"
      },
      "Action": [
        "s3:GetObject",
        "s3:PutObject",
        "s3:DeleteObject"
      ],
      "Resource": "arn:aws:s3:::your-bucket-name/*"
    }
  ]
}

GCP Destinations

Buckets: AWS S3/GCP GCS

Data Structure

As shown above, the name will either be a concatenation of one of the below:

AWS SQS (Simple Queue Service)

Data Structure

Data sent to SQS can either take the type of healthcheck or s3_payload. See Event Types for further details.

Each of these payloads will be a simple JSON payload formatted as in the diagram above. the url field in the data payload will be a download link from which you will need to download the data payload using a GET request. This is done to minimize the size of messages in your queue.

Note: Terra offers the possibility of using your own S3 bucket in combination with the SQS destination. For setting this up, kindly contact Terra support.

Terra generates signatures using a hash-based message authentication code () with . To prevent , ignore all schemes that aren’t v1

Step 3: Determine the expected signature

Step 4: Compare the signatures

You will need to ensure your SQL database is publicly accessible. As a security measure, you may implement using the list of IPs above.

will be created in the terra_users table, will be placed in the terra_data_payloads, and all other sent will be added to the terra_other_payloads.

When using as a destination, Terra handles data storage for you. All will be stored in a Terra-owned S3 bucket, and the download link for each payload will be found under the payload_url column

offers the best of both worlds in terms of allowing you to have both and . Both coexist within the same platform, making development a breeze.

with an appropriate name (e.g. terra-payloads) in your project

within your project. You do not need to add columns to it, Terra will handle that when connecting to it.

for access to your supabase project. Terra will need read & write access to the previously created resources in steps 1 and 2.

When using Supabase (since you get the best of SQL and S3 buckets ) Terra stores the data in the same structure as for and for . Follow those sections for more detailed information on how that is stored!

The most basic way to allow Terra to write to your AWS resource is to with access limited to the resource you're trying to grant Terra access to. for access to the specific resource, (write access is generally the only one needed, unless specified otherwise)

You'll need to create a service account, and generate credentials for it. See the guide for further details. once you have generated credentials, download the JSON file with the credentials you just created, and upload it in the corresponding section when setting up your GCP-based destination

Terra allows you to connect an as a destination, to get all data dumped directly into a bucket of your choice. Follow the /GCP Setup section for setting up credentials for it

When data is sent to your or , it will be dumped using the following folder structure

objects will be placed under the appropriate (in the screenshot above, this corresponds to 2022-03-16. Non versioned objects (e.g. ) will be placed in their appropriate event type folder, outside of the version folder

In all, every event will have as a parent folder the which it corresponds to, and will be saved with a unique name identifying it.

For : the user ID & the start time of the period the event refers to

For all other : the user ID & timestamp the event was generated at

is a managed queuing system allowing you to get messages delivered straight into your existing queue, minimizing the potential of disruptions and barriers that may occur when ingesting a request from Terra API to your server.

The URL will be a to an object stored in one of Terra's S3 buckets.

HMAC
SHA-256
downgrade attacks
Supabase
storage buckets
Postgres SQL tables
Create a storage bucket
Create a table
Create an API key
create an IAM user
Attach relevant policies
here
S3 Bucket
GCS
Event Type
Event Types
AWS SQS
pre-signed link
IP whitelisting
🎉
SQL
S3 Buckets
S3 Bucket
AWS Setup
payloads
SQL
Destination
Users
Versioned
API version
Example for AWS S3
data payloads
data payloads
authentication Events
Data Events