Kotlin Spotify Web API Wrapper (KMP)
Spotify Web API wrapper for Kotlin Multiplatform.
This repository provides a type-safe, coroutine-friendly Spotify API client for Kotlin, Android, iOS, JVM, and JS.
Why This Library
- Kotlin-first Spotify Web API wrapper for KMP projects
- Type-safe request/response models with kotlinx.serialization
- Coroutine-based API calls with Ktor client
- Shared business logic across Android, iOS, JVM, and JS
Status
Installation (Maven Central)
Dependency coordinates:
- Group:
io.github.nubasu
- Artifact:
kotlin-spotify-web-api-wrapper
Recommendation : libs.versions.toml (Version Catalog)
gradle/libs.versions.toml:
[versions]
kotlinSpotifyWebApiWrapper = "2.0.0"
[libraries]
kotlin-spotify-web-api-wrapper = { module = "io.github.nubasu:kotlin-spotify-web-api-wrapper", version.ref = "kotlinSpotifyWebApiWrapper" }
build.gradle.kts:
repositories {
mavenCentral()
}
commonMain.dependencies {
implementation(libs.kotlin.spotify.web.api.wrapper)
}
build.gradle.kts (Kotlin DSL)
repositories {
mavenCentral()
}
commonMain.dependencies {
implementation("io.github.nubasu:kotlin-spotify-web-api-wrapper:2.0.0")
}
build.gradle (Groovy DSL)
repositories {
mavenCentral()
}
dependencies {
implementation 'io.github.nubasu:kotlin-spotify-web-api-wrapper:2.0.0'
}
Features
- Spotify Web API coverage for albums, artists, playlists, player, tracks, users, and more
- Authorization flows: Authorization Code with PKCE, Authorization Code, Client Credentials, Refresh Token
- Pagination helpers, rate-limit handling, and retry support
- Unit-tested non-private functions
- Sample app and docs for end-to-end auth-to-API usage
Documentation
Sample App (composeApp)
- A sample app is included in
composeApp/.
- It supports:
PKCE auth -> load current user's playlists -> start/pause playback.
- Android callback deep link:
spotifyauth://callback (configure this in Spotify Dashboard redirect URIs).
- JVM/Desktop auto callback:
http://127.0.0.1:8888/callback (configure this in Spotify Dashboard redirect URIs).
- Desktop/JVM: run
./gradlew :composeApp:run.
Quick Start
import com.nubasu.spotify.webapi.wrapper.SpotifyClient
import com.nubasu.spotify.webapi.wrapper.api.authorization.SpotifyAuthManager
val auth = SpotifyAuthManager(
clientId = "YOUR_CLIENT_ID",
redirectUri = "your.app://callback"
)
val pkce = auth.startPkceAuthorizationAndLaunch(scope = listOf("user-read-email"))
// Android/iOS: prefer in-app authentication UI, otherwise open the browser
// After the redirect comes back:
auth.completePkceAuthorizationFromRedirectUri(redirectedUri)
// asTokenProvider() wires automatic token refresh into every API call:
// PKCE / Authorization Code refresh with the stored refresh token,
// Client Credentials reacquires a fresh token (no refresh_token needed).
// A refresh failure surfaces as the original Spotify error inside
// SpotifyResult.Failure - API calls never throw.
val spotify = SpotifyClient(tokenProvider = auth.asTokenProvider())
val album = spotify.albums.getAlbum("album-id")
Stability & Versioning
- SemVer. Breaking changes ship only in major releases; 2.0 collected all
breaking changes accumulated during 1.x (see CHANGELOG.md and the
migration guide).
- Explicit, pinned API surface. The library compiles in Kotlin
explicit API mode,
and the public ABI is committed under
shared/api/*.api and enforced by
binary-compatibility-validator (apiCheck) in CI.
- Data classes in the public API. Request/response models are deliberately
data class for serialization and destructuring ergonomics, accepting that
adding fields can break copy/componentN binary compatibility — such
changes are treated as breaking and reserved for major releases, with bcv as
the tripwire.
- Deprecation policy. Members that mirror endpoints Spotify itself marks as
deprecated stay available (with
@Deprecated warnings) until Spotify removes
them. Wrapper-level compatibility shims (for example TokenHolder) are
@Deprecated(WARNING) and are removed in the next major release.
References
Contributing
PRs/issues are welcome.
- If you find a bug: open an issue with steps to reproduce.
- If you want to add an endpoint: follow existing conventions and include tests.
- If you add any features: open an issue or open PR!