How to Make API Calls from Apple Shortcuts

Introduction

In this short blog post i’ll show you how to create an Apple Shortcut that calls an API to generate AI Art using the Lilypad distributed compute network. By the end of this blog you’ll have created a shortcut that you can pass a prompt to and have the Lilypad Network generate an image for you using sdxl-turbo.

Motivation

AI art is blowing up on social media, with new trends popping up almost every week. Whether it’s Studio Ghibli-inspired creations or whatever aesthetic is trending right now, people are constantly sharing their favourite AI-generated styles. The point being is you should be able to quickly generate some art to join the trend on a whim and what better way then an automated shortcut using AI!

What is Apple Shortcuts?

If you’re not familiar, Apple Shortcuts is a powerful automation tool pre-packaged with iPhones, iPads and Macs to enable Siri powered workflows like turning on the smart lights in your home during a particular part of the day, having our phone go on do not disturb during work time or as i’ll show you here, making an API call to generate yourself some AI art.

Getting Started

So the first thing we’ll want to do is get ourselves set up with the Lilypad Inference API; however, you should theoretically be able to use any inference API here that allows you to generate an image but I like Lilypad so i’m going to use that.

So here’s what you’ll want to do:

  1. Head over to the Anura website and sign up (its free for the moment)

  2. Once you’ve signed up, create your Anura API Key which will let you programatically access the API

  3. You can also check out the docs if you want to learn more about the API, the endpoint we’ll be using (/api/v1/image/generate) and to see which models are supported

Now make note of your API key which we’ll be using in the next step

Creating the Shortcut

So what you’ll want to do is open up the shortcuts app on the device of your choosing, for this guide i’m going go through the Mac desktop version but you can also proceed on your iPhone or iPad as well.

Note: Through iCloud you’ll have access to the shortcut via all your Apple devices once you’ve built it

Once you open Shortcuts, click on the “+” button on the top right to open up the shortcut creation screen, you should end up with a window like this:

Now in the search bar on the right hand side, type in “text” and drag that over to the main pane. This will be the way we pass our AI prompt into our shortcut. Once you’ve added it, inside the text box area type “Ask Each Time” and press on the item that shows up as an auto-complete option. What this will do is make it so that every time you run this shortcut you’ll be able to provide a new prompt

Making the API call

Now the next step is to add the functionality to make an API call (the fun stuff), for that you’ll want to search for “Get Contents of URL” in the search bar on the right and drag that over to the main pane. Now click on the input area next to “Get contents of” so that we can configure the api endpoint we want to hit where it show “url”

In the URL text box paste the following:

https://anura-testnet.lilypad.tech/api/v1/image/generate

You should end up with something that looks like this now:

Now click on the “show more” dropdown so that we can start configuring our API call. What you’ll want to do is select “POST” from the “Method” dropdown:

Next, collapse the header section and add the two following keys using the “+” button at the bottom of the header section:

KeyValue
Content-Typeapplication/json
AuthorizationBearer <your-anura-api-key>

Now let’s round out by adding in the request parameters, under the Request Body section click the “+” button at the bottom of this section to add a new parameter. First you’ll want to add a parameter called prompt as a key and for the value what you’ll want to do for the value is start typing the word “text” and you should see the Shortcuts app suggest to you an autocomplete option that looks like the following:

What this does is that the shortcut will “pipe” in the text input we created in the first step of our automation as the input to our POST payload. Next we’ll want to add one more input parameter with the key model and value set to sdxl-turbo which will be the model we use to create our AI image. So now your “Get contents of” action should look like this:

Phew! That was a lot of work but that was the most trickiest step in the process, if you’ve stuck around this far let’s finish up our shortcut!

Grabbing the response from the API

So in your shortcut search bar, type in “Get images from input” which is the action we’ll take once we have a response from the Anura API from our “Get contents of” action. This action will take the raw bytes returned from the API and turn it into an image file. Drag that over to the main pane and it should auto connect to the previous action and look like this:

Now to move on to the final step!

Displaying the image

For the final step we’ll be adding an action that will take the image from the previous action and display it for us (as well as let us download the image to our device, share it, etc…). For this we’ll want to add a “Quick Look” action and have it connect to the “Get Images from” action from the previous step:

This is what your full Apple shortcut should look like now:

Now with that in place let’s test out our shortcut and generate some AI Art! Click on the “play” button on the top right hand corner of the window to test out the shortcut

Boom! Generative AI Art at your finger tips ready to cook you up some new images as the trends come your way 🙌

Go ahead and add this as a shortcut on your Desktop or mobile device so that you can activate it whenever you please!

Recap

To recap what we’ve built here, you just made a one-click Ai Art generator using Apple Shortcuts providing you a frictionless way to access the power of the Lilypad distributed compute network. As mentioned earlier in the article, with iCloud you can now also run this on all your Apple devices that are connected to your account! If you enjoyed this article consider giving it a thumbs up or subscribing to never miss any content I push out. Cheers until the next one!

10
Subscribe to my newsletter

Read articles from Narbeh Shahnazarian directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Narbeh Shahnazarian
Narbeh Shahnazarian