Getting my Apple Health data out of my iPhone

Tonal and Peloton took an afternoon over MCP. Apple Health took Shortcuts, iPhone Mirroring, an AI tapping my phone, and finally an iPhone app of my own.

#miai#claude#mcp#apple-health

MiAi, the personal platform I run at home, started with one question: can I see all of my training in one place? I lift on a Tonal, I ride and row on Peloton, and my Apple Watch records everything else, including heart rate and sleep. Three companies each hold part of the picture, and none of them talk to the others very well.

Getting the data out of Tonal and Peloton took an afternoon. Getting it out of Apple Health took most of a week, two dead ends, and an iPhone app I never planned to write.

Starting on the laptop: MCP made it easy

I didn't start with a platform. I started in Claude Code on my laptop with two MCP servers: an open-source one for Tonal and a community Peloton server I patched to run locally. An MCP server is a small adapter that gives Claude a set of tools, like "list my recent workouts" or "get the metrics for this ride," and handles the API calls behind them.

That made the first step pure exploration. I asked questions in plain English and watched what came back: which fields Tonal returns for a set, how Peloton reports output and heart rate zones, and what's missing. Before writing a line of pipeline code, I knew what the data looked like and which parts were worth keeping.

It wasn't all smooth. Peloton turned off password logins, so the server signs in with a token I copy from a browser session once, and it refreshes itself after that. That was the worst of it. Both services have an API somewhere, and once something can call that API, the data flows. When I later moved the syncing onto the MiAi server, it was the same calls on a timer.

Apple Health is a different animal

Apple Health has no web API. There's no cloud endpoint and no iCloud export you can call from a server. The data lives on the iPhone, and the only way to read it is with code running on that iPhone.

There's a second rule that shaped everything after: iOS locks Health data while the phone is locked. An app or automation that wakes up at 3 AM can't read anything. Every approach has to wait until I've unlocked my phone.

Attempt one: Shortcuts, iCloud and my Mac

Apple's Shortcuts app is free and can read Health data, so I tried that first. The plan:

  1. A Shortcut runs Find Health Samples for each metric and writes one small JSON file to iCloud Drive.
  2. A scheduled job on my Mac picks the file up and uploads it to the MiAi server.
  3. The server loads it into the database.

It worked, in the sense that a file eventually showed up and loaded. Getting there was a string of small fights:

  • Double counting. Daily totals like steps and active energy came out about twice as high as they should. The Shortcut summed every source, so my iPhone and my Watch both counted the same steps. Each of those blocks needs a filter for one source.
  • macOS privacy rules. The background job on the Mac wasn't allowed to read iCloud Drive until I granted it access.
  • File permissions. Files kept the iPhone's private permissions after upload, and the server couldn't read them.
  • No schedule. I asked Shortcuts' new AI editor to run it every morning, and it refused: "Scheduling a time can't trigger a shortcut." Automations are built by hand in a different tab.
  • The lock rule. A time-of-day automation fires while the phone is often still locked, so it reads nothing. The workaround is to trigger it when I open an app, which only happens when I'm holding the phone.

The result was a once-a-day file, routed through a Mac that had to be awake. I wanted fresh data during the day, without the Mac.

Attempt two: letting Claude drive my iPhone

Building Shortcut automations means a lot of tapping through small menus, so I tried having Claude do it. macOS has iPhone Mirroring, which shows my iPhone in a window on the Mac. The Claude desktop app has computer use, so it can see that window and click and type in it.

Claude Code in the terminal can't control the desktop, so it wrote a step-by-step task, and I pasted it into the desktop app. From there Claude worked my iPhone through the mirror: opening Shortcuts, building automations and filling in settings while I watched.

It was impressive to watch, and it didn't solve the problem. Two things got in the way:

  • Mirroring keeps the phone locked. While the iPhone is mirrored, the phone itself stays locked, so the lock rule applies. Test runs had no Health data to send, and they failed without saying why.
  • Two plans at once. At one point the task Claude wrote for the desktop app targeted a paid app we'd discussed earlier, while I was working in Shortcuts. It took a few rounds to get us back on the same page.

Even with every setting right, the lock rule meant Shortcuts could never deliver a reliable feed every half hour. I ruled it out.

The paid option

There's a well-known third-party app, Health Auto Export, that reads Health and posts it to a URL on a schedule. Its automations are a paid upgrade. It would probably have worked, but it's one more subscription in the middle of my own data, and it has to live within the same lock rule.

The real requirement had become clear: something on the phone that runs on its own, wakes up when new Health data arrives, and sends it to my server. Apple supports exactly that for apps, through HealthKit background delivery. Shortcuts can't use it, but an app can.

So I had Claude write one.

MiAi Health: an iPhone app for one user

MiAi Health is a small SwiftUI app. It asks for read access to the Health types MiAi uses and registers for background delivery. When iOS wakes it with new data, it sends the last few days to MiAi's ingest endpoint over HTTPS, using a per-device token that can only upload. On the server, the data lands in the same tables the Shortcut and the full exports used, so nothing downstream changed.

An iPhone home screen. Polished icons for Claude Duet, Aura, Hulu and GitHub sit above MiAi Health, which still has Xcode's blank placeholder icon: a white square of grey guide lines.

The polish level of an app with one user: MiAi Health on my home screen, still wearing Xcode's blank placeholder icon.

The design, build, review and install took an evening, with me doing the parts that needed a human: signing into Xcode, turning on Developer Mode on the phone, and approving permissions. One snag: the first send failed until the phone fully trusted the certificate my home server uses.

The real test was the next morning. I didn't open the app. At 6:28 AM, right after I first unlocked the phone, it sent last night's sleep and the morning's numbers on its own. That's the feed I wanted, with no Mac in the loop.

The duct tape

This is a cobbled-together solution, and I'm fine with that.

Apps outside the App Store have to be signed. With a free Apple ID, the signature on a personal app expires after 7 days, and then the app stops launching. Once a week I plug in the phone and rerun one install script, and it's good for another week. The $99-a-year Apple Developer Program would stretch that to a year. For an app with one user, I'd rather spend 30 seconds a week.

If the weekly reinstall ever gets old, that $99 starts looking reasonable. For now the weekly reinstall is the price of keeping my health data entirely on hardware I own.

A note on duplicates

With three sources there's a catch: they sync with each other. Tonal writes my lifts to Apple Health. Peloton reads workouts from Apple Health, so it gets a copy of my Tonal sessions too. Tonal imports Peloton workouts back as unnamed "Free Lift" entries with zero weight. One strength session can show up three times.

MiAi builds one row per session with a few rules:

  • The source that recorded the workout wins. Tonal owns strength sessions and Peloton owns rides and rows. An overlapping Apple Health workout gets folded in for heart rate and calories, not counted again.
  • Match on duration as well as start time. Copies made by syncing are sometimes stamped with the sync time instead of when I worked out, so start time alone misses them.
  • Drop the echoes. Tonal's zero-weight imports of Peloton workouts are excluded.
  • Keep what only Apple saw. Walks, pickleball and outdoor rides only exist in Apple Health, and they count.

What I'd tell someone trying this

  • If a service has an API, put an MCP server in front of it and explore first. Asking questions about your own data in plain English is the fastest way to learn what's there.
  • Apple Health means an app on the phone. Shortcuts is fine for a daily snapshot. For anything closer to live, you need HealthKit background delivery, and that means an app, paid or your own.
  • Design for the lock rule from day one. Nothing reads Health while the phone is locked, so don't build anything that assumes otherwise.
  • Plan for duplicates as soon as you have a second source. Fitness platforms copy each other's workouts more than you'd expect.