Apple Watch Integration

Apple Watch Integration

Apple Watch provides continuous heart rate and HRV data via HealthKit on iOS. Because Apple Watch cannot make HTTP requests to third-party APIs directly, an iOS companion app is required to bridge the data to Nefesh.

How It Works

  1. Apple Watch writes HR and HRV data to HealthKit on the paired iPhone
  2. Your iOS app reads from HealthKit using HKHealthStore
  3. Extract heart rate and HRV (SDNN) samples
  4. POST the data to Nefesh via /v1/ingest

Important Note

Apple Watch apps cannot call third-party HTTP APIs directly. You need a companion iOS app that reads HealthKit data and forwards it to Nefesh. Alternatively, use the Watch Connectivity framework to send data from the watch to the phone app, which then calls the API.

Reading from HealthKit (Swift)

import HealthKit

let healthStore = HKHealthStore()

// Request authorization
let typesToRead: Set = [
    HKQuantityType(.heartRate),
    HKQuantityType(.heartRateVariabilitySDNN)
]
try await healthStore.requestAuthorization(toShare: [], read: typesToRead)

// Query latest heart rate
let hrType = HKQuantityType(.heartRate)
let query = HKSampleQuery(
    sampleType: hrType,
    predicate: HKQuery.predicateForSamples(withStart: Date().addingTimeInterval(-60), end: Date()),
    limit: 1,
    sortDescriptors: [NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)]
) { _, samples, _ in
    guard let sample = samples?.first as? HKQuantitySample else { return }
    let bpm = sample.quantity.doubleValue(for: HKUnit.count().unitDivided(by: .minute()))

    // POST to Nefesh /v1/ingest
    let payload: [String: Any] = [
        "session_id": sessionId,
        "timestamp": ISO8601DateFormatter().string(from: Date()),
        "heart_rate": bpm,
        "source_device": "apple_watch"
    ]
    // Send via URLSession...
}
healthStore.execute(query)

Supported Apple Watch Models

  • Apple Watch Ultra 2
  • Apple Watch Series 9 / 10
  • Apple Watch SE (2nd gen)
  • Any Apple Watch with optical heart sensor (Series 1+)

Resources