Every now and then I’ll receive a “Song of the Day” (SOTD) email from a colleague, sent to a handpicked group of music connoisseurs, usually consisting of a Spotify link and a bit of a “Friday Feels”, or similar. For a while I’ve been thinking of ways to capture these emails and automate the process of adding these songs to a shared Spotify playlist but never really got around to it.

I had played around with the Spotify API in the past and I’d just started looking into Microsoft Flow for business use, so I decided to see if I could fit these two things together and see what I could do.

Getting the Flow started

The SOTD email always includes “SOTD” in the subject line, so it was quite easy to set a Flow trigger up.

Microsoft Flow Trigger

I only want to process emails that contain a Spotify Track URL, so we can ignore everything else (YouTube, Apple Music etc) for now, at least, by using a condition. An example of the URL we’re looking for is https://open.spotify.com/track/6nIWjBwBH0A84tXh8gcxQ1?si=VmYXOKSeQcCv, so we will look for https://open.spotify.com/track/ in the email body.

Microsoft Flow Condition

Spotify Authorization

To start this project off, I needed to log onto the Spotify Developer site (with my normal Spotify account) and create an app so I can a Client ID and Client Secret. Spotify have a great Quick Start Guide and Authorization Guide which helped get this started.

I followed the authorization guide until I got my Refresh Token then I created a SharePoint Online List to store my Client ID, Secret, Refresh Token and the Spotify Playlist ID that I’d like tracks added to, so our next part of the Flow is to grab all this auth info from SharePoint.

Get Authorization Information

The Spotify API Access Token only lasts 60 minutes, so we need to use the Refresh Token we got earlier to get a new Access Token before continuing. We can use the HTTP Action in Flow.

Refresh Access Token

Now, using the Parse JSON action to parse the body of this response, we can use the data within the Flow.

Getting the Track Details

Now we have a fresh Access Token, it’s time to get the track details. Using string functions we can extract the track ID from a URL like https://open.spotify.com/track/6WLlCkMRe00XPLGppmqegf?si=VvimOrxqQ7ydMD5Ov6zq3Q then we can query the Spotify API to get data on the given track.

String Function

Get Track Details

Again, we must use the Parse JSON action, and now we have access to the Spotify URI for the track. This is the ID we can use to add the track to our playlist!

Adding the Track to the Playlist

Here we make a call to the Spotify API with our Playlist ID (stored in SharePoint) along with the URI for the track that we just obtained.

Add Track to Playlist

So, this sums up how I used Flow to automate new additions to a Song of the Day playlist we have going at work. Everytime a SOTD email comes through containing a Spotify URL, it is automatically added to the playlist.

Finally…

Some things to consider - duplicates and spam. Regarding spam, it would be smart to only trigger this flow for a defined group of senders. Duplicate handling is currently quite hard with the Spotify API and I haven’t found a nice way to set this up.

Another thing that was cool - I set this up retrospectively by looping it through previous emails. This instantly populated the playlist with a few years’ worth of songs.