Skip to content

livekit/server-sdk-kotlin

Repository files navigation

The LiveKit icon, the name of the repository and some sample code in the background.

LiveKit Server SDK for Kotlin

Use this SDK to interact with LiveKit server APIs and create access tokens from your Kotlin backend.

Installation

This SDK is available as a Maven package through Maven Central.

Maven

<dependencies>
    <dependency>
        <groupId>io.livekit</groupId>
        <artifactId>livekit-server</artifactId>
        <version>0.14.0</version>
    </dependency>
</dependencies>

Gradle

dependencies {
    implementation 'io.livekit:livekit-server:0.14.0'
}

Usage

Server API Access

LiveKitAPI is a single entry point to every server API, exposing each service (room, egress, ingress, sip, agentDispatch, connector). Create it with createClient (API key & secret, for backend use) or createClientWithToken (a pre-signed token, for client-side use where the API secret must not be exposed). Host and credentials fall back to the LIVEKIT_URL, LIVEKIT_API_KEY, LIVEKIT_API_SECRET, and LIVEKIT_TOKEN environment variables. Values you pass explicitly take precedence; the environment variables are used only as a fallback for arguments you omit — an ambient LIVEKIT_TOKEN, for example, won't override an explicitly-provided API key and secret.

package org.example;

import com.google.protobuf.util.JsonFormat;

import java.io.IOException;

import io.livekit.server.LiveKitAPI;
import livekit.LivekitModels;
import retrofit2.Call;
import retrofit2.Response;

public class Main {
  public static void main(String[] args) throws IOException {

    // With LIVEKIT_URL, LIVEKIT_API_KEY, and LIVEKIT_API_SECRET set, call createClient()
    // with no arguments; pass any of them to override the corresponding env var.
    LiveKitAPI api = LiveKitAPI.createClient();
    // explicit:    LiveKitAPI.createClient("http://example.com", "apiKey", "secret");
    // client-side: LiveKitAPI.createClientWithToken("http://example.com", token); // token from LIVEKIT_TOKEN if omitted

    Call<LivekitModels.Room> call = api.getRoom().createRoom("room_name");
    Response<LivekitModels.Room> response = call.execute(); // Use call.enqueue for async
    LivekitModels.Room room = response.body();

    System.out.println(JsonFormat.printer().print(room));
  }
}

Call adapters are also available through Retrofit for other async constructs such as CompletableFuture and RxJava. The individual service clients (RoomServiceClient, etc.) can also be created directly with the same arguments.

Error handling

Retrofit doesn't raise on API errors, so a failed call comes back as an unsuccessful response. ServerError.from(response) decodes the error code, message, and metadata from it (returns null if the response succeeded or isn't a server error):

import io.livekit.server.ServerError;

Response<LivekitModels.Room> response = api.getRoom().createRoom("my-room").execute();
if (!response.isSuccessful()) {
    ServerError error = ServerError.from(response);
    if (error != null) {
        System.out.println(error.getCode() + ": " + error.getMessage());
    }
}

A failed SIP dial (e.g. the callee is busy) can be decoded as a SipCallError (a ServerError subclass) that also exposes the SIP status. SipCallError.from(response) returns null if it isn't a SIP failure:

import io.livekit.server.SipCallError;

Response<LivekitSip.SIPParticipantInfo> response =
    api.getSip().createSipParticipant("ST_trunk", "+15105550100", "my-room", null).execute();
if (!response.isSuccessful()) {
    SipCallError error = SipCallError.from(response);
    if (error != null && Integer.valueOf(486).equals(error.getSipStatusCode())) {
        // callee is busy
    }
}

Creating Access Tokens

Access tokens can be generated through the io.livekit.server.AccessToken class.

AccessToken token = new AccessToken("apiKey", "secret");

// Fill in token information.
token.setName("name");
token.setIdentity("identity");
token.setMetadata("metadata");
token.addGrants(new RoomJoin(true), new RoomName("myroom"));

// Sign and create token string.
System.out.println("New access token: " + token.toJwt())

By default, tokens expire 6 hours after generation. You may override this by using token.setTtl(long millis).


LiveKit Ecosystem
Agents SDKsPython · Node.js
LiveKit SDKsBrowser · Swift · Android · Flutter · React Native · Rust · Node.js · Python · Unity · Unity (WebGL) · ESP32 · C++
Starter AppsPython Agent · TypeScript Agent · React App · SwiftUI App · Android App · Flutter App · React Native App · Web Embed
UI ComponentsReact · Android Compose · SwiftUI · Flutter
Server APIsNode.js · Golang · Ruby · Java/Kotlin · Python · Rust · PHP (community) · .NET (community)
ResourcesDocs · Docs MCP Server · CLI · LiveKit Cloud
LiveKit Server OSSLiveKit server · Egress · Ingress · SIP
CommunityDeveloper Community · Slack · X · YouTube

About

No description, website, or topics provided.

Resources

License

Contributing

Stars

81 stars

Watchers

15 watching

Forks

Packages

 
 
 

Contributors