User authentication
Connect an end user's wearable or health app to Terra, then store the user ID from the auth event.
Connecting a user lets them grant your app access to a provider such as Fitbit, Garmin or Oura. Once they connect, Terra pushes their data to your destination as events, with no polling. Terra stores and refreshes the provider tokens for you.
Before you begin
Get your API key and dev ID from the Terra dashboard.
Set up a destination where Terra sends events.
Enable the data sources your users can connect.
How authentication works
The user clicks Connect device in your app.
Your frontend asks your backend for a widget URL.
Your backend creates a widget session with
POST /api/widget/session. See Terra authentication widget.Your frontend opens the widget URL in a new tab, or an in-app browser on mobile.
The user picks their provider and logs in.
The widget redirects to your success or failure URL with
user_idandreference_idas query parameters.Terra sends an
authevent to your destination.
From then on, the user's data reaches your destination automatically.
Try it from the dashboard
To see the flow before writing any code, connect your own device from the Terra dashboard. Your end users don't connect this way: live users go through the API.
Handle the auth event
Terra sends an authentication event to your destination whenever a user connects, disconnects, fails to connect, reconnects, or has their access revoked. Each event carries the user_id, the auth status and a message.
A successful auth event:
Every authentication event format is listed under Event types. All events you have received are in Dashboard > Events.
Store the user ID
Save the user_id from the auth event against your own user. Every data event carries this user_id, so without it you can't tell whose activity or sleep data has arrived.
The user_id is also on the success redirect:
When the user ID changes
Connecting the same provider again with the same reference_id and dev_id deletes the previous user record and creates a new user_id. This is per provider: reconnecting Fitbit doesn't affect an Apple Health connection under the same reference_id.
The old user_id disappears from the Users tab in your dashboard, though Payload History still shows data sent to it. Terra sends a user_reauth event with both the old and new user_id. Handle it and update the stored user_id, or new data won't match your records.
Check your setup
Your setup is working when:
Your backend gets a
201fromPOST /api/widget/sessionand reads theurlfield. With a custom UI, it gets a200fromPOST /auth/authenticateUserand readsauth_url.The end user lands on your success redirect after connecting.
Your destination receives an
authevent withstatus: success.You store the
user_idfrom that event against your own user.
Next steps
Terra authentication widget: create a widget session and open the hosted connect screen.
Custom authentication UI: build your own provider buttons and generate login links.
Authentication redirect pages: send the end user back to your app after they connect.
Last updated
Was this helpful?