For the complete documentation index, see llms.txt. This page is also available as Markdown.

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

How authentication works

  1. The user clicks Connect device in your app.

  2. Your frontend asks your backend for a widget URL.

  3. Your backend creates a widget session with POST /api/widget/session. See Terra authentication widget.

  4. Your frontend opens the widget URL in a new tab, or an in-app browser on mobile.

  5. The user picks their provider and logs in.

  6. The widget redirects to your success or failure URL with user_id and reference_id as query parameters.

  7. Terra sends an auth event 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.

terminalRun a user through auth and read the auth event
spinner

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.

terminalInspect auth events and why one failed

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:

  1. Your backend gets a 201 from POST /api/widget/session and reads the url field. With a custom UI, it gets a 200 from POST /auth/authenticateUser and reads auth_url.

  2. The end user lands on your success redirect after connecting.

  3. Your destination receives an auth event with status: success.

  4. You store the user_id from that event against your own user.

Next steps

Last updated

Was this helpful?