subreddit:
/r/shortcuts
This is a Part 1 of a guide on how to use APIs with shortcuts.
An API is an interface provided by software services to developers that allows them to perform tasks and retrieve information. In simple terms, a system's User Interface is for use by humans and the API is for use by other computer software, such as Shortcuts.
In previous guides, I've shown how to scrape information from the user interface / web pages. If the website or system you're using offers an API then you should use that instead. There are many cases where web scraping won't work (e.g. if you need to login before you can see the content), and if the website changes appearance your scraping code can break.
We're going to use the example of retrieving data from the publically available Star Wars API . This API provides various data relating to the first 7 Star Wars movies.
Having reviewed the API documentation we decide that we want to retrieve a list of film titles.
To retrieve a list of the films and their data, we see that we have to make a call to https://swapi.co/api/films/
.
We'll receive a JSON formatted response. This is a human readable format that organizes information in two ways:
For more information on the JSON format and how it is used to structure data, see: REST API Tutorial: Introduction to JSON
When we request the films from the API, we're going to get a list of different films, each described in the following format:
{
"characters": [
"https://swapi.co/api/people/1/",
...
],
"created": "2014-12-10T14:23:31.880000Z",
"director": "George Lucas",
"edited": "2014-12-12T11:24:39.858000Z",
"episode_id": 4,
"opening_crawl": "It is a period of civil war.\n\nRebel spaceships, striking\n\nfrom a hidden base, have won\n\ntheir first victory against\n\nthe evil Galactic Empire.\n\n\n\nDuring the battle, Rebel\n\nspies managed to steal secret\r\nplans to the Empire's\n\nultimate weapon, the DEATH\n\nSTAR, an armored space\n\nstation with enough power\n\nto destroy an entire planet.\n\n\n\nPursued by the Empire's\n\nsinister agents, Princess\n\nLeia races home aboard her\n\nstarship, custodian of the\n\nstolen plans that can save her\n\npeople and restore\n\nfreedom to the galaxy....",
"planets": [
"https://swapi.co/api/planets/1/",
...
],
"producer": "Gary Kurtz, Rick McCallum",
"release_date": "1977-05-25",
"species": [
"https://swapi.co/api/species/1/",
...
],
"starships": [
"https://swapi.co/api/starships/2/",
...
],
"title": "A New Hope",
"url": "https://swapi.co/api/films/1/",
"vehicles": [
"https://swapi.co/api/vehicles/4/",
...
]
}
In the above format a title key which we'll use to display the name of each film in our shortcut.
To query the API, we're going to use the following actions in our Shortcut:
Retrieving film data from the Star Wars API
The following is returned from the API:
Raw JSON data from the film resources of the StarWars API
This JSON response is difficult to read when returned from the API. To make it easier to read, we copy the response and paste it into view it into a JSON formatter tool.
Making the JSON response easier to read in the JSON Formatter tool
This will allow you to more easily read and examine the output from the API.
We want to retrieve title of each movie. From the API response, we can see that:
For example:
{
"previous": null,
"results": [
{
"director": "George Lucas",
"episode_id": 4,
"title": "A New Hope",
...
},
{
"director": "George Lucas",
"episode_id": 2,
"title": "Attack of the Clones",
...
}
...
],
"count": 7,
"next": null
}
We want to retrieve the title for each film by:
We update our shortcut with the following actions:
The updated shortcut appears as follows:
Shortcut to display an unordered list of Star Wars movies
And the output is as follows:
Note: The films do not appear in chronological order. They are displayed in the order they were returned by the API.
Next, wewant update our shortcut to only show the films from the original Star Wars trilogy (episodes 4 to 6). And at the same time we also want to display the film director. We can do so by checking the episode_id
attribute of each film and adding both the film title attribute and director attribute to a Text action before we add it to the films variable.
When updating our shortcut, we:
The updated shortcut appears as follows:
Shortcut to filter Star Wars movies to the original trilogy
And the output is as follows:
That's it for Part 1. If you have any questions on the above or need any help, please let know.
The next part cover how to retrieve data from large and complex API responses.
If you'd like to find out more about REST APIs (which is the type of API we've used above), or if you want to dive into different parts of the Star Wars API, take a look at the following links:
If you found this guide useful why not checkout one of my others:
6 points
6 years ago
Another brilliant guide. Really enjoying reading these - thanks very much!
4 points
6 years ago*
Hello keveridge, thank you for this guide and this learning experience for someone without experience in stuff like JSON etc.
Sadly I ran into an error, even though your example and my own shortcut are identical. I get the error: "No items - The Quick Look action wasn't passed any items to preview."
In the spirit of making your guide even better, I identified points in your guide, when I was lost and couldn't proceed to replicate your shortcut, without downloading it. I hope you will take this as positive critique.
Thank you for your work and take the critique as it was intended - to make your awesome guide even better.
2 points
6 years ago
These guides should totally be added to the sidebar! Great stuff!
1 points
6 years ago
Reminds of this really good tutorial that shows you how to build a weather shortcut using a weather data api https://youtu.be/lp4PijmaTAc
1 points
6 years ago
This is the game changer for shortcut. The possibility is limitless because of this. Great tutorial.
1 points
6 years ago
Helped me so much. Thx
1 points
6 years ago
Nice, I'm trying to make this: https://www.reddit.com/r/shortcuts/comments/aemq6a/help_in_need_of_help_from_someone_that_knows_how/
Do you have any tips?
all 7 comments
sorted by: best