### Get Recommended Servers
Source: https://github.com/jellyfin/jellyfin-sdk-kotlin/blob/master/docs/guide/getting-started.md
Determine the best server to connect to from a list of candidates. This example retrieves recommended servers with a 'GOOD' score.
```kotlin
// Get a flow of potential servers to connect to
val recommended = jellyfin.discovery.getRecommendedServers(candidates, RecommendedServerInfoScore.GOOD)
```
--------------------------------
### List Libraries using Jellyfin CLI
Source: https://github.com/jellyfin/jellyfin-sdk-kotlin/blob/master/samples/kotlin-cli/README.md
This example shows how to set the Jellyfin server URL and authenticate with the CLI tool to list all available libraries. Ensure the JELLYFIN_SERVER and JELLYFIN_TOKEN environment variables are set before running.
```sh
JELLYFIN_SERVER="https://demo.jellyfin.org/stable"
JELLYFIN_TOKEN=`jellyfin login --username demo`
jellyfin libraries
```
--------------------------------
### Discover Local Servers
Source: https://github.com/jellyfin/jellyfin-sdk-kotlin/blob/master/docs/guide/getting-started.md
Discover Jellyfin servers on the local network using the discovery feature. This example prints the name and address of each found server.
```kotlin
// Discover servers on the local network
jellyfin.discovery.discoverLocalServers().collect {
println("Server ${it.name} was found at address ${it.address}")
}
```
--------------------------------
### Get Server Address Candidates
Source: https://github.com/jellyfin/jellyfin-sdk-kotlin/blob/master/docs/guide/getting-started.md
Obtain a list of potential server addresses for a given input string. This is useful for normalizing server addresses.
```kotlin
// Get all candidates for a given input
val candidates = jellyfin.discovery.getAddressCandidates("demo.jellyfin.org/stable")
```
--------------------------------
### Configure Snapshot Repository
Source: https://github.com/jellyfin/jellyfin-sdk-kotlin/blob/master/docs/guide/getting-started.md
Add the snapshot repository to your build script if you need to use SNAPSHOT versions of the SDK. This example configures Gradle to only allow the 'master-SNAPSHOT' version.
```kotlin
repositories {
maven("https://s01.oss.sonatype.org/content/repositories/snapshots/") {
content {
// Only allow SDK snapshots
includeVersionByRegex("org\\.jellyfin\\.sdk", ".*", "master-SNAPSHOT")
}
}
}
```
--------------------------------
### Create API Instance
Source: https://github.com/jellyfin/jellyfin-sdk-kotlin/blob/master/docs/guide/getting-started.md
Create an API instance by providing the server base URL. Client and device information are set automatically but can be overridden. Additional HTTP client options can also be configured.
```kotlin
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
)
```
--------------------------------
### Create API Client with Access Token
Source: https://github.com/jellyfin/jellyfin-sdk-kotlin/blob/master/docs/guide/authentication.md
Instantiate an API client with a pre-existing access token. This is useful if you already have a token from a previous session.
```kotlin
val api = jellyfin.createApi(
baseUrl = "https://demo.jellyfin.org/stable/",
accessToken = "02a7174a4d1843448b6d177d8288efd0",
)
```
--------------------------------
### Create API Instance and Subscribe to All WebSocket Messages
Source: https://github.com/jellyfin/jellyfin-sdk-kotlin/blob/master/docs/guide/websockets.md
Instantiate the API client and subscribe to all incoming WebSocket messages. The connection is managed automatically based on subscription activity.
```kotlin
val api = jellyfin.createApi(baseUrl = "https://demo.jellyfin.org/stable/")
api.webSocket.subscribeAll().collect { message ->
println(message)
}
```
--------------------------------
### Check Quick Connect Server Status
Source: https://github.com/jellyfin/jellyfin-sdk-kotlin/blob/master/docs/guide/authentication.md
Verify if Quick Connect is enabled on the Jellyfin server before initiating a session. Responds with HTTP 401 if disabled.
```kotlin
val api = jellyfin.createApi(/* .. */)
// Check if Quick Connect is enabled (this is optional)
val enabled by api.quickConnectApi.getQuickConnectEnabled()
if (!enabled) println("QuickConnect is disabled in the server!")
// Create a Quick Connect session and store the state
try {
val quickConnectState by api.quickConnectApi.initiateQuickConnect()
} catch (err: InvalidStatusException) {
if (err.status == 401) {
// Quick Connect is disabled
println("QuickConnect is disabled in the server!")
}
}
```
--------------------------------
### Authenticate with Quick Connect
Source: https://github.com/jellyfin/jellyfin-sdk-kotlin/blob/master/docs/guide/authentication.md
Retrieve an access token after a user has authorized the application via Quick Connect. Requires the session secret obtained during initiation.
```kotlin
val authenticationResult by api.userApi.authenticateWithQuickConnect(
secret = quickConnectState.secret,
)
// Use access token in api instance
api.update(accessToken = authenticationResult.accessToken)
// Print session information
println(authenticationResult.sessionInfo)
```
--------------------------------
### Create Jellyfin Instance
Source: https://github.com/jellyfin/jellyfin-sdk-kotlin/blob/master/docs/guide/getting-started.md
Instantiate the Jellyfin class using a Kotlin DSL. Provide essential client information. The Android context is required for Android applications.
```kotlin
val jellyfin = createJellyfin {
clientInfo = ClientInfo(name = "My awesome client!", version = "1.33.7")
// Uncomment when using android:
// context = /* Android Context */
}
```
--------------------------------
### Subscribe to Filtered Play State WebSocket Commands
Source: https://github.com/jellyfin/jellyfin-sdk-kotlin/blob/master/docs/guide/websockets.md
Listen for specific play state commands, such as NEXT_TRACK or PREVIOUS_TRACK. All play state commands are received if the 'commands' set is omitted.
```kotlin
api.webSocket.subscribePlayStateCommands(
commands = setOf(PlaystateCommand.NEXT_TRACK, PlaystateCommand.PREVIOUS_TRACK)
).collect { message ->
// type of message is PlaystateMessage
println("Received a message: $message")
}
```
--------------------------------
### Authenticate User by Name and Password
Source: https://github.com/jellyfin/jellyfin-sdk-kotlin/blob/master/docs/guide/authentication.md
Log in to the Jellyfin server using username and password. Handles invalid credentials by throwing an InvalidStatusException with status code 401.
```kotlin
val api = jellyfin.createApi(/* .. */)
try {
val authenticationResult by api.userApi.authenticateUserByName(
username = "demo",
password = "",
)
// Use access token in api instance
api.update(accessToken = authenticationResult.accessToken)
// Print session information
println(authenticationResult.sessionInfo)
} catch (err: InvalidStatusException) {
if (err.status == 401) {
// Username or password is incorrect
println("Invalid user")
}
}
```
--------------------------------
### Subscribe to WebSocket Events
Source: https://github.com/jellyfin/jellyfin-sdk-kotlin/blob/master/docs/guide/getting-started.md
Use the SocketApi to subscribe to all WebSocket events. Incoming messages are collected and printed to the console.
```kotlin
api.webSocket.subscribeAll().collect { message ->
println(message)
}
```
--------------------------------
### Run OpenAPI Generator Tasks with Gradle
Source: https://github.com/jellyfin/jellyfin-sdk-kotlin/blob/master/openapi-generator/README.md
These Gradle tasks are used to manage the generation and verification of Kotlin source code from OpenAPI specifications. Use `updateApiSpecStable` or `updateApiSpecUnstable` to download the spec and generate/update sources.
```bash
gradlew openapi-generator:updateApiSpecStable
git add .
git commit -m "Update generated sources"
git push
```
--------------------------------
### Subscribe to Filtered SyncPlay WebSocket Commands
Source: https://github.com/jellyfin/jellyfin-sdk-kotlin/blob/master/docs/guide/websockets.md
Receive specific SyncPlay commands, like PAUSE or UNPAUSE. If no commands are specified, all SyncPlay commands are sent.
```kotlin
api.webSocket.subscribeSyncPlayCommands(
commands = setOf(SendCommandType.PAUSE, SendCommandType.UNPAUSE)
) { message ->
// type of message is SyncPlayCommandMessage
println("Received a message: $message")
}
```
--------------------------------
### Subscribe to All WebSocket Messages
Source: https://github.com/jellyfin/jellyfin-sdk-kotlin/blob/master/docs/guide/websockets.md
Subscribe to all available WebSocket message types. This is less efficient if you only need specific message types.
```kotlin
api.webSocket.subscribeAll().collect { message ->
// type of message is OutboundWebSocketMessage
println("Received a message: $message")
}
```
--------------------------------
### Add Jellyfin Core Dependency
Source: https://github.com/jellyfin/jellyfin-sdk-kotlin/blob/master/docs/guide/getting-started.md
Include the Jellyfin core library in your project. Choose the appropriate dependency for your build system (Gradle Kotlin, Gradle Groovy, or Maven).
```kotlin
implementation("org.jellyfin.sdk:jellyfin-core:$sdkVersion")
```
```groovy
implementation "org.jellyfin.sdk:jellyfin-core:$sdkVersion"
```
```xml
org.jellyfin.sdk
jellyfin-core
$sdkVersion
```
--------------------------------
### Subscribe to Filtered General Command WebSocket Messages
Source: https://github.com/jellyfin/jellyfin-sdk-kotlin/blob/master/docs/guide/websockets.md
Receive specific general command messages, like DISPLAY_MESSAGE, by filtering the command type. If no commands are specified, all general commands are received.
```kotlin
api.webSocket.subscribeGeneralCommand(GeneralCommandType.DISPLAY_MESSAGE).collect { message ->
// type of message is GeneralCommandMessage
println("Received a message: $message")
}
```
--------------------------------
### Update Quick Connect Session State
Source: https://github.com/jellyfin/jellyfin-sdk-kotlin/blob/master/docs/guide/authentication.md
Periodically poll the server to update the state of an ongoing Quick Connect session. This is crucial for detecting user authorization.
```kotlin
// Update the Quick Connect session state
quickConnectState = api.quickConnectApi.getQuickConnectState(
secret = quickConnectState.secret,
)
```
--------------------------------
### Authenticate User
Source: https://github.com/jellyfin/jellyfin-sdk-kotlin/blob/master/docs/guide/getting-started.md
Authenticate a user using their username and password. The access token obtained from the authentication result can then be used to update the API instance for subsequent authenticated requests. Exceptions during authentication are caught.
```kotlin
val userApi = api.userApi
try {
val authenticationResult by userApi.authenticateUserByName(
username = "demo",
password = "",
)
// Use access token in api instance
api.update(accessToken = authenticationResult.accessToken)
// Print session information
println(authenticationResult.sessionInfo)
} catch(err: ApiClientException) {
// Catch exceptions
println("Something went wrong! ${err.message}")
}
```
--------------------------------
### Update Access Token in Existing API Client
Source: https://github.com/jellyfin/jellyfin-sdk-kotlin/blob/master/docs/guide/authentication.md
Modify the access token of an already created API client instance. This is useful for refreshing tokens or switching users.
```kotlin
api.update(accessToken = "02a7174a4d1843448b6d177d8288efd0")
```
--------------------------------
### Subscribe to Specific WebSocket Message Type
Source: https://github.com/jellyfin/jellyfin-sdk-kotlin/blob/master/docs/guide/websockets.md
Listen for a particular type of WebSocket message, such as UserDataChangedMessage. The SDK handles connection and message deserialization.
```kotlin
api.webSocket.subscribe().collect { message ->
// type of message is UserDataChangedMessage
println("Received a message: $message")
}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.