WHOOP Integration series Part 3: Integrating with Terra API
Adding WHOOP as an integration to your app is fairly straightforward thanks to the standardized integration method. Following the documentation, you simply have to implement: auth flow, data gathering, and data processing.
However, if you are building an app utilizing fitness data, you most likely are not gonna want to stop at one data provider. This means you would need to build multiple auth flows, multiple data gathering methods, and data processing systems one for each integration. How about instead of doing this, you make one integration only, and this provides you access to all the wearable companies as well (including WHOOP)?
This is where Terra comes in.
Authentication
Like any data flow, authentication is first and foremost, the most important part. You need to be able to get the user's consent to retrieve their data. This can be done through our (customizable) widget:
curl --location --request POST 'https://api.tryterra.co/v2/auth/generateWidgetSession' \
--header 'dev-id: DEV-ID' \
--header 'X-API-Key: X-API-KEY'
Where DEV-ID
and X-API-KEY
are credentials you receive when you sign up with Terra.
This will provide you with a widget URL that you can display to your user to log in with whatever provider they want:
{
"expires_in": 900,
"session_id": "aece836a-7d69-4489-82cb-1c348e68097e",
"status": "success",
"url": "https://widget.tryterra.co/session/aece836a-7d69-4489-82cb-1c348e68097e"
}
The flow would look like this:
The user is uniquely identified by a user_id
provided at the auth success page (last in the image above) URL's query parameters.
Data Gathering
That's it! When you have signed up with us, you can set up a webhook URL (under API -> Customise) for which we will continuously send you new events for the user that is authenticated.
Data Processing
The data sent to your webhooks are all of the same structure. They will always contain the same formatting, same field names, and same units. For example, a webhook event from any provider (for example WHOOP) for a workout your user completed will always look like this:
This makes processing events from Terra very simple and elegant as you simply need to pick off points you need from the payload for whatever your app requires.
Advanced
Now, what if you want historic data as well? Tera also has endpoints that allow you to retrieve historical data! You can make a simple HTTP request formatted as:
curl --location --request GET 'https://api.tryterra.co/v2/activity?end_date=2022-10-29&start_date=2022-10-28&user_id=USER_ID' \
--header 'dev-id: DEV-ID' \
--header 'X-API-Key: X-API-KEY'
Where again, DEV-ID
and X-API-KEY
are credentials you receive when you sign up for Terra. USER_ID
on the other hand, is the unique identifier we provide to you after authentication completion to identify the user that has authenticated. The request above with the requested start_date
and end_date
will gather (and send) the workouts the user has completed over that period of time to your webhook.
Buy or Build
Terra API follows a very simple and easy-to-use structure that allows integrations to WHOOP, Garmin, Fitbit, and hundreds of fitness wearables in one single integration.
You may think that if you can do one, you can go over and do all the others as much as your app would want to support.
However, there are many things to consider, such as partnerships, parsing different data models from other companies, changing your auth system slightly for every company you want to integrate with, and at the same time building a frontend structure to display all of your integrations! These can make a huge difference between if you want to buy or build in-house.
In our Integration series on WHOOP, we are discussing the data available from our API when fetching data from WHOOP.
As a reminder, the WHOOP series contains: