Skip to content

Latest commit

 

History

History
74 lines (55 loc) · 3.5 KB

File metadata and controls

74 lines (55 loc) · 3.5 KB

Overview

In order for client apps to connect to LiveKit rooms, they need to present an Access Token with the appropriate permission grants. For web applications, the process of generating tokens can often be easily incorporated into your existing web server. For other targets like mobile and desktop applications, you will need a server with an endpoint to generate these tokens.

This template implements a minimal token server using Node.js, TypeScript, Express, and the LiveKit Server SDK.

Sandbox

When running this token server as a sandbox app, LiveKit hosts a public endpoint you can use to generate tokens. To generate a token, simply make a POST request to https://cloud-api.livekit.io/api/sandbox/connection-details with the following request format:

Request format
Method POST
URL https://cloud-api.livekit.io/api/v2/sandbox/connection-details
Headers X-Sandbox-ID (your Sandbox ID)
Content-Type application/json
Optional Body room_name (optional room name)
participant_identity (optional participant identity)
participant_name (optional participant name)
participant_metadata (optional participant metadata)
participant_attributes (optional participant attributes)
room_config (optional room config)
Response format
Response Body server_url Room connection url
participant_token Room connection token

Generated tokens will have a default TTL of 15 minutes. Optional parameters not included in the request will be generated by the token server.

Installation

To use this template locally, clone the repo or use the LiveKit CLI:

lk app create --template token-server-node my-server

You can also bootstrap a minimal token server yourself with the following code:

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

async function createToken({ roomName, participantName }) {
  const at = new AccessToken('my-key', 'my-secret', {
    identity: participantName,
    ttl: '10m',
  });
  at.addGrant({
    roomJoin: true,
    room: roomName,
    canUpdateOwnMetadata: true,
  });
  return at.toJwt();
}

const app = express();
const port = 3000;

app.post('/token', async (req, res) => {
  const { roomName = 'demo-room', participantName = 'demo-user' } = req.body || {};
  const token = await createToken({ roomName, participantName });
  res.send(token);
});

app.listen(port, () => {
  console.log(`Server listening on port ${port}`);
});

For more information on tokens, see our documentation on generating tokens.