Kotlin SDK for Jellyfin, supporting Android and JVM targets
Go to file
2022-04-17 11:09:35 +02:00
.github Bump actions/setup-java from 2 to 3 2022-04-17 11:09:25 +02:00
buildSrc Disable wildcard imports (#344) 2021-12-23 13:41:22 +01:00
docs WebSocket API rewrite (#352) 2022-03-26 20:45:43 +01:00
gradle Update Gradle Wrapper from 7.4.1 to 7.4.2. 2022-04-01 09:39:42 +02:00
jellyfin-api Add retry logic to SocketInstance (#373) 2022-04-09 23:15:54 +02:00
jellyfin-core Add OkHttpWebsocketSession for JVM and Android targets (#366) 2022-04-04 21:58:51 +02:00
jellyfin-model WebSocket API rewrite (#352) 2022-03-26 20:45:43 +01:00
openapi-generator Add generator task for stable-pre openapi files 2022-04-17 11:09:35 +02:00
samples WebSocket API rewrite (#352) 2022-03-26 20:45:43 +01:00
testutils Use kotlinx.coroutines.test in unit tests 2021-12-22 10:49:38 +01:00
.editorconfig Disable wildcard imports (#344) 2021-12-23 13:41:22 +01:00
.gitattributes Add .api files 2021-04-03 10:59:54 +02:00
.gitignore Update gitignore 2020-09-13 13:05:34 +02:00
android-lint.xml Miscellaneous build script changes (#289) 2021-07-08 23:03:33 +02:00
build.gradle.kts Require explicit enabling of publishing (#331) 2021-10-17 14:36:57 +00:00
CODEOWNERS Add CODEOWNERS file 2021-03-18 22:08:46 +01:00
detekt.yaml Disable wildcard imports (#344) 2021-12-23 13:41:22 +01:00
gradle.properties Use Java 17 as default and move to Adoptium distribution (#321) 2021-10-04 19:15:16 +02:00
gradlew Update Gradle Wrapper from 7.1.1 to 7.2. (#305) 2021-08-18 17:06:07 +02:00
gradlew.bat Update Gradle Wrapper from 6.7 to 6.8.2. 2021-02-22 04:19:49 +00:00
LICENSE Update license to LGPL 3.0 2020-09-13 17:08:50 +02:00
openapi.json Update generated sources to 10.7.7 (#320) 2021-09-08 19:34:01 +02:00
README.md Update README (#370) 2022-04-09 13:31:45 +02:00
settings.gradle.kts Update Gradle Wrapper from 7.3.3 to 7.4 (#355) 2022-02-09 18:14:58 +01:00

Jellyfin Kotlin SDK

Part of the Jellyfin Project


Logo Banner

LGPL 3.0 license Current Release Maven Central Release
Donate Chat on Matrix Join our Subreddit Release RSS Feed Master Commits RSS Feed


The Jellyfin Kotlin SDK is a library implementing the Jellyfin API to easily access servers. It is currently available for the JVM and Android 4.4 and up. Java 8 or higher is required. Android versions below Android 8 should use library desugaring.

Setup

Releases are published to mavenCentral(). Make sure to use the correct library depending on your platform.

Latest version on Maven Central

Gradle with Kotlin DSL

implementation("org.jellyfin.sdk:jellyfin-core:$sdkVersion")
Gradle with Groovy
implementation "org.jellyfin.sdk:jellyfin-core:$sdkVersion"
Maven
<dependency>
    <groupId>org.jellyfin.sdk</groupId>
    <artifactId>jellyfin-core</artifactId>
    <version>$sdkVersion</version>
</dependency>
Using SNAPSHOT versions

When working on new features in your application you might need a build of the SDK targeting the next server version. For this use case we publish two SNAPSHOT releases: master-SNAPSHOT and openapi-unstable-SNAPSHOT. To use the snapshot versions, add the snapshot repository to your build script: https://s01.oss.sonatype.org/content/repositories/snapshots/

An example using Gradle with Kotlin DSL that only allows the master-SNAPSHOT version:

repositories {
    maven("https://s01.oss.sonatype.org/content/repositories/snapshots/") {
        content {
            // Only allow SDK snapshots
            includeVersionByRegex("org\\.jellyfin\\.sdk", ".*", "master-SNAPSHOT")
        }
    }
}

Usage

Creating a Jellyfin instance

Most functionality of the SDK requires an instance of the Jellyfin class. This class holds the configuration required to make API calls and platform specific options. The Jellyfin class can be instantiated using a custom Kotlin DSL:

val jellyfin = createJellyfin {
    clientInfo = ClientInfo(name = "My awesome client!", version = "1.33.7",)

    // Uncomment when using android:
    // context = /* Android Context */
}

Make sure to supply the client information if you want to make API calls. The context is required for Android applications.

Creating an API instance

API calls require an API instance. This can be done with the createApi function. It requires a server address. The client and device information are set automatically but can be changed. All properties can be changed later in the API instance.

val api = jellyfin.createApi(
    baseUrl = "https://demo.jellyfin.org/stable/",
    // Optional options:
    // accessToken = "access token or api key"
    // clientInfo = ClientInfo(), // defaults to parent info
    // deviceInfo = DeviceInfo(), // defaults to parent info
    // httpClientOptions = HttpClientOptions() // allows setting additional options
)

Authenticating a user

All API operations are grouped. To make use of an operation you need to first get the group from your ApiClient instance. All groups are defined as extension functions, and you can alternatively construct the API instances manually.

val userApi = api.userApi

try {
    val authenticationResult by userApi.authenticateUserByName(
        username = "demo", 
        password = "",
    )
    
    // Use access token in api instance
    api.accessToken = authenticationResult.accessToken
    
    // Print session information
    println(authenticationResult.sessionInfo)
} catch(err: ApiClientException) {
    // Catch exceptions
    println("Something went wrong! ${err.message}")
}

WebSockets

Jellyfin uses WebSockets to communicate events like library changes and activities. This API can be used with the SocketApi. Documentation is available in the docs folder.

val instance = api.ws()

instance.addGlobalListener { message ->
	println(message)
}

Server discovery

The server discovery feature can be used to find servers on the local network, normalize inputted server addresses and to determine the best server to use from a list of addresses.

// Discover servers on the local network
jellyfin.discovery.discoverLocalServers().collect {
    println("Server ${it.name} was found at address ${it.address}")
}

// Get all candidates for a given input
val candidates = jellyfin.discovery.getAddressCandidates("demo.jellyfin.org/stable")

// Get a flow of potential servers to connect to
val recommended = jellyfin.discovery.getRecommendedServers(candidates, RecommendedServerInfoScore.GOOD)

More examples

We provide a few small projects in the samples folder. The samples are used for testing new features and can be used as a basis for your own application.

Projects using the SDK

Official Jellyfin clients

Third party clients

  • Findroid provides a native user interface to browse and play movies and series.

Want to add your project? We'd love to know about it, open an issue or create pull request!