This is a simple API design for use with a PostgreSQL database and Auth0.
The problem: Having to open a mood tracking app and navigate through it to log something every day is tedious. How can I track mood daily, but with as few clicks as possible?
The hypothesis: iOS shortcuts can do many things, like prompting to choose from a list. Can it automate sending http requests?
The solution: Yes it can. It can also parse JSON!
The result: Mood tracking with a single click!
Back when Heroku was free, I hosted it with Heroku Postgres. I also integrated it with Apple Shortcuts so I can log a mood daily with just one tap.
Here are the steps for my setup.
npm install
- install dependencies
npm start
- start dev server on localhost:5001/api (either comment out the auth0 code in app.js or add your auth0 environment variables to the .env for local testing.)
docker-compose up
- start a postgres database in a docker container for testing.
If you don't have Heroku CLI: brew tap heroku/brew && brew install heroku
Create an account on Heroku.
Create the app:
heroku login
heroku create <appname>
heroku addons:create heroku-postgresql:hobby-dev -a <appname>
In a terminal in the root directory:
heroku git:remote -a <appname>
git push heroku main
- Go to your Heroku dashboard and click your app
- Click on 'Resources' then 'Heroku Postgres'
- Click on 'Settings' then 'View credentials...'
- Use these credentials to connect to your db via a client e.g. Postico
- Check that the schema was loaded, otherwise add it
I added auth config to this API for Auth0. A single user is covered by the free trial.
- Create an Auth0 account
- Create a new tenant
- In the dashboard sidebar, click 'Applications' then 'APIs'. Create an API, make up a name and identifier
- In the dashboard sidebar, click 'Applications' then 'Applications'. Create a machine-to-machine application and for authorization, select the auth0 API you just made.
- Go back to your Heroku app dashboard and click 'Settings', then 'Reveal Config Vars'. DATABASE_URL should already be there.
- Add convig vars:
- AUTH0_AUDIENCE: The identifier for the auth0 API that you made
- AUTH0_ISSUER_BASE_URL: https://(your-tenant-name).(your-region).auth0.com. You can find yours under 'Domain' in your machine-to-machine application as well.
- Make a new shortcut.
- Go to your machine-to-machine application in Auth0 and click 'Settings'. You will be using the credentials under 'Basic information'.
- Add action: Get Contents of URL. For
URL
, put the Domain URL from your machine-to-machine application but add/oath/token
. - Click the sideways arrow after the url. Change the method to
POST
. Add 4 new Text fields underRequest Body
(make sure the option to the right ofRequest Body
saysJSON
) with these key-values from your Auth0 machine-to-machine application and Auth0 api:- client_id: (Client ID)
- client_secret: (Client Secret)
- audience: (the identifier from the Auth0 API that you made)
- grant_type: client_credentials
- Add action: Get Dictionary Value. Populate it as: Get
Value
foraccess_token
inContents of URL
(linked to the action above it). - Add action: List. Populate the list with some presets for what you want to track.
- Add action: Choose from
List
(List being a variable linked to the action above it). - Add action: Get Contents of URL. For the URL, put
https://(your-heroku-app).herokuapp.com/api?mood=
Then clickChosen Item
from your autocorrect bar and it should append the chosen item from the List to the URL. - Click the little arrow after the URL and change method to
POST
. Add a new header, put 'authorization' for theKey
field and 'Bearer (Dictionary Value)', selecting theDictionary Value
variable from your autocorrect bar, for thetext
field.
Depending on how you want to use this, you could
- create an automation that prompts you with the list once per day and uses your choice as input to the shortcut (instead of the list being in the shortcut itself). The notification persists saying 'Tap to respond' so I can answer later if I'm not on my phone at the time it runs.
- add it to the homescreen so that you can press the button to choose from the list
Data analysis time~!