How to use Shortcuts to enter data into an API

richard moult
4 min readJan 20, 2022

--

In a previous post we explored how to use Apple Shortcuts to parse data from an API that did not require any user input. In this post we’ll see how to change that and pass information into the API and display the output in an Alert.

For this example we are going to use and API that predicts the age of a person by their name.

The URL is https://api.agify.io/?name=bob, where bob is the value we would like to pass in when we start the Shortcut. So if we wanted to predict the age of a person called fred we want to switch the url to https://api.agify.io/?name=fred.

The returned JSON can be seen below, where we will want to extract the age and name to display to the user. Importantly name and age are the keys we want to extract from the data and display to the user.

{
"name": "meelad",
"age": 29,
}

Let’s get to it.

  • Open the Shortcut app and select “+” to create a new Shortcut
  • Give the Shortcut any name you like, e.g “Age from name?”
  • Add action “Ask for input” and switch “prompt” for something like “Enter the name of the person you would like to predict the age for”.
  • Add action “URL”, change the url text to “https://api.agify.io/?name=”

Now we need to figure out how to get the input from “Ask for input” into our URL. Usually the Shortcut App does a great job at auto filling in this information for us, but this one is a bit tricky for the system to auto fill, so we are doing to need to inject the input to the url ourselves.

So, at the end of the URL text, right click and select “Select Variable”

You will then see the Shortcut interface change to…

Each variable in your Shortcut will now be exposed and highlighted blue, in our case we have 2 variables, “Shortcut Input” and “Provided Input”. Select the latter and continue with the steps.

  • Add action “Get contents of URL”
  • Add action “Get dictionary from input”
  • Add action “Show Alert”

What we ideally want to show the user is, “The predicted age for Fred is 23”. To show the name and the age in the Alert from the URL response you will need to type the key word “dictionary” and a pop up should appear with the object Dictionary, select that. Then type “is”, followed by “dictionary”, once again select the object that is presented to you. So you should now have “The predicted age for Dictionary is Dictionary” where Dictionary is not text but the highlight object you selected.

Now all we need to do is change Dictionary to the values provided by the URL. Do double tap Dictionary to get this popup.

For one Dictionary set “Get Value for Key” to “age” and for the other set the “name” (these are the key words from the JSON).

Finally your Shortcut should look something like…

On running the Shortcut you’ll see the input action, enter any name you like, e.g Kevin.

The Shortcut will then construct the URL, call the API, parse the data and display…

Hope that helps.

If you enjoyed this blog post, you’ll love the book packed with plenty of real-world examples and AI integration — dive deep into Shortcuts.

If that was helpful you can find more information and examples here.

--

--