Workflow Meets Airtable API

Firstly URL schemes with x-callback-url, then the workflow, these automation tools have expanded the scope of iOS productivity to an unprecedented level, pulling people out of the repetitiveness of their work, and allowing them to do tasks in a completely customisable and, more importantly, fun way because imagination always matters in automating the tasks.

I get a kick out of working out a workflow that could make my life a little easier each time. Whenever there is a task that would require me to repeat certain actions over and over, Firstly I would think if I could make a workflow to take care of the tedious, repetitive part of the task. And I’m always amazed by workflow’s capabilities ranging from data formatting, scripting to third party app integrations, and how much more I can explore.

Months ago, workflow made itself even more powerful by embracing web APIs. With this capability, you can use myriad services out there, even without a local app installed on your device, as the workflow talks to the web directly. The neater part is that once you’ve configured your workflow successfully, you can forget about all the services behind it, and you don’t even need to unlock your iPhone or iPad to run the workflow for some tasks, which wouldn’t be possible in the “traditional” way. This is revolutionary.

Inspired by the aforementioned article, I’ve been playing around with this new feature and I find the process not hard as I imagined, and again, full of joy. For example, I have a workflow that interacts with the the New York Times API. It will fetch articles from the vast database of the New York Times ( dating back to 1851 ) based on the query I type and send them to my Instapaper. It allows me to specify the keyword, the begin and end date, etc. (I retrieve the articles from the past week). As a result, I get my personalised magazine that “issues” weekly.

In this article, I want to talk about another workflow that I recently created in detail, demonstrating how workflow can be deeply embedded in our daily activities. Hopefully you can get the concrete concept of how flexible, powerful, and yet easy-to-use the tool is, if you still have no idea of what I have talked about so far.

The Problem Faced

One of my colleague and I have some coffee at Starbucks 2–3 times a week. While relishing the coffee, I want to have a quick way to check our “deposit”. When it is running out, we recharge it.

The request would simply be:

  • to check how much money left at any time.
  • to add a new cost entry each time we visit the coffee house.

A possible procedure on my iPhone could be something like:

  • Unlock my iPhone and locate the app (any suitable app) and open it.
  • Add a new entry.
  • Fill in the date.
  • Fill in the money.
  • Hopefully there would be a way to get the balance within the app, equivalent to a sum() function.

Although the process doesn’t sound complex and it will only happen 2–3 times per week, I don’t want to do it myself; I want the machine to do it because the actions are exactly repetitive — I only have to change date and cost.

So the whole process can be automated and this is exactly where workflow finds powerful.

When Workflow Meets Airtable API

I use Airtable to keep the records. I love the beautifully designed UI of Airtable on mobile devices and I primarily use the iPhone app to keep my reading records (a book list). An Airtable base is essentially a workbook in a spreadsheet and thus suitable for my application here. But the workflow I’m describing here will leverage Airtable’s web API which allows creating or reading data stored in a specific base through standard HTTP methods.

I’ve created a base that has two columns. The first column records the date and the second column records the cost.

Here is a short video that illustrates how the workflow works.

The workflow will first pop up a menu, asking for my choice, whether

  • a) I want to check the balance or
  • b) to push a new record to my table.

If I choose option a), what will be doing underneath is to retrieve the data of the second column of my table — the money column, one by one, then add them together; If I choose option b), a mini number keyboard will be popped up, with the default input being -38 — normally what we pay for the coffee.

Also you can see that the entire process is done right in the widget, without unlocking my iPhone. If you are an iPhone 7 or 7 plus user, with the “Raise to Wake” feature, things are just cooler.

The Making of the Workflow

The design of the workflow involves two separate tasks:

  1. Get the API information on Airtable’s developer website.
  2. The in-app actions on the iOS device.

Airtable API

Most web API services require some sort of authorization that uniquely links to one’s account. Some, however, don’t, for example, Google Books API, when you only retrieve public data, In this case, we will use the API key of Airtable. It is generated on the account page.

The authentication method of Airtable API, instructed on the AUTHENTICATION section is:

Authorization: Bearer api_key

Lastly, click the item that contains the name of the base (STARBUCKS in my case) and get the URL.

The Workflow Actions

With the API information at hand, we are ready to compose the workflow.

For clear thought and error reduction purposes, I suggest designing the “flow” first, then fill in details. Remember the workflow is a series of actions, with each action’s output being the next action’s input (always bear this concept in mind, e.g. don’t feed an action that takes care of a date with an, say, image).

The concept of this workflow is fairly easy. It consists of two main actions that are divided by the “Choose from Menu” action. Depending upon your choice, the workflow will “GET” or “POST” a piece of information through the API.

Preceding the menu block, there are two blocks setting the API key and then storing the key to a variable whose name is Key for later use.

Next we will convert the concept to real actions.

GET

The first option of our menu is to check the balance, which is to fetch all the data and sum them up. In the context of workflow, retrieving data through API is done by first calling the URL block to specify the address, then choosing the GET method in the following Get Contents of URL block. Authorization is needed in the GET block.

JOSN and Dictionary

The output of the Get Contents of URL action is a vast JSON structure (a popular text format adopted by many web APIs) that contains all the information of the base. Think of JSON as a list, with each of its element, called an object , being a dictionary. A dictionary, in analogous with the real world version, is a data pair, usually expressed as {key:value}. With the key, you “look up” the dictionary and it will return the value, if the key exists (Don’t confuse with it the API key, though😜).

The fun part is that, dictionaries can be nested, meaning the output of a Get Dictionary Value can be another dictionary, so to retrieve the desired data, you have to put a sequence of actions to reach the data iteratively.

The sum() function

The final remark in this section is how to get the sum.

The trick is to use a loop. First append the retrieved data to a variable iteratively (actually forming an array). After the looping ends, the Calculate Statistics block will sum the data up.

Finally, the result is reported through the Alert action.

POST

I’m gonna be short in this section.

To add an entry to the base, we need to push a JSON format text through the POST method. The JSON text has two fields, the date, in ISO 8601 format (per Airtable API doc), set as variable Date, and the amount of money, a number, set as variable Exp.

If the JSON structure is correctly configured, after the POST action, open your Base and you should be able to find the new entry.

Conclusion

I’m not a fan of programming, but with workflow, I can accomplish many tasks that would otherwise require substantial programming skills, for example, solving the hassle of syntax errors. I can easily drag if/else blocks and the like into my workflow and watch the actions “flow” from top to bottom. This is bliss.

If you capture the gist of this article, you can easily apply the idea to your cases, where, for instance, your can track your personal finance, or maintain a reading list, and more.

With a lively and enquiring mind, what workflow can do is boundless.

If you are new to workflow and would like to know about it, the workflow episodes on podcast Canvas is highly recommended.