iOS Toggl Report through Shortcuts
How to use shortcuts to make API requests and automate Toggl Reporting
iOS Shortcuts has become one of my favorite programming languages in the last 4 years. It’s surprisingly powerful for how simple it’s design is. Sometimes that simplicity makes it very tedious to do things, but once you have it done, the results are very satisfying.
Today I wanted to share a shortcut I built to gather and transform my Toggl Track data into a nice report using the alerts action. Using shortcuts I was able to make API Requests to toggls report API, store my data, and output my data in a string format. If you use toggl track, you can check below for how to install.
I won’t be running through how the shortcut works step-by-step, but I will be highlighting some of the key actions that make it work. You should also read my medium article on all the shortcuts I've built for time tracking
How to install
- Get your toggl API Token (If you haven’t created one yet, you can go here to find out how to get it) and workspace ID (You can get your workspace ID by going to your reports page and copying the last number in the URL)
- You might need to enable use of outside shortcuts (You can check out Apple's documentation here)
- Download the shortcut by clicking this link while on your iPhone
- Enter your API Token when prompted in the shortcut.
- Viola! Now you can run the shortcut and get your report!
To set parameters for a shortcut, you can use the “Ask for input” action under scripting. Here you can specify a data type, this will make change the UI to be a number pad instead of a keyboard (or date picker).
Then you can set that parameter to a variable to be used in other actions.
O the dreaded date math. Shortcuts actually makes it really easy with their “Adjust Date” calendar function. You can specify what you want to add or subtract to another date. You can also specify what format you want to store your output in. Toggl reports API takes dates in ISO format.
API requests on shortcuts cold not be any simpler with the “Get Contents of URL”. You input your URL, you select what type of request (GET, POST, PUT, PATCH, DELETE) and set whatever parameters inputs your API requires.
You can also encode tokens for your more secure APIs.
While making a request is really easy, the tedious part becomes the working with the response. Depending on the API, you might have some work cut out for you to do what you want, but there are many actions that help you along the way.
Iterate Through Dictionaries
Shortcut makes it easy to iterate through dictionaries with their “Repeat” scripting actions. In this shortcut I run through each entry in my API response to store the data into staging variables before I attach it to a string list for my output.
There is not to many options for what you can place in system previews and alerts, so I generally format my outputs into strings (sort of like a command line program). With my staging variables I can format my string with the “Text” action, and then add it to my final output as a new line.
I really like using “Show Result” for my outputs since it presents a nice looking window I can quickly run through. Show results can also show images, and many other output types.