Skip to content

Getting Started

Kaustubh Patange edited this page Jun 6, 2020 · 4 revisions

In order to work with this API you must need some general understanding on how official Spotify-Web-Api works or at least you've used it one time.

Registering Application

Before making any request to the API you need three things

  • CLIENT ID - An unique ID which defines your application
  • CLIENT SECRET - A secret key which will be used to generate access & refresh token
  • REDIRECT URI - A website where the Auth flow will redirect with the generated auth code.

In order to obtain these three things, Spotify has an excellent guide.

Adding Dependency

In your top-level build.gradle file,

allprojects {
    repositories {
        ...
        mavenCentral()
    }
}

In your app/module build.gradle file add the following dependency.

Maven Central

dependencies {
    ...
    implementation 'io.github.kaustubhpatange:spotifyapi:$version'
}

Creating a client

With this client, we can make then make a call to official API (like TracksApi, AlbumsApi, etc).

Below is a Kotlin excerpt (can be converted into Java).

val client: SpotifyClient = SpotifyClient.Builder(this)
        .setClientId(CLIENT_ID)
        .setClientSecret(CLIENT_SECRET)
        .setRedirectUrl(REDIRECT_URI)
        .setScopes(Scopes.STREAMING)
        .setResultCallback(object : ResponseAction<AuthResponse> {
            override fun onComplete(t: AuthResponse) {
                Toast.makeText(this@MainActivity, "Token: ${t.accessToken}", Toast.LENGTH_SHORT).show()
            }
            override fun onError(e: Exception) {
                Toast.makeText(this@MainActivity, "Error: ${e.message}", Toast.LENGTH_SHORT).show()
            }
        })
        .build()
  • Builder - You must pass an activity as the constructor parameter.
  • SetScopes - An authorization scope that will permit certain access. A list of scope and their description can be found here.
  • SetResultCallback - This will be invoked whenever the Auth Flow completes (either success or error).

Making an API call

The library does provide some certain routes to make official calls using OkHttp client however, you can extend it to create other API.

The list of built-in API which the library provides. A complete list of other API Spotify provides can be found here.

  • TracksApi
  • AlbumsApi
  • EpisodeApi
  • ShowsApi
  • SearchApi

Below is a Kotlin excerpt demonstrating the usage of SearchApi.

client.methods.searchApi.searchItem("trap", arrayOf(Type.TRACK), 2, 5,
        object : ResponseAction<Search> {
            override fun onComplete(t: Search) {
                Toast.makeText(this@MainActivity, "Name: ${t.tracks?.items?.get(0)?.name}", Toast.LENGTH_SHORT).show()
            }
            override fun onError(e: Exception) {
                Toast.makeText(this@MainActivity, "Error: ${e.message}", Toast.LENGTH_SHORT)
                    .show()
            }
        })

Another example in Kotlin demonstrating usage of TrackApi.

client.methods.tracksApi.getTrack("7xGfFoTpQ2E7fRF5lN10tr",
    object : ResponseAction<com.kpstv.spotifyapi.data.models.Track> {
        override fun onComplete(t: Track) {
            Toast.makeText(this@MainActivity, "Name: ${t.name}", Toast.LENGTH_SHORT).show()
        }
        override fun onError(e: Exception) {
            Toast.makeText(this@MainActivity, "Error: ${e.message}", Toast.LENGTH_SHORT)
                .show()
        }
    })

If you want to understand more about how to use such API make sure to refer spotify-web-api-reference.

Clone this wiki locally