By Lee Martin, on Feb 5, 2019

Developing an Apple Music search into Contentful using UI extensions

I’m currently helping a client setup a year long marketing campaign and we decided to use Contentful as our infrastructure to manage data and content. For the uninitiated, Contentful is a content management system with a very intuitive online interface and library of APIs. You setup your data models like any database and then use the content created to craft any number of apps and services. I honestly didn’t even know headless CMS existed a few months ago and now find myself adding it to almost every proposal. Giving my clients easy access to their own data feels more professional and saves me time.

On this particular project, we have a Content Model of Song which contains the fields of Name (song name) and a list of associated Artists. In addition to displaying our songs, we realized it would be helpful to our users of our web app if we linked to the song on Apple Music and Spotify. A quick idea would be to simply add another text field for each so that we could manually provide the relevant url. This would require my client to open each streaming app, search for the song, copy the link, and return to Contentful to input it. I thought it would be wise to try and build the search into the Contentful UI to save my client a few steps and expand the data we would be storing on each track.

So you could imagine my surprise when I started reading about Contentful’s UI extensions. Amazingly, Contentful provides an SDK for rolling your own customized interface components and it works all through an iframe. My vision for our component was simple. The client should be able to type in the name of a song and receive a list of results back from the Apple Music API. If the client finds what they are looking for, they simply need to click the result and it will update the field accordingly. The field itself would be JSON so we can store a nice object of song data rather than just the url.

Here’s a video of what we’ll be building. Ready to build? The first thing we’ll want to do is build an Apple Music search which I’ll be using Vue.js and CodePen.IO to construct.

Apple Music search

In order to develop with the Apple Music API, you’ll first need a valid developer token. I’ve written a bit about that process here. Once you have a token, you can then use the MusicKit JS framework to begin calling the Apple Music API. Here’s the simplest Vue app ever that does exactly this. First create a single element in your HTML with the id of extension. Then, include both the Vue script and MusicKit JS script. Then type up your simple Vue app.

Let’s break this down. As soon as your Vue app is mounted, the MusicKit framework configures itself with your developer token. The view itself is simply a single input which accepts a song query. When you press enter on your keyboard, the search method is called. This method then uses the MusicKit instance you configured earlier to query the Apple Music API for the first 5 songs which match the criteria. At the moment these results are simply logged in the console but you can easily evolve this to list them out.

The next step is to add your search as an extension and begin to wire it together with Contentful.

Adding an extension

You can add an extension to your Contentful space by going to Settings > Extensions and clicking “Add extension.” First, give your extension a good name. We’ll call ours “Apple Music Search.” Then, choose the appropriate field type for your use case. We’ll select Object since we plan on using this to build a JSON object. Finally, point to where your extension is hosted or simply drop your code into Contentful’s hosted editor if it will be less than 200kb. Since I built our extension on Codepen, I added a link to the Codepen debug url. Click “Save” and you’ll have successfully added a Extension.

You must now add the field to your model that will utilize this Extension. For us, that involves adding a new JSON object field called Apple to our Song model. While configuring this new field, select the tab “Appearance” and choose your newly created Extension as the display choice. Once everything is saved, you should see your new search input show up when creating a new Song. Nice work. Type something in, click enter, and check your console for search results. It’s now time to bring in Contentful’s UI Extension SDK.

Integrating UI extension SDK

Let’s add the sdk library script into your application alongside Vue and MusicKit. You may also want to include the Contentful stylesheet if you’re trying to mimic their aesthetic. We’ll then begin evolving our Vue app. First, let’s initialize the SDK within our mounted event alongside the MusicKit configuration and associate it with our data object for easy access.

Next we’ll create an additional Vue component just for the song results. This will allow us to reuse the layout for both a search result and search selection.

Each song component will display the artwork, artist, song title, and artist name. It can be used as a search result which when clicked would update the currently chosen song. It can also be used as a selected result and includes a Contentful provided delete icon which would remove the song when clicked. Both the set and remove functions will be setup on the parent app.

Next, let’s modify our main app component’s template to use this new song component. First, add a blank array of songs to the data(). Then evolve your Apple Music search to map the result data into the final format we’ll store on Contentful and visualize on our song component. In addition to artwork, artist, name, and url, we’ll store the ISRC and preview url because who know how our app’s might evolve.

Let’s also declare two more methods that we’ll actually update the value of our field on Contentful. One for setting and another for removing the value.

We’re close now. Let’s update our app’s template code to make sure the extension SDK is loaded before showing our app. We’ll also check for an existing value. If we find one, we will simply show a set song component with the delete button. If we do not find a value, we will show the search field and also a list of results if they are present.

Now do some testing and see how your custom extension has come together. If all goes well, you should see something like this:

Thanks for reading. I hope this technology and the possibilities of it excites you as much as me. Props to Contentful for building a developer friendly platform that inspires this level of customization. As always, leave a comment if you have a question or feedback.

Lee Martin

Developing and designing campaigns and products for 15 years in music. Previously Silva Artist Management, SoundCloud, and Songkick. Currently splitting time between client, contract, consulting, and personal projects.

Community Contributor
Become a contributor