The Magic Admin Kotlin SDK provides convenient ways for developers to interact with Magic API endpoints and an array of utilities to handle DID Tokens.
See the Magic documentation for detailed information on how to use the Magic Admin SDK.
The SDK requires Kotlin and is compatible with JVM-based projects. You can include it in your project by adding the dependency to your build file.
Add the following to your build.gradle
file:
dependencies {
implementation 'com.skunkworks.magic:magic-admin-kotlin:1.0.0'
}
<dependencies>
<dependency>
<groupId>com.skunkworks.magic</groupId>
<artifactId>magic-admin-kotlin</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
Before you start, you will need an API secret key. You can get this from the Magic Dashboard. Once you have it, you can instantiate a MagicAdminClient object.
Retrieve User Info by DID Token Sample code to retrieve user info by a DID Token:
import com.skunkworks.magic.api.MagicAdminClient
import kotlinx.coroutines.runBlocking
fun main() = runBlocking {
val magicClient = MagicAdminClient(
secretKey = "<YOUR_API_SECRET_KEY>"
)
val userInfoResult = magicClient.getUserMetadataByToken("<DID_TOKEN>")
userInfoResult.onSuccess { userInfo ->
println("User Info: $userInfo")
}.onFailure { error ->
println("Error: ${error.message}")
}
}
}
Sample code to validate a DID Token and retrieve the claim and proof from the token:
import com.skunkworks.magic.token.Token
fun main() {
val didToken = "<DID_TOKEN>"
try {
val token = Token.decode(didToken)
token.validate()
println("Token is valid")
println("Claim: ${token.claim}")
println("Proof: ${token.proof}")
} catch (e: Exception) {
println("DID Token is invalid: ${e.message}")
}
}
The MagicAdminClient allows you to customize the HTTP client configuration, including timeouts and base URL:
import com.skunkworks.magic.api.MagicAdminClient
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.plugins.*
import kotlinx.coroutines.runBlocking
fun main() = runBlocking {
val customHttpClient = HttpClient(CIO) {
install(HttpTimeout) {
requestTimeoutMillis = 60_000
connectTimeoutMillis = 30_000
}
}
val magicClient = MagicAdminClient(
secretKey = "<YOUR_API_SECRET_KEY>",
httpClient = customHttpClient
)
// Use magicClient as needed
}
We welcome contributions to the SDK. To get started, clone this repository and set up your development environment.
To run the existing tests:
./gradlew test
To build the project:
./gradlew build
Changelog See Changelog for a history of changes.