From Beats to Data: An Easy Guide to Spotify’s API

Angel Merino
MCD-UNISON
Published in
4 min readJan 15, 2024
Generado con IA ∙ 2 de noviembre de 2023, 6:45 p. m.

Table of Contents

  • Introduction
  • Get Started
  • Jupyter Notebook
  • Results
  • Conclusion
  • References

Introduction

Spotify is a digital music, podcast, and video service that gives you access to millions of songs, and other content from creators all over the world. Spotify’s platform revolutionized music listening when it was launched in 2008, and since then, more listeners have discovered, managed, and enjoyed over 100 million tracks, 5 million podcasts titles, and 350,000 audiobooks on Spotify. Without a doubt, we can say that Spotify is the most popular audio streaming subscription service with more than 574 million users, including 226 million subscribers in over 180 markets (Spotify, 2023).

So, besides all the cool things we’ve got from this app, there are a few things that we can retrieve, which you might not know about until now. These are things that will allow us to get to know ourselves a little bit better. If you’re a music lover, like me, you might have wondered, “Who is indeed my favorite artist?”, or maybe“What’s my favorite song?” but don’t worry because that’s in the past.

In this post, you will learn how to retrieve data from you Spotify Account using the Spotify API. In this case, I’ll show you how to get data of your current top artists, current top tracks, the recently liked, or saved, tracks and more…

Get Started

First things first. This guide assumes you have a Spotify account (free or premium). Now, if you want to interact with this API, you have to follow these instructions.

Log in into the dashboard using your Spotify account. It will look like this:

spotify-for-developers. (2023). Spotify.com. https://developer.spotify.com/dashboard

‌Then, hit on Create app button:

spotify-for-developers. (2023). Spotify.com. https://developer.spotify.com/dashboard

In this part

  • You have to name your App
  • Add a description
  • The Website is not required so don’t be afraid of not filling it
  • For this project, we’ll use the Redirect URI as http://localhost:8888/callback
  • You can ignore the boxes of Which API/SDKs are you planning to use
  • Finally, you must agree with terms of service

Once you’ve done this, you’re going to see something like this:

spotify-for-developers. (2023). Spotify.com. https://developer.spotify.com/dashboard

Now, you have to hit the Settings button, this is to get the API’s credentials as you can see below:

spotify-for-developers. (2023). Spotify.com. https://developer.spotify.com/dashboard

‌Now that you’re here, hit on the View client secret:

spotify-for-developers. (2023). Spotify.com. https://developer.spotify.com/dashboard

‌In this part, you have to copy the Client ID and the Client secret, and paste it into a credentials.txt (with the same name) file.

Jupyter Notebook

Just before running the notebook, remember to install, or have installed, the following libraries:

pip install spotipy
pip install requests
pip install plotly

Below is my reusable function. Also, remember that the credentials.txt file has to be in the same directory of the notebook. After that, you’re ready to go.

Results

Since Medium can’t support interactive charts, here we’ll see the static charts we’ve got from our notebook. Also, since this is an easy guide, the graphs are going to be treemaps to make the representation the easiest possible. Remember, there’s a lot of ways to visualize data.

The last 20 liked songs
The most listened artist depending on the time range
Top genres from my top artists
My most listened songs depending on time range

Conclusion

This is an interesting API that you can work with. There’s still so many things that you can use from it, and there’s a lot of information that you can retrieve from your profile that can help you to create a lot of new things to use them in your life. For example, extract information from a pre-existing playlist and see if it’s going to be a banger for your party based on the information that each song has.

So, even though this API has a lot of potential, there’s a few things that might be a little bit tricky, and most of them are not easy to catch at first either; but don’t worry, you’ll learn these things on the journey. Finally, I hope this helps you to get to know it better, and please check the references I used if you get stuck while using the API.

References

‌‌‌

--

--