LiveKit Server SDK

2.15.1 · active · verified Sun Apr 19

The LiveKit Server SDK, currently at version 2.15.1, provides a comprehensive set of APIs for managing LiveKit rooms, generating access tokens, and processing webhooks from a server-side JavaScript, TypeScript, Node.js, Deno, or Bun environment. This SDK is actively maintained with frequent patch releases, as evidenced by recent updates addressing memory leaks and adding new features like `ringingTimeout` for SIP participants. A key differentiator of this SDK is its multi-runtime compatibility, supporting Node.js (>=18), Deno, and Bun, a significant evolution from its v1 predecessor which was primarily Node.js focused. It allows developers to programmatically control aspects of LiveKit sessions, such as listing, creating, and deleting rooms, and assigning granular permissions to participants joining rooms. The library ships with TypeScript types, facilitating robust development. While it theoretically runs in browsers, the documentation strongly advises against it due to security implications of exposing API secrets.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to generate an access token for a LiveKit participant to join a specified room, including setting basic permissions and token expiration.

import { AccessToken } from 'livekit-server-sdk';

// Load API keys from environment variables for security.
// NEVER hardcode secrets in production code.
const LIVEKIT_API_KEY = process.env.LIVEKIT_API_KEY ?? '';
const LIVEKIT_API_SECRET = process.env.LIVEKIT_API_SECRET ?? '';

const roomName = 'my-dynamic-room-' + Math.random().toString(36).substring(7);
const participantIdentity = 'user-' + Math.random().toString(36).substring(7);

async function generateLiveKitToken() {
  if (!LIVEKIT_API_KEY || !LIVEKIT_API_SECRET) {
    console.error('LIVEKIT_API_KEY and LIVEKIT_API_SECRET must be set as environment variables or provided directly.');
    return;
  }

  const at = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET, {
    identity: participantIdentity,
    name: `User ${participantIdentity.substring(5)}`,
    ttl: '1h', // Token expires in 1 hour
  });

  at.addGrant({
    roomJoin: true,
    room: roomName,
    canPublish: true,      // Participant can publish audio/video
    canSubscribe: true,    // Participant can subscribe to others' tracks
    canUpdateOwnMetadata: true,
    // Additional permissions like canPublishSources, canUpdateRoom, etc., can be added here
  });

  const token = await at.toJwt();
  console.log(`Generated LiveKit Access Token for participant '${participantIdentity}' in room '${roomName}':`);
  console.log(token);
  console.log('\nThis token can be used by a LiveKit client SDK to join the room.');
}

generateLiveKitToken().catch(console.error);

view raw JSON →