Skip to main content

Client Initialization

KoaniClient is the main entry point for interacting with the MyAnimeList API. You can create it using either the KoaniClient DSL or KoaniClient.Builder; both support configuring credentials, token storage, timeouts, and logging levels.

Basic Initialization​

To make unauthenticated API requests (such as searching anime, fetching public manga details, or browsing forums), you only need a MyAnimeList Client ID:

import io.github.rribeiro5.koani.KoaniClient

val client = KoaniClient(clientId = "YOUR_MYANIMELIST_CLIENT_ID")

Or with KoaniClient.Builder:

import io.github.rribeiro5.koani.KoaniClient

val client = KoaniClient.Builder(clientId = "YOUR_MYANIMELIST_CLIENT_ID")
.build()

Note: The KoaniClient DSL factory is available starting with version 2.1.0. Use KoaniClient.Builder with earlier versions.

Advanced Configuration​

For apps that require user authentication, custom token storage, network timeouts, or debug logging, configure the builder accordingly:

import io.github.rribeiro5.koani.KoaniClient
import io.github.rribeiro5.koani.LogLevel
import io.github.rribeiro5.koani.auth.persistence.ksafe.KSafeTokenManager
import eu.anifantakis.lib.ksafe.KSafe

// Optional secure token manager using KSafe
val kSafe = KSafe()
val persistentTokenManager = KSafeTokenManager(kSafe)

val client = KoaniClient(clientId = "YOUR_MYANIMELIST_CLIENT_ID") {
clientSecret("YOUR_OPTIONAL_CLIENT_SECRET") // Optional
tokenManager(persistentTokenManager) // Optional: Custom or persistent token manager
timeoutMillis(30_000L) // Optional: 30 second timeout
logLevel(LogLevel.DEBUG) // Optional: Debug logging
}

Builder Options Summary​

ParameterTypeDefaultDescription
clientIdString(Required)Your MyAnimeList application Client ID.
clientSecretString?nullYour MyAnimeList Client Secret (if required by your API app type).
tokenManagerTokenManagerMemoryTokenManager()Strategy for storing and retrieving OAuth2 access/refresh tokens.
timeoutMillisLong?nullTimeout in milliseconds for request, connect, and socket timeouts.
logLevelLogLevelLogLevel.NONESeverity level for internal client logging and network requests.

Managing Client Instances​

For best performance and resource utilization, maintain a single instance of KoaniClient across your application lifetime.

Koin Example​

val appModule = module {
single {
KoaniClient(clientId = "YOUR_CLIENT_ID") {
logLevel(LogLevel.INFO)
}
}
}

Hilt Example​

@Module
@InstallIn(SingletonComponent::class)
object KoaniModule {

@Provides
@Singleton
fun provideKoaniClient(): KoaniClient {
return KoaniClient(clientId = "YOUR_CLIENT_ID") {
timeoutMillis(15_000L)
}
}
}