Spotify Playlist Generator

Spotify Playlist Generator

November 29, 2021 - Fun & Games

The other day a friend of mine requested recommendations for songs that were both sad but upbeat, a tall order. After wracking my brain and coming up with pretty much nothing I thought, "Wouldn't it be nice if you could just specify specifically what kind of songs you want and have Spotify generate a playlist?" Well if you know me you know I went ahead and built a little app that does just that! So, have you ever wanted to generate a playlist based on specific attributes like happiness, danciness, or acousticness? Or perhaps you'd be interested in seeing what your top Spotify tracks say about what you like to listen to? Well with this Spotify Playlist Generator you can do both of those things!

How to use

  1. Login with your Spotify account and allow the app to access your track data and make playlists for you. (I do not collect any of your Spotify information except what is necessary to generate the playlist 🙂)
  2. After logging in you will see the distribution of various attributes for your top 50 songs. (Feel free to change the time range at the top to see how your tastes have changed over time!)
  3. If you want to make a new playlist, adjust the sliders under each attribute to match the type of songs you want to listen to. Hint: Don't make your search too narrow or it will be hard to find any songs!
  4. Select a genre as a seed for your recommendations then give your playlist a name and click "Generate!" to make a new playlist.
  5. Your new playlist link will show up below the button (scroll down!) and you can open it up in Spotify and start listening!

How did I build this?

With everything I post on this blog, I want to share a bit of the detail that went into this project. The first thing you'll need to do if you want to make your own app is to create a new app in the Spotify Developer platform. From there you will be able to get your client id and secret to be used in your requests as well as set your redirect URLs. Once you're set up the process is as follows:

Authenticate the user with OAuth

Spotify offers a couple of ways to authenticate users but I went with the Authorization code flow. You will need to create a link using your client id, a redirect URL, and the specific scopes you require (for this project I'm using "user-read-private", "user-read-email", "user-top-read", "playlist-modify-public", and "playlist-modify-private"). If the user allows your app you will receive a code that you can then use with your secret to get a token for the user. You'll use this token in all subsequent requests for user data.

Get user's top tracks

Once you have a token you can make a request to get the user's top tracks. In order to learn about the characteristics of these tracks however you'll need to get the features for each track. Thankfully there is a single endpoint you can use to get the features of multiple tracks but you will need to pass in the track ID. Parse through the top track response to grab the IDs and then you can make the request for features and then tie the results back to the original list of top tracks.

Analyze track features

Now that you've got the top tracks and their features, you can analyze the distribution of some of the top attributes like valence, danceability, and acousticness. I've used ApexCharts.js to build out each mini histogram and Ion.RangeSlider to create a slider for the range below each one. With the slider, you can add a callback that will help keep track of what values have been selected.

Get the recommendations

Now that the user has selected a set of ranges for the attributes and features they are interested in it's time to generate the playlist. I've decided to use genre as the "seed" but Spotify allows you to use genre, song, or artist as the seed for getting a recommendation. I have a drop-down with all the genres Spotify offers and in combination with the min and max attribute ranges you can get a recommendation for songs that match.

Create and update the playlist

Finally, now that you have a list of songs you need to actually create the playlist and then add those songs to the playlist. Here again, you'll want to parse through the response from the recommendation and grab the IDs that you will then pass to your newly created playlist. Make sure you capture the returned ID for the playlist you created so you can use it to add the tracks. Also, you will get the URL for the playlist as a part of the response when you create it and you can use that in the output to the user once the songs have been added.

And that's it! I haven't gone into the nitty-gritty details for this one but hopefully, you are able to understand the process. Feel free to try out the Spotify Playlist Generator for yourself!


Sign up to never miss a post!

Leave a comment

Your email address is optional and will not be published.

A picture of Keshia Rose.

About Me

Hi, I'm Keshia, an ex-Tableau Product Manager currently evangelizing Fingerprint Pro to developers who has a love for building fun random apps and helping people solve problems. Keep coming back to my blog to learn how to use Tableau APIs and developer tools or to hear about other cool tech I'm learning about!

Sign up to never miss a post!

Topic Request

Want me to cover a specific topic? Let me know!