React Native
Sync your users' Apple Health data to Sonar from a React Native or Expo app.
Android
The package builds and runs on Android, so one codebase ships to both platforms. It turns on Health Connect when the Android SDK opens to everyone; it is in early access today. Until then, Sonar calls on Android reject with platform_unsupported, signOut resolves and getState reports signed_out. Call Sonar only when Platform.OS === "ios".
@sonarhealth/react-native-sdk is a thin Expo module over the iOS SDK. The iOS SDK ships inside the npm package, so there is no separate pod to add. Sync, background work and the session run natively, so they keep working when JavaScript is not running.
A working Expo app and a demo backend are in sonar-sdk-examples.
Before You Start
- An SDK app ID for your iOS bundle ID, from Atlas → Developers → Apps. Sandbox and live have separate app IDs: build with the one that matches the API key your backend uses.
- A client-token endpoint on your backend. It calls
POST /v1/users/{id}/sdk-sessionswith your API key and returns theclient_tokento the app. See SDK Authentication. - A Sonar user for each of your users (
POST /v1/users).
The app never holds a Sonar API key.
Requirements
| Area | Requirement |
|---|---|
| Expo | SDK 54 or later. The New Architecture is supported. |
| iOS | 16 or later. The config plugin raises the deployment target to 16.0 when it is lower. |
| Build | A development build (npx expo run:ios or EAS). Expo Go cannot load the native module. |
| Device | An iPhone for real testing. The simulator’s Health app has no data unless you add it. |
Install
npx expo install @sonarhealth/react-native-sdkThis adds the config plugin to your app config without options, and prebuild stops until you set appId. Add it:
{
"expo": {
"plugins": [
[
"@sonarhealth/react-native-sdk",
{
"appId": "app_Qm3t8Vx1LmN0aB7cD9eF2g",
"healthShareUsageDescription": "Reads your health data to show your activity, sleep and vitals."
}
]
]
}
}| Option | Required | What it does |
|---|---|---|
appId |
Yes | Your SDK app ID. The SDK is configured with it natively at every launch. |
historyDays |
No | Days of history a new connection imports, from 30 to 1825. The default is 730. See History Period. |
healthShareUsageDescription |
No | The text iOS shows in the Health permission sheet. There is a generic default; write your own. |
privacyPolicyUrl |
No | For Android, where Health Connect shows it to the user. |
At prebuild, the plugin writes:
- the app ID and history period into
Info.plist, read at launch to configure the SDK before any JavaScript runs; NSHealthShareUsageDescription;- the HealthKit entitlement with Background Delivery;
- the background modes
fetchandprocessing, and the task identifiersco.sonarhealth.sdk.refreshandco.sonarhealth.sdk.backfill; - an iOS deployment target of at least 16.0, for the app and its pods.
Then build: npx expo prebuild and npx expo run:ios, or an EAS build. Changing appId needs a new build. On a phone where a user is already signed in, the SDK keeps the old app ID until signOut().
Bare React Native
Run npx install-expo-modules first, then install the package. Config plugins do not run in a bare app, so make the same changes in Xcode yourself: follow Set Up the App Target on the iOS page, and add the Info.plist key that lets the SDK configure itself at launch (package 0.1.2 or later):
<key>SonarAppId</key>
<string>app_Qm3t8Vx1LmN0aB7cD9eF2g</string>To choose a History Period other than two years, add SonarHistoryDays too:
<key>SonarHistoryDays</key>
<integer>365</integer>Use
import { Platform } from "react-native";
import Sonar from "@sonarhealth/react-native-sdk";
export async function startSonar() {
if (Platform.OS !== "ios") return;
await Sonar.authenticate({
userId: currentUser.id,
clientTokenProvider: async ({ appId, installationId }) => {
const response = await myBackend.sonarClientToken({ appId, installationId });
return response.client_token;
},
});
const unsubscribe = Sonar.observeState((state) => render(state));
await Sonar.connect("apple_health");
}Authenticate when your user is signed in
Call authenticate as soon as your app knows who is signed in: right after the user signs in, whenever that happens, and at every launch once your app has restored a signed-in user. The session is kept natively, so this usually resolves already_authenticated. It still registers the token provider, and the SDK can renew an ended session silently only while one is registered.
Observe state
observeState calls your listener with the current state at once, then on every change. It returns the unsubscribe function. getState() and getProviderState("apple_health") read the current state without subscribing. backfill.fraction (0 to 1) gives import progress; dates are ISO 8601 strings.
Connect Apple Health
connect("apple_health") shows the HealthKit permission sheet, records the connection in Sonar and starts syncing. Call it from a user action, such as a “Connect Apple Health” button.
You do not need to call Sonar.configure: the plugin configures the SDK natively at launch, which background sync needs. It exists for apps that want to be explicit; calling it with the same app ID is harmless.
The package exports Sonar as both the default and a named export, and the types (SonarState, ProviderState, results and error codes) as named exports.
Calls
| Call | What it does |
|---|---|
sync() |
Sends new data now |
resync("apple_health") |
Reads and sends the history period again. Deletes nothing |
disconnect("apple_health") |
Stops syncing and deletes the user’s Apple Health data in Sonar |
signOut() |
Ends the Sonar session on this phone and clears its credentials. The connection and data stay in Sonar. When the same user signs in again, syncing picks up where it stopped. Call it only when the user signs out of your app |
diagnostics() |
Returns the SDK’s own log, with no health values |
sendDiagnostics() |
Uploads that log to Sonar. Quote the returned receiptId when you contact support. |
Failed calls reject with a SonarSdkError, exported by the package:
import { SonarSdkError } from "@sonarhealth/react-native-sdk";
try {
await Sonar.connect("apple_health");
} catch (error) {
if (error instanceof SonarSdkError) {
console.log(error.code, error.retryable, error.recoveryAction);
}
}The SDK Interface lists every result, state and error code.
Permissions, Limits and App Store
The native iOS SDK does the work, so everything on the iOS page applies unchanged:
- Permissions: iOS never reports read denial, so the permission status stays
requested. - Limits: deletions, edits, one installation per user, locked phones, background timing.
- Privacy and App Store: the privacy manifest ships inside the framework in the npm package. You still declare health data in App Store Connect and publish a privacy policy.
Sonar