React Native SDK 1.x
Connect Apple Health, Samsung Health and Health Connect users with the terra-react 1.x package: install it, initialise it, connect a user and enable background delivery.
This page shows how to integrate Apple Health, Samsung Health and Health Connect in your React Native app with terra-react 1.x. It covers SDK initialization, user connection, background delivery, and reading and writing data.
Install and set up the plugin
Install the
terra-reactpackage using npm (npm install terra-react).Complete the following iOS and/or Android setup.
In your terminal, cd to your
/iosfolder, and runpod installto install all the dependencies.Add Capabilities:
Healthkit > Healthkit Background Delivery
Background Modes > Background processing
Background modes > Background fetch
Add the following keys to your info.plist:
Option 1: Use Xcode
1) Go to the
/iosdirectory of your project, open the.xcworkspacein XCode.2) Go to info.plist, and add the following keys and values:
Privacy - Health Share Usage Description
Description of how Health data is used
(Min 3 words)
Privacy - Health Records Usage Description
Description of how Health data is used
(Min 3 words)
Privacy - Health Update Usage Description
Description of how Health data is used
(Min 3 words)
Permitted background task scheduler
co.tryterra.data.post.request
Option 2: Edit info.plist directly
1) In your app project, go to your
/iosfolder2) Go to info.plist, and add the following tags:
<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
<string>co.tryterra.data.post.request</string>
</array>
<key>NSHealthClinicalHealthRecordsShareUsageDescription</key>
<string>Using TerraiOS to gather health data</string>
<key>NSHealthShareUsageDescription</key>
<string>Using TerraiOS as a mean of getting Health Data</string>
<key>NSHealthUpdateUsageDescription</key>
<string>Allow writing data to health kit</string>Access Samsung Health
Terra has a privileged partnership with Samsung that gives you direct access to the Samsung Health SDK, with no Health Connect intermediary. This is the recommended route for Samsung devices.
1. Apply for Samsung Health access
Submit your application through the Samsung Health partnership portal.
2. Install the Samsung-tagged SDK
After approval, install the Samsung-tagged version of the Terra SDK:
React Native:
npm install terra-react@<samsung-version>. Find Samsung-tagged versions on the npm versions pageFlutter: Use the Samsung-tagged version of
terra_flutter_bridge. Find versions on pub.dev
3. Add the ProGuard rule (required for release builds)
In your android/app/proguard-rules.pro, add:
Without this, R8 minification will strip Samsung SDK classes and cause runtime crashes in production.
4. Check the requirements
Samsung Health app must be installed on the device
Android 28 (minSDK 28) and above
Enable Developer Mode in Samsung Health on test devices (Settings > About Samsung Health > tap version number repeatedly)
No additional manifest or Gradle changes needed: the Terra SDK handles everything
Access Health Connect
1. Grant permissions in the Health Connect app
Give all permissions between the apps you wish to read from (e.g. Samsung Health, Google Fit, etc) & Health Connect.
2. Add the Health Connect capability to your app
In your app project, go to the /android folder.
Include the permission tags under the Activity you wish to link the user to when they click the privacy policy link in the Health Connect permission screen. Here are the steps:
Go to your Android App's
AndroidManifest.xmlGo to your Privacy Policy
<activity>and include the following tags under this<activity>tag.
"Your Android manifest needs to have an Activity that displays your app's privacy policy, which is your app's rationale of the requested permissions, describing how the user's data is used and handled." (Health Connect)
Initialize the SDK
The first step is to initialize the Terra SDK.
The initialization only needs to be done once on app start (for example in your app.tsx or an equivalent file), and ideally every time the app is brought into the foreground. This ensures that the SDK is properly set up and ready to be used.
Import the Terra plugin
In your project, you should now be able to import function from the Terra Plugin. Here is an example:
Call initTerra
In order to interact with the SDK, you need to call initTerra first.
Call initTerra() with the following arguments:
devId: Your Developer ID provided by Terra.referenceId: An ID of your choice to identify your app user.
Find more details in the SDK Reference: React Native SDK reference
This call is asynchronous. Make sure it completes before you use other SDK functions.
Connect a user
Once Terra is initialized, you can create a connection by calling the function initConnection() to trigger a permission screen pop up.
Call initConnection()
From terra-react, import initConnection and call the function with the following arguments:
type: Specify the connection type:Connections.APPLE_HEALTH,Connections.SAMSUNG, orConnections.HEALTH_CONNECTtoken: A one-time authentication token generated from your backend server. This ensures secure communication between your app and the Terra servers.schedulerOn:iOS: This parameter has no effect. Background delivery is controlled by calling
Terra.setUpBackgroundDelivery()in your AppDelegate. See Set up background delivery below.Android: Enables background delivery, so Terra keeps syncing while your app is in the background. See Set up background delivery below.
customPermissions: A set of permissions that define what data you want to request (e.g., heart rate, steps). If empty, it defaults to all available permissions. See Permissions mapping for every value and what it requests on each platform.
Generate an auth token
To be able to call the initConnection() method, you need to pass a token as an argument.
This token is a single-use token created to ensure the authentication endpoint for creating a connection (and connecting the SDK to Terra's servers) does not get abused.
Generate the token with this endpoint POSThttps://api.tryterra.co/v2/auth/generateAuthToken . Make sure to call it with your Terra x-api-key and dev-id in the headers from your backend server. After you generate the token, provide the response to your client side using your own logic.
For every parameter, see the /auth/generateAuthToken endpoint in the API reference.
Validate the connection
To ensure a connection is still valid on the client side, use the getUserId method. This function returns the user_id for the connection or null if none exists.
Set up background delivery
Background delivery allows data to be synced even when your app is not brought to the foreground. iOS needs a setup step in your AppDelegate; on Android it is enabled through initConnection.
No AppDelegate step is needed. Passing true as the third argument (schedulerOn) to initConnection enables background delivery.
On Health Connect connections, Terra requests Health Connect's background-read permission during the connection flow and schedules the periodic sync as soon as the user grants it.
Optional: Filter by source app on iOS
Apple Health aggregates data from every health app on the user's device. If a user has both a cloud-based Terra connection (e.g. WHOOP, Garmin) and the same provider's companion app syncing into Apple Health, you'll receive duplicate data: once from the cloud API and once from the Apple Health SDK.
Use setIgnoredSources to tell the SDK to skip data from specific apps in HealthKit. Call this once on every app launch, after initTerra() completes. It is not persisted across app restarts. On Android, this function is a no-op.
Common bundle identifiers:
WHOOP
com.whoop.app
Garmin Connect
com.garmin.connect.mobile
Fitbit
com.fitbit.FitbitMobile
Oura
com.ouraring.oura
You now receive health data events automatically at your destination (for example, a webhook).
You can also request historical data to backfill, to verify that data exists, or as a fallback.
Check out the React Native SDK reference for details about all the functions in the SDK.
Disconnect a user
In order to disconnect an SDK user, you may use the same endpoint as for Web API-based integrations, called from your backend.
Request historical data
You can request for historical data using one of the data retrieval functions.
You may set toWebhook to false if you wish for the callback function to return the data payload on the client side.
Check out the React Native SDK reference for details about all the functions in the SDK
Write data
You may write data into Apple Health (Health Connect not yet supported) through one of the helper functions in the SDK
Last updated
Was this helpful?