Building Better Playlists: Spotify Integration

Grant Holtes
CodeX
Published in
4 min readDec 26, 2021

For somebody who takes a lot of pride in their playlists, mine are pretty sub-par. Sure, I may like the songs, but the order of playback leaves a lot to be desired — Pitbull and The Veronicas certainly belong in the same playlist, but you might not want their top hits back-to-back! This is my over-engineered solution to this minor issue.

Motivation

Spotify is a fantastic service which has drastically changed how we find, listen and share music. Their tech forward approach has also allowed new features to be released that further elevate their user’s ability to interact with their music, such as the discover weekly playlist or the recently released blend tool that merges two people’s music tastes into a single playlist.

At the heart of the Spotify user experience is the playlist, which allows the user a way to catalogue and organise songs in a manner reminiscent of the cassette mixtapes of the 1980s. Without playlists the listening experience on Spotify would be a transient one, with users needing to search through the endless library of tracks to find “their” music. As such playlists provide the user with some level of ownership and control over what is “their” music in a system with no ownership and a universe of music and genres.

Given the importance of playlists and the mechanism by which new songs are added to a playlist (just slap them on the end), it is unsurprising that personal playlists can quickly become incoherent, a consequence of evolving tastes and the level of effort required to reorder and reorganise songs.

To avoid this fate for my own playlists, I have started to build solutions to this issue through using Spotify’s developer tools to create my own methods to interact with the Spotify music universe.

This is part one in a series, with links to the other parts at the bottom of this article.

The Spotify web API

Spotify offers a free Web API that allows you to programmatically access various aspects of Spotify and modify parts of a user’s account — given their permission at least!

I want to be able to access a user’s playlists and tracks within, as well as be able to modify these playlists or create new ones. This requires 4 key abilities:

  • Ability to list all of a users playlists
  • Ability to list the tracks within a playlist
  • Ability to get data about a track
  • Ability to create or modify a user’s playlist

Fortunately the web API offers all of these functions (and many more), as well as the ability to bundle many requests into a single HTTPS call, which reduces the number of requests required when loading track data on all tracks within a playlist.

For reference, the required playlist level interactions are contained within the Playlist API while the track level interactions are contained within the Tracks API.

The basic flow can be summarised as below:

Authorisation

For any of the API features to be useful, the user must be logged in as a valid Spotify user. Spotify provides a number of ways for a user to log in and provide the application with the required authorisation to access their data and make changes.

For my applications I have chosen to use the implicit grant method as I do not need long-term persistent authorisation and as implicit grant authorisation allows the application to be front-end only. This flow provides the application with a token that is then used in all subsequent API calls.

Pagination

Due to the large number of playlists and songs a user may have, all of the used API calls have a maximum return length as well as pagination to get the next chunk of data if any exists. This keeps the size of each api call manageable, especially when the data is contained within the URL parameters rather than headers, and so is subject to the strict character limits by browser.

The tool

For those that are interested the playlist optimiser tool is available at https://www.grantholtes.com/smartshuffle

Other stories

--

--