Request your first API in Shortcuts on Mac
To request your first API, you create the URL that points at the API “endpoint” that you want to talk to, and then pass that URL to the Get Contents of URL action. When the shortcut is run, this action makes the API request.
Build the API request
Typicode’s endpoint requires a URL request to be formatted this way:
The text in brackets is the resource you would like to access. Typicode provides many different examples for testing requests. This example will be using
https://jsonplaceholder.typicode.com/users to request a list of users.
In the Shortcuts app on your Mac, add the request to the URL.
usersrequest will return a list of users.
You can narrow your search by specifying a specific user by the id:
https://jsonplaceholder.typicode.com/users/6. This will return the user whose id is 6.
Most APIs allow for queries using the following format:
https://jsonplaceholder.typicode.com/users?username=Bret. The “?” starts the query, which searches for items using key/value pairs. In this example, the query
?username=Bretsearches for a user whose username is Bret.
Check your API’s documentation for supported searches and queries.
Perform the API request with the Get Contents of URL action
In the Shortcuts app on your Mac, click Show More in the Get Contents of URL action.
The following options for the types of API requests you can make become available:
GETallows you to retrieve data.
POSTallows you to create new data.
PUTallows you to update data by replacing it.
PATCHallows you to update data by modifying it.
DELETEallows you to remove an object specified in the URL request.
For this example, use a
GETrequest to retrieve data from the Typicode API.
Now that the URL action has your request and the Get Contents of URL action is set up to
GETdata, you’re ready to make the API request.
Note: When the Get Contents of URL action is switched to
PATCH, a new parameter called Request Body is added. Request Body allows you to send JSON, a Form, or a File to the API as part of your request. This lets you enter new data manually or by using variables so you can send the data to the API for creating, replacing, or modifying an entry.
To test the shortcut, click .
To learn about working with JSON and to continue with the Typicode API example, see Intro to using JSON in Shortcuts on Mac.