Spotify Api for beginner

Spotify API for beginner using JavaScript

Since you are here, I assume you have basic understanding of API(Application Programming Interface). Okay cool, let me delve directly into this topic.

Meta Data means data about data. And Spotify gives you the data like artist name, producer, writer, song title, release date of audio file which can be useful. Often you can use such data to make music platform , mp3 converter and…. sky is limit.

To use Spotify API, there is very good documentation that a beginner may not find easy to understand so i will not walk you through but will show you how it is done with explanation.

I will be using JavaScript as an example to call an Web API and get the data from it.

Getting Started

step 1: Access Token

Note that i will use Axios (for no reason) to call an API, you can use fetch or whatever you want.

Access Token

Again note that it is POST request. In above program, this.client_id and this.client_pass means the Client ID and Client Secret that you got after login(that you just saved from cat). So in your case, you have to just paste those string in place of this.client_id and this.client_pass.

Our intention with the above program is to send (POST request) our unique client_id and client_pass to Spotify so that it sends back the unique authorization token (access token), which will be required for communicating with the Spotify API. This access token will be useful to validate us as a client for every request.

headers:{“content-type” : “application/x-www-form-urlencoded” }

headers means the extra information about the request. “content-type” is specifying the type of content that is being sent to request body and “application/x-www-form-urlencoded” indicates that the content of requested body is formatted as a series of key-value pairs encoded in specific way.

This program will return access token that we are talking about Example :(BQDQooopppppCaoZWRskdhjpb9Bz7zooppppooMhjhjskdhjhjkhjkfhjfhXxoiiiooppppo), which we will use later.

step 2: API call

As you can see here,

await axios.get(`https://api.spotify.com/v1/search?q=${trackname}&type=${type}&offset=0&limit=1`);

Here, we are using search as “keyword”. and trackname is the name of your song (never gonna give you up). and type can be: “album", "artist", "playlist", "track", "show", "episode", "audiobook"

The offset means the position to get element and limit means how many element. So &offset=0&limit=1 will give you only one search result. You can remove that &offset=0&limit=1 so that you get all the search result and select based on your choice.

If you want to not use offset and limit and put values in those variable, it will look something like this, await axios.get(`https://api.spotify.com/v1/search?q=”never gonna give you up”&type=track`);

In headers, ACCESS_TOKEN, is the token that we obtained before. and should be a space after “Bearer” as shown in picture.

Now this response will give you the information about the songs in JSON format, and can be obtained by response.data

step 3: Track ( optional )

You can be all set from above two step, but if you want to get track only,

replace URL on step 2 with this URL: https://api.spotify.com/v1/tracks/id

this id can be obtained from the search API that we just did in step 2. You can find it in response.data; it will give the data in json format as specified in documentation.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

After getting the meta data from the API you can use it for your own builds like making your own Spotify (using music metadata from Spotify and actual music data from somewhere else…like Youtube) but make sure you don't violate any condition of Spotify😉. I strongly recommend you to read those documentation after this, you will understand it much better.
Feel free and keep exploring.

Go Back

Read this on medium