How to Easily Use an API With Apple Shortcuts (Hello World Example)

How to Easily Use an API With Apple Shortcuts (Hello World Example)

This is a simple, practical guide for new Shortcut developers who want to take their first steps in communicating with web services. Some shortcuts often need to connect to the internet to get information—whether it's the weather forecast, sports scores, or a simple notification. This communication happens through APIs (Application Programming Interfaces), and one of the most common ways to interact with them is with a GET request.

In this exercise, we'll create a shortcut that sends a GET request to a server, receives a response, and extracts a simple "Hello, World" message. This article will help you understand how to use this feature in your shortcuts.

Step 1: Set Up the Shortcut

  1. Open the Shortcuts app and create a new shortcut.
  2. Name the shortcut "Get Hello World".

Step 2: Define the API Endpoint

  1. Add the "URL" action to the shortcut.
  2. In the URL field, enter the following API endpoint address: https://api.rhub.pro/api/v1/hello-world.
    • Context: In API terms, an endpoint is simply the specific web address you send your request to. It's the gateway to the information the server has available.

Step 3: Make the GET Request

  1. Add the "Get Contents of URL" action.
  2. Make sure this action is connected to the URL you defined in the previous step, also, ensure the selected method is GET.
    • Context: This is the core of your shortcut. It performs the GET request, which is the most common method for asking a server for data. This action acts as a client that asks the API endpoint to send back the information it holds.

Step 4: Extract Data from the JSON

  1. Add the "Get Dictionary Value" action.
  2. Ensure this action is set to use the output from the "Get Contents of URL" action.
  3. In the "Key" field, enter "message".
    • Context: The response we get from the server comes in JSON format, which is a standard way to structure data in key-value pairs. For example, the server sends us something like {"message": "Hello World! Welcome to Rhub."}. The key is "message" and the value is "Hello World! Welcome to Rhub.". This action allows you to find a specific key and extract its value for you to use.

Step 5: Show the Result

  1. Add the "Show Result" action.
  2. This action will display the value you extracted in the previous step.
    • Context: This is the final step where you make the data visible. It takes the text you extracted from the JSON and displays it in a pop-up window on your device's screen.

Shortcut Summary

Your final shortcut should have the following structure, from top to bottom:

  • URL: https://api.rhub.pro/api/v1/hello-world
  • Get Contents of URL
  • Get Dictionary Value: (Key: message)
  • Show Result

By running this shortcut, you've completed a fundamental client-server communication cycle. With this knowledge, you can explore more complex APIs to build more powerful shortcuts.