LiveKit Server SDK
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
-
Error: signature verification failed
cause Incorrect LiveKit API Key or API Secret provided to the AccessToken constructor or when creating a WebhookReceiver.fixDouble-check your `LIVEKIT_API_KEY` and `LIVEKIT_API_SECRET` environment variables or constructor arguments. Ensure they match the credentials from your LiveKit Cloud project or self-hosted instance. -
TypeError: app.use() requires callback functions but got a [object Undefined]
cause Often seen in Express.js applications when trying to use middleware like `express.raw()` without importing it correctly or if Express.js is not initialized properly, specifically when handling webhook bodies.fixEnsure `express` is imported and `express.raw()` is correctly configured for the webhook endpoint: `import express from 'express'; const app = express(); app.use(express.raw({type: 'application/webhook+json'}));` -
ERR_REQUIRE_ESM: require() of ES Module <...>/livekit-server-sdk.js from <your-file>.js not supported
cause Attempting to use CommonJS `require()` syntax with `livekit-server-sdk` v2+ in a Node.js project that is configured for ES Modules, or vice-versa with a mixed configuration.fixIf your project is ESM-first (e.g., `"type": "module"` in `package.json`), use `import { ... } from 'livekit-server-sdk';`. If your project is CJS, you might need to ensure your transpilation setup is correct or check if an older CJS-compatible version is available (though v2 is primarily ESM).
Warnings
- breaking The SDK underwent a major rewrite from v1.x to v2.x, introducing breaking changes in API structure and package exports. Users migrating from v1 will need to review the dedicated migration guide.
- gotcha Exposing LiveKit API keys and secrets in client-side code (e.g., browsers) is a severe security risk. This SDK should ONLY be used in secure server-side environments.
- gotcha When integrating webhooks, ensure your HTTP server is configured to correctly parse `application/webhook+json` content type. Standard JSON parsers (e.g., `express.json()`) may not work.
Install
-
npm install livekit-server-sdk -
yarn add livekit-server-sdk -
pnpm add livekit-server-sdk
Imports
- AccessToken
const AccessToken = require('livekit-server-sdk').AccessToken;import { AccessToken } from 'livekit-server-sdk'; - RoomServiceClient
const { RoomServiceClient } = require('livekit-server-sdk');import { RoomServiceClient } from 'livekit-server-sdk'; - WebhookReceiver
import WebhookReceiver from 'livekit-server-sdk';
import { WebhookReceiver } from 'livekit-server-sdk';
Quickstart
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);