Spotify API

Author

Brody Spears

Introduction

Spotify is a music service that allows over 500 million users to listen to music or podcasts whenever and wherever they would like. I will be using the Spotify API, which allows users to pull information on artists, songs, albums, and even your own user data. This can be very useful information if you are interested in a particular genre, artist, album, or even want to learn a thing or two about your own user preferences!

Installing and loading packages

First you will need to install and load the Spotify r package using the code below. The other packages are also useful in pulling information from the data later.

install.packages('spotifyr')
library('spotifyr')
library('knitr')
library('tidyverse')

Account Setup

Next you will need to sign into your Spotify account and create a developer account by going to the dashboard here. From there, create an app and fill in the information for “App name”, “App description”, and “Redirect URI”. The name and description can be anything you would like. For the “Redirect URI” you can use http://localhost:1410/. Select “Web API” to access the Web API (or select other APIs if you are interested in those).

From here go to settings and find your Client ID and Client Secret. You will need this information for the following authentication steps.

Authentication Process

Now that you have a developer account and a Client ID and Client Secret, you can obtain your access token which is required to access Spotify’s API. You can do this using the code below.

id = Sys.setenv(SPOTIFY_CLIENT_ID = 'xxxxxxxxxxxxxxxxxxxxx')
secret = Sys.setenv(SPOTIFY_CLIENT_SECRET = 'xxxxxxxxxxxxxxxxxxxxx')

access_token <- get_spotify_access_token()

After this you are now able to access Spotify’s API and pull information on artists, music genres, or even your own Spotify account. Below I have an example of what this could look like.

API Call Examples

Here I will be using data from Spotify’s API on one of my favorite artists Kendrick Lamar. Here is the code I used to find out which notes he uses most in his songs.

kendrick_lamar <- get_artist_audio_features('kendrick lamar')

kendrick_lamar %>% 
    count(key_mode, sort = TRUE) %>% 
    head(5) %>% 
    kable()
key_mode n
C# major 31
C# minor 10
D major 10
G major 10
B minor 9

Valence is a measure from 0.0 to 1.0 that Spotify uses to categorize a song’s positiveness, with 0 being a sad, angry, or depressed song and 1 being a happy, cheerful, or joyous song. I ran the code below to find the average valence for Kendrick Lamar’s songs.

kendrick_lamar %>% 
  summarize(mean(valence))
  mean(valence)
1     0.4536831

This value is a little less than 0.50, which means that out of the songs that Kendrick Lamar has made they tend to on average cater more towards a more sad or angry tone (which makes sense if you have heard any of his music).

Conclusion

This is just a short taste of what can be done using Spotify’s API, but hopefully using this tutorial can help make the process to access it easier and give some examples of how it can be used to find information that you are interested in. Good luck out there!