One of my first posts here was about YouTube API and it is still one of the most viewed ones, so I have decided to write about another API related to music – Spotify API. I guess everyone has ever heard about this platform. It offers to listen to a huge amount of music, podcasts or even watch music videos. But apart from those things, there exist tools that can be used by developers to integrate Spotify with other services/websites/apps. In this blog post, I would like to focus on Spotify API, which allows getting information directly from the Spotify Data Catalogue.
As I mentioned, Spotify API endpoints return JSON metadata about music artists, albums and songs, directly from Spotify’s data catalogue. In addition, it provides access to user-related data, like playlists and music that the user saves in the Your Music library. It is possible thanks to selective authorization.
Spotify API configuration
Firstly, you should log into Spotify for Developers or create an account there if you do not already have one. Then create a new app by completing the form below:
After that, you should see something like that:
Finally, we can open Visual Studio or another IDE. First of all, create a console application project and download SpotifyAPI.Web from NuGet Packages:
We will start the implementation with the code that will be responsible for the communication of the application with Spotify. We use CredentialsAuthenticator, which will guarantee that the Spotify instance always has an up-to-date access token by automatically refreshing the token on demand. As shown below, this constructor contains two arguments: clientId and clientSecret. You can take those elements from Spotify’s dashboard.
Now, we should create an instance of SpotifyClient that will be needed for getting some data from Spotify. In our example, we just take details about one track by sending the request with trackId that was taken from the URL.
And it works, you will notice the name of my favourite composer – Ludovico Einaudi. 🙂
Spotify API has plenty of capabilities. You have a possibility to try it here. The Spotify Web API Console makes it easy to explore endpoints through an easy-to-use interface. Have fun there! 🙂
In this short blog post, I wanted to show you how to start playing with the Spotify API and introduce you to the possibilities it offers. It is really smooth to use and you can easily add a few features to your system that will leverage it. Have a look at my other .NET related articles. 😉