Skip to content

@gj-kit/expo-workouts recipes

Each code block reuses a public-contract example already type-checked in the package README.

1. Check native availability before requesting access

Section titled “1. Check native availability before requesting access”

Start with availability and permissions in a development build, not a web or Expo Go fallback.

Outcome: A native Expo app can check HealthKit or Health Connect availability and request workout access.

Application-owned boundary: Add the config plugin and use a development build; Expo Go and the web cannot call the native module.

import { getAvailability, requestAuthorization } from '@gj-kit/expo-workouts';
export async function requestWorkoutAccess() {
const availability = await getAvailability();
if (availability.status === 'available') {
await requestAuthorization({ read: ['workouts'] });
}
return availability;
}

Read a native ID only in the saved state and preserve why the platform cannot write.

The two saveWorkout mistakes that only surface in production — a forgotten route and an unhandled locked device — are a TS2741 and a TS2339.

import { workouts } from '@gj-kit/expo-workouts';
import type { WorkoutWrite } from '@gj-kit/expo-workouts/core';
export const write: WorkoutWrite = {
id: 'a2f6c0b8-0d7e-4f31-9d1a-7c2b5e8f0a11', // your own idempotency key, not the platform's
version: 3, // never Date.now(): a crash retry would mint a second workout
kind: 'running',
startMs: 1_754_000_000_000,
endMs: 1_754_000_600_000,
route: 'none', // delete this line -> TS2741; an Android upsert without it erases the stored route
};
export async function save(): Promise<string | null> {
const saved = await workouts.saveWorkout(write);
// @ts-expect-error TS2339 — `nativeId` is absent on the `pendingUnlock` branch
void saved.nativeId;
if (saved.status === 'pendingUnlock') return null; // locked device: retry the same id + version
return saved.nativeId; // narrowed past pendingUnlock, so it exists
}

Use the complete API reference for every export and import path.