Skip to content
Distractic edited this page Aug 13, 2023 · 2 revisions

The Mojang API Implementation allows you to easily interact with API. You just need to define a Http client from Ktor.

import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.serialization.json.Json
import com.github.rushyverse.mojang.api.MojangAPI
import com.github.rushyverse.mojang.api.MojangAPIImpl

suspend fun main() {
    // We advise to ignore unknown keys in case of api change
    val json = Json {
        ignoreUnknownKeys = true
    }

    // You can use another engine (other than CIO) for your http client
    val httpClient = HttpClient(CIO) {
        // Necessary to transform the response from api to a data object
        install(ContentNegotiation) {
            json(json)
        }
    }

    val mojangApi: MojangAPI = MojangAPIImpl(httpClient)
    // you can interact with Mojang api
    println(mojangApi.getUUID("Notch"))
}
Clone this wiki locally