18

Create an Apple Health API With Shortcuts and Firebase

 4 years ago
source link: https://medium.com/better-programming/create-an-apple-health-api-with-shortcuts-and-firebase-a76d178319b7
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Responses

You have 2 free member-only stories left this month.

Create an Apple Health API With Shortcuts and Firebase

Learn how to create a personal Apple Health API for yourself

Image for post
Image for post
Photo by Miesha Moriniere from Pexels

I came by an inspiring post by Craig Carlyle. He created a dashboard to track his life. He can track his heart rate, calories, cups of coffee, open-source contributions, etc. day by day in a big calendar view. It’s a really cool project. I highly recommend you take a look at it.

I want to have a similar dashboard for myself, so I decided to build it step by step since it requires a lot of work to cover the different features. I also decided to share my journey with you so we can work on it together.

First, I want to track my daily steps, calories, etc. I have an iPhone and Mi Band 4 to provide these data. Apple Health collects everything, but there’s no way to access it outside of iOS devices. So we need a way to extract these data.

There’s an Export All Health Data option in the Health app. This option exports all your health data in an XML format. It’s a good option for one-time usage. But it’s not optimal if you want to sync your data every day or hour with your dashboard. We need a better option.

Apple released the Shortcuts app with iOS 12 in 2018. The Shortcuts app allows you to create macros and automate tasks on iOS devices. The sky’s the limit. You can really go crazy with it.

I came up with a solution using Shortcuts and Firebase. We can create a Firebase HTTP function to write health data into Firestore, and then we can create a shortcut to export and send health data to this Firebase function. This way we can sync the data with our dashboard whenever we want.

I’ll cover each step in this story to create this API. Here we go.

Data Structure to Export

The data is going to like this in Firestore.

Image for post
Image for post
Firestore preview

But first, we need a data structure to export the data. The shortcut will export the data in the following JSON format:

Apple Health data structure to export

Firebase

The free tier of Firebase is more than enough for our needs. First, we need to create a Firebase project and do some configuration in the Firebase Console.

If you’re already experienced with Firebase, you can directly get the code from my GitHub repository. You can find the link in the resources section.

Visit the Firebase Console, and click “Add Project.” Follow the instructions to complete. After the project creation, click the gear icon to open the “Project settings.” Once you’re inside the “Project settings,” select “Google Cloud Platform (GCP) resource location.” I selected “eur3 (europe-west)” since I’m located in Berlin, Germany.

Image for post
Image for post
Select the Firebase resource location

After selecting the resource location, click “Database” on the left console menu to open Firestore. Once you land in the Firestore landing page, you’ll see the following warning:

This project is set up to use Cloud Firestore in Datastore mode. This mode can only be accessed from Google Cloud Platform

Click the “Go to Cloud Platform” button. Once you’re on the “Cloud Platform” page, you’ll see a blue “SWITCH TO NATIVE MODE” button. Click that, and follow the instructions to complete.

Image for post
Image for post
Switch to Firestore Native mode

We’re done with the Firebase Console settings. Before diving into the code, we need to create our project boilerplate.

We’ll use the Firebase CLI to create our project boilerplate. I’ll use npx to skip any installation steps. Feel free to see installation options on the Firebase CLI docs.

First, we need to login to our Firebase account in the terminal to work with the CLI. Run the following command, and then follow the instructions to complete.

npx firebase-tools login

After logging in, execute the following commands to create the project boilerplate:

mkdir apple-health-api
cd apple-health-api
npx firebase-tools init

An interactive CLI will open after running the init command. Use the space button to choose “Firestore” and “Functions,” and then push Enter. Next, select our newly created Firebase project by choosing the “Use an existing project” option. After that, just follow the instructions to complete. As a side note, I’ve used TypeScript as the project language.

Since we’ll use Firestore to store our data, we need the firebase-admin package. Run the following commands to install:

cd functions
npm i firebase-admin

Congratulations. We’re all set up. Now we can dive into coding.

We’ll write a Firebase HTTP function to accept the data in the POST request body as a JSON format. Then, we’ll parse this data and store it inside Firestore.

Take the following code, and put it in functions/index.ts file. You can go through the code to see what’s happening.

Firebase function to parse and store data

Now, it’s time to deploy. Run the following command to deploy.

npx firebase-tools deploy

After the deployment, you’ll see the deployed endpoint of the function in the terminal. Alternatively, you can visit the “Functions” section in the Firebase Console to see it as well. Take it and store it somewhere handy. We’ll use it when looking at shortcuts.

Once you have the data, you can query and fetch, as below:

collection('apple-health').where('type', '==', 'Steps').get()
collection('apple-health').where('type', '==', 'Active Caloriies').get()
...

Shortcuts

I created a shortcut to export daily data from the Health app. First, download it to your phone. Then, we can tweak it for your needs.

The shortcut is configured to export your steps and active calories for the current day. You can configure these settings to add more data or change the time frame.

Image for post
Image for post
Add health types and change the time frame in a shortcut

The most important setting is the Firebase function URL. We need to provide it to send exported health data. When the shortcut runs, it’ll export the health data as JSON and then post it to the provided URL.

Image for post
Image for post
Set the Firebase function URL in the shortcut

Once you’re finished with the setup, run the shortcut to store the data in Firestore. Then go to the “Firestore” section in the Firebase Console to take a look at the exported data.

Conclusion

As a first step to creating a life-tracking dashboard, I’m pretty satisfied. The solution is pretty simple yet powerful. The Firestore SDK is great. You can run the shortcut on a daily basis with Siri, or you can try to automate it in the “Automation” section of the Shortcuts app.

If you’re looking for a project to work on in your free time, here it is. I developed this in hackathon mode. Take it and improve it to fit your project.

I want to finish this dashboard project and write about each step. If you’re interested, stay tuned.

Happy coding!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK