Spotify API Tutorial

Author

Taggart Shetty

Spotify’s API

Spotify is a digital music streaming service that gives users access to millions of songs, podcasts, and videos from artists all over the world. As you can imagine they collect lots of data about the music and podcasts on their service and about the users that use it. Luckily lots of this data is available to the public through an API(Application Programming Interface). Some potential uses are:

Market Research- To understand the popularity of different genres or artists in various regions and demographics, aiding in market targeting and analysis

Audio Analysis-Explore audio features of tracks such as a songs danceability, energy and valence(positivity) for applications in fitness, therapy, or entertainment industries

Peer Review-Artists looking to dissect the musical composition of their peers work, such as key signatures, speech levels, and track lengths

Data Analytics- Anyone with a passion for data analytics or just wants to play with Spotify’s data

Setting up the API

Before we can get into using R code to do analysis we first have to get set up with Spotify to get an access key that will allow us to use the data.

  1. First go to the Spotify’s API website https://developer.spotify.com/documentation/web-api

  2. Log into your Spotify account by clicking “sign in” in the top right corner

  3. Now that your signed in click your username in the top right than double click on dashboard to go to dashboard

  4. Select the create app button and fill in the following

  5. App Name Example: Jakes Spotify App

  6. App Description Example: Jake’s Spotify App for BAIS 462

  7. Redirect URL this could be anything and does not have to be a specific working url I find it best to just use what spotify gives you by default Example: “http://localhost:3000”

  8. Where it says “Which API/SDKs are you planning to use?” select the box that says “Web API”

  9. Check the box that says “I understand and agree with Spotfy’s Developers Terms of Service and Design Guidelines”

  10. Click Save

  11. Next click on settings in the top right below your username

  12. Take note of your client ID as you will need this for the R code

  13. Click where it says “View client secret” below your Client ID and take note of your own client secret as you will need this for the R code

Setting up with R code

In order to use the Spotify API in R you need to load some packages and set up your access key

#Loading Packages
library(spotifyr)# needed to acess the spotify data
library(tidyverse)# All the tidy things
library(httpuv)#Commonly used when using spotifyr
library(knitr)#will be used in a later example 

#Setting up your acess key
#Note where it says "Your Client ID" and "Client Secret"
#You will input your own unique ID and Secret

Sys.setenv(SPOTIFY_CLIENT_ID = 'Your Client ID')
Sys.setenv(SPOTIFY_CLIENT_SECRET = 'Your Client Secret')
access_token <- get_spotify_access_token()

Example API Call

I will now be demonstrating an API call with the Spotify API. Say you are someone who wants to start creating music. Your favorite artist is Bruce Springsteen so naturally you want to make your songs sound similar to his. You could use the Spotify API to view which key and mode are most prevalent in his music in order to help you make your songs sound similar. This code will allow you to view the 5 most commonly used keys and mode in Bruce Springsteen songs on Spotify and count how often they occur.

bruce <- get_artist_audio_features('bruce springsteen')
bruce %>% 
  count(key_mode, sort = TRUE) %>% 
  head(5) %>% 
   kable()

Results

In case you were curious the results were the following

G major 130

F major 117

C major 100

A major 93

D major 78

Conclusion

While this is just one potential use case for this API the possibilities are endless and hopefully this tutorial puts you in a good place to start playing with the data.