@0x3639/intervals-icu
    Preparing search index...

    Wellness

    client.wellness reads and writes daily wellness records such as weight, HRV, sleep and resting heart rate. Records are addressed by date, and can be created, updated individually, updated in bulk, or uploaded from a CSV file.

    Method What it does
    WellnessService.listWellness List wellness records for a date range
    WellnessService.getWellnessByDate Get a single wellness record by date (ISO-8601 local date)
    WellnessService.createWellness Create or update a wellness entry (PUT /athlete/{id}/wellness)
    WellnessService.updateWellness Update an existing wellness entry by date
    WellnessService.updateWellnessBulk Bulk update wellness records
    WellnessService.uploadWellnessCSV Upload wellness data from CSV file

    List the last 30 days of wellness records, print the most recent week, and fetch today's record, treating a 404 as "no record yet".

    const client = new IntervalsClient({ apiKey });

    const localDate = (d: Date) => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
    const now = new Date(); // one timestamp for both bounds
    const monthAgo = new Date(now);
    monthAgo.setDate(monthAgo.getDate() - 29);
    const newest = localDate(now);
    const oldest = localDate(monthAgo);

    const records = await client.wellness.listWellness({ oldest, newest });
    if (records.length === 0) {
    console.log('No wellness records in the last 30 days');
    } else {
    for (const record of records.slice(-7)) {
    console.log(`${record.date ?? ''} weight=${record.weight ?? 'n/a'} restingHR=${record.restingHR ?? 'n/a'} hrv=${record.hrv ?? 'n/a'}`);
    }
    }

    const today = localDate(now);
    try {
    const todayRecord = await client.wellness.getWellnessByDate(today);
    console.log(`Today: weight=${todayRecord.weight ?? 'n/a'}`);
    } catch (err) {
    if (err instanceof IntervalsAPIError && err.status === 404) {
    console.log('No record for today');
    } else {
    throw err;
    }
    }

    Update today's wellness entry with a weight, read it back, then put the previous weight back.

    CI never runs this example; running it by hand changes the authenticated account: it writes a demo weight to today's record and restores the previous one in a finally block. It writes only when it can restore — WellnessInput.weight is typed number | undefined, so the SDK cannot clear the field again, and a demo value written to a record that had no weight would stay there for good. When today's record has no weight yet the example says so and exits without writing.

    "Today" is resolved in the athlete's own time zone (Athlete.timezone), not the machine's: the API keys wellness records by the athlete's local date.

    // maxRetries: 0 — a create that committed before a 5xx would be repeated by a retry, and only the last response's id would be cleaned up.
    const client = new IntervalsClient({ apiKey, maxRetries: 0 });

    // The API keys wellness records by the athlete's local date, not the machine's. A machine
    // in a different time zone (or a CI runner on UTC) would otherwise read one day's record
    // and write another's, so the date is formatted in the athlete's zone (see
    // examples/_shared/local-date.ts).

    const me = await client.athletes.getAthlete();
    const today = localDateIn(new Date(), me.timezone);

    // Read the current record first so the demo value can be put back. A 404 means
    // there is no record for today yet, which is the same as "nothing to restore".
    let previousWeight: number | undefined;
    try {
    const existing = await client.wellness.getWellnessByDate(today);
    if (typeof existing.weight === 'number') previousWeight = existing.weight;
    } catch (err) {
    if (!(err instanceof IntervalsAPIError) || err.status !== 404) throw err;
    }

    if (previousWeight === undefined) {
    // Writing here would leave the demo weight on the record permanently, so do not write.
    console.log(`No weight on ${today}'s record to restore; writing nothing and exiting`);
    } else {
    try {
    await client.wellness.updateWellness(today, { weight: 72.5 });

    const record = await client.wellness.getWellnessByDate(today);
    console.log(`${record.date ?? today}: weight=${record.weight ?? 'n/a'}`);
    } finally {
    await client.wellness.updateWellness(today, { weight: previousWeight });
    console.log(`Restored weight=${previousWeight}`);
    }
    }