This Kotlin SDK is built to interact with the Magic: The Gathering API, providing a clean, Kotlin-centric interface to access the API's data. The SDK is designed to offer a more modern and structured approach with features such as UseCases, tailored for better flexibility and ease of use compared to other SDKs available.
The SDK was developed to offer a more modern alternative to the officially listed Kotlin SDK for the Magic: The Gathering API, which also don't appear to be maintained anymore. It aims to provide developers with a better, more organized way to interact with the API while being easy to integrate into your Kotlin-based applications.
The Magic: The Gathering API documentation can be found here for a deeper understanding of how the API works.
Feel free to use or fork it as per your needs!
WIP - Ongoing updates to complete development and polish features.
Full generated API reference (KDoc): Releases → API Docs
Once KDoc generation via Dokka is published (see #32), this link will point to the hosted API reference.
Internal layer design, data flow diagrams, extension points, and the guide for adding new endpoints: docs/ARCHITECTURE.md
Current SDK version: v1.0.0.81
There are three methods you can choose from to install the MTG API Kotlin SDK into your project: via
GitHub Packages or Maven Central (recommended) or by manually downloading the .jar file.
You can use GitHub Packages to install the SDK using Gradle. GitHub Packages requires authentication.
-
Add the GitHub Packages repository to your
build.gradle.ktsfile:repositories { maven { url = uri("https://maven.pkg.github.com/rikezero/mtgapi-kotlin-sdk") credentials { username = project.findProperty("gpr.user") as String? ?: System.getenv("USERNAME_GITHUB") password = project.findProperty("gpr.token") as String? ?: System.getenv("TOKEN_GITHUB") } } } -
Add the dependency:
dependencies { implementation("com.rikezero:mtgapi-kotlin-sdk:1.0.0.81") } -
Authenticate by creating a GitHub Personal Access Token with
read:packagesscope, then setgpr.userandgpr.tokenin your~/.gradle/gradle.properties(or as environment variables).
No authentication required.
-
Ensure
mavenCentral()is in your repositories (it is by default in most projects):// settings.gradle.kts dependencyResolutionManagement { repositories { mavenCentral() } } -
Add the dependency to your
build.gradle.kts:dependencies { implementation("io.github.rikezero:mtgapi-kotlin-sdk:1.0.0.81") }
-
Visit the Releases page and download the
.jarfile for the release you want. -
Place the
.jarin your project'slibs/directory. -
Add it as a dependency in your
build.gradle.kts:dependencies { implementation(files("libs/mtgapi-kotlin-sdk-1.0.0.81.jar")) }
Once installed, initialize the SDK's Koin modules after starting Koin and before using any SDK feature.
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
// 1. Start Koin with your own modules
startKoin {
androidContext(this@MyApplication)
modules(myAppModules)
}
// 2. Load SDK modules into the running Koin instance
startMtgApiLibrary()
// 3. Load your modules that depend on SDK types (e.g. inject GetCardsUseCase)
loadKoinModules(myModulesThatDependOnSdk)
}
}import org.koin.core.context.startKoin
fun main() {
// 1. Start Koin with your own modules
startKoin {
modules(myAppModules)
}
// 2. Load SDK modules into the running Koin instance
startMtgApiLibrary()
// 3. Load your modules that depend on SDK types
loadKoinModules(myModulesThatDependOnSdk)
}Order matters:
startMtgApiLibrary()must be called afterstartKoin { }but before injecting any SDK use case.
After initialization, inject a use case and make your first API call:
import com.rikezero.mtgapi_kotlin_sdk.domain.usecase.GetCardsUseCase
import com.rikezero.mtgapi_kotlin_sdk.domain.usecase.GetCardsUseCase.GetCardsParams
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
class CardRepository : KoinComponent {
private val getCards: GetCardsUseCase by inject()
suspend fun fetchDragons() {
val params = GetCardsParams(
name = "Dragon",
pageSize = 10
)
val result = getCards(params)
result
.onSuccess { cardList ->
println("Found ${cardList.cards.size} cards")
cardList.cards.forEach { println(it.name) }
}
.onFailure { error ->
println("Error: ${error.message}")
}
}
}This example assumes the library is already initialized. See Initializing the Library in Koin above.
All 8 use cases follow the same pattern — see Integration Samples for runnable examples of every endpoint.
The SDK includes standalone sample apps for every API endpoint. Each sample initializes dependencies manually (no host app required) and hits the real MagicTheGathering.io API, making them useful for quick endpoint validation.
| Gradle Task | Sample | Endpoint |
|---|---|---|
runCardListSample |
Card List | GET /v1/cards |
runCardByIdSample |
Card By ID | GET /v1/cards/{id} |
runCardSetsSample |
Card Sets | GET /v1/sets |
runCardSetByCodeSample |
Card Set By Code | GET /v1/sets/{code} |
runTypesSample |
Types | GET /v1/types |
runSubtypesSample |
Subtypes | GET /v1/subtypes |
runSupertypesSample |
Supertypes | GET /v1/supertypes |
runFormatsSample |
Formats | GET /v1/formats |
./gradlew runCardListSample
./gradlew runCardByIdSample
./gradlew runCardSetsSample
./gradlew runCardSetByCodeSample
./gradlew runTypesSample
./gradlew runSubtypesSample
./gradlew runSupertypesSample
./gradlew runFormatsSample./gradlew runAllSamplesEach sample follows the same pattern:
fun main() = runBlocking {
// 1. Build dependencies manually — no host app or Koin needed
val networking = setupNetworking()
val repository = MtgApiRepositoryImpl(networking)
val useCase = GetXxxUseCase(repository)
// 2. Call the use case
val result = useCase(params)
// 3. Handle result
result
.onSuccess { println(it) }
.onFailure { println("Error: ${it.message}") }
}Sample files are located in:
src/main/kotlin/com/rikezero/mtgapi_kotlin_sdk/samples/
Symptom: KoinAppAlreadyStartedException or NoBeanDefFoundException at startup before any API call is made.
Fix: Ensure startKoin { } is called first, then startMtgApiLibrary():
startKoin { modules(...) } // ← must come first
startMtgApiLibrary() // ← then thisSymptom: Gradle sync fails with 401 Unauthorized or Could not resolve com.rikezero:mtgapi-kotlin-sdk.
Fix:
-
Create a GitHub Personal Access Token with the
read:packagesscope. -
Add credentials to
~/.gradle/gradle.properties:gpr.user=YOUR_GITHUB_USERNAME gpr.token=YOUR_PERSONAL_ACCESS_TOKEN
Alternatively, switch to Maven Central (Option 2) which requires no authentication.
Symptom: Build fails with Duplicate class errors or Could not resolve for Koin, OkHttp, or Retrofit transitive dependencies.
Fix: Force-resolve the conflicting version in your build.gradle.kts:
configurations.all {
resolutionStrategy {
force("io.insert-koin:koin-core:3.4.2")
}
}Check the releases page for the dependency versions bundled with each SDK release.