Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/kingstinct/react-native-healthkit/llms.txt

Use this file to discover all available pages before exploring further.

This guide will help you build your first HealthKit integration, from requesting authorization to reading your first health sample.
Make sure you’ve completed the installation before continuing.

Check HealthKit availability

First, verify that HealthKit is available on the device:
import { isHealthDataAvailable } from '@kingstinct/react-native-healthkit';

const isAvailable = isHealthDataAvailable();

if (!isAvailable) {
  console.log('HealthKit is not available on this device');
}

Request authorization

Before reading or writing any health data, you must request authorization from the user.
Failing to request authorization before accessing HealthKit data will cause your app to crash. Always request authorization first!
import { requestAuthorization } from '@kingstinct/react-native-healthkit';

await requestAuthorization({
  toRead: ['HKQuantityTypeIdentifierStepCount', 'HKQuantityTypeIdentifierHeartRate'],
  toShare: ['HKQuantityTypeIdentifierStepCount']
});
Parameters:
  • toRead - Array of data types you want to read
  • toShare - Array of data types you want to write
Users can deny permission for specific data types. Always handle the case where permissions might not be granted.

Read your first sample

Once authorized, you can query health data. Here’s how to get the most recent step count:
import { getMostRecentQuantitySample } from '@kingstinct/react-native-healthkit';

const stepData = await getMostRecentQuantitySample('HKQuantityTypeIdentifierStepCount');

console.log(`Steps: ${stepData.quantity}`);
console.log(`Unit: ${stepData.unit}`);
console.log(`Date: ${stepData.startDate}`);

Complete example with React hooks

Here’s a complete example using React hooks for a more declarative approach:
App.tsx
import React, { useEffect, useState } from 'react';
import { View, Text, Button, ActivityIndicator } from 'react-native';
import {
  useHealthkitAuthorization,
  useMostRecentQuantitySample,
  isHealthDataAvailable
} from '@kingstinct/react-native-healthkit';

export default function App() {
  const [isAvailable, setIsAvailable] = useState(false);
  const [authorizationStatus, requestAuthorization] = useHealthkitAuthorization(
    ['HKQuantityTypeIdentifierStepCount']
  );

  useEffect(() => {
    setIsAvailable(isHealthDataAvailable());
  }, []);

  const stepData = useMostRecentQuantitySample('HKQuantityTypeIdentifierStepCount');

  if (!isAvailable) {
    return <Text>HealthKit is not available on this device</Text>;
  }

  return (
    <View style={{ padding: 20 }}>
      <Text style={{ fontSize: 24, marginBottom: 20 }}>HealthKit Example</Text>

      {authorizationStatus === 'unnecessary' ? (
        <View>
          {stepData ? (
            <View>
              <Text style={{ fontSize: 18 }}>Most Recent Steps</Text>
              <Text>Steps: {stepData.quantity}</Text>
              <Text>Unit: {stepData.unit}</Text>
              <Text>Date: {stepData.startDate.toLocaleString()}</Text>
            </View>
          ) : (
            <ActivityIndicator />
          )}
        </View>
      ) : (
        <Button
          title="Request HealthKit Authorization"
          onPress={requestAuthorization}
        />
      )}
    </View>
  );
}
1

Check availability

The app checks if HealthKit is available using isHealthDataAvailable().
2

Request authorization

The useHealthkitAuthorization hook manages the authorization state and provides a function to request permissions.
3

Fetch data

Once authorized, useMostRecentQuantitySample automatically fetches the most recent step count and re-fetches when it changes.

Write health data

You can also save health data to HealthKit:
import { saveQuantitySample } from '@kingstinct/react-native-healthkit';

// First, request write permission
await requestAuthorization({
  toShare: ['HKQuantityTypeIdentifierStepCount']
});

// Then save a sample
await saveQuantitySample(
  'HKQuantityTypeIdentifierStepCount',
  'count',
  1000,
  {
    startDate: new Date(),
    endDate: new Date(),
  }
);

Subscribe to changes

Listen for updates when health data changes:
import { subscribeToChanges } from '@kingstinct/react-native-healthkit';
import { useEffect } from 'react';

useEffect(() => {
  const unsubscribe = subscribeToChanges('HKQuantityTypeIdentifierStepCount', () => {
    console.log('Step count data changed!');
    // Refetch your data here
  });

  return () => unsubscribe();
}, []);
Make sure you’ve requested authorization before subscribing to changes, otherwise your app will crash.

Common patterns

Authorization timing

Avoid requesting authorization in the same component where you immediately fetch data:
// ❌ Bad - may crash
function MyComponent() {
  useEffect(() => {
    requestAuthorization({ toRead: ['HKQuantityTypeIdentifierStepCount'] });
  }, []);

  // This hook runs immediately, before authorization completes!
  const stepData = useMostRecentQuantitySample('HKQuantityTypeIdentifierStepCount');
}

// ✅ Good - wait for authorization
function MyComponent() {
  const [hasAuth, setHasAuth] = useState(false);

  useEffect(() => {
    requestAuthorization({ toRead: ['HKQuantityTypeIdentifierStepCount'] })
      .then(() => setHasAuth(true));
  }, []);

  const stepData = hasAuth
    ? useMostRecentQuantitySample('HKQuantityTypeIdentifierStepCount')
    : null;
}

Next steps

Authorization

Learn more about managing HealthKit permissions

Data Types

Explore all available health data types

Reading Data

Advanced querying with filters and anchors

React Hooks

Explore all available hooks

Build docs developers (and LLMs) love