Skip to content

Commit e2eedca

Browse files
authored
Merge pull request #2 from livekit-examples/tf/template-md
2 parents 0cc0469 + 8ebcf1b commit e2eedca

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

TEMPLATE.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## Overview
2+
3+
In order for client apps to connect to LiveKit rooms, they need to present an [Access Token](https://docs.livekit.io/home/get-started/authentication/) 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.
4+
5+
This template implements a minimal token server using Node.js, TypeScript, Express, and the [LiveKit Server SDK](https://github.com/livekit/node-sdks/tree/main/packages/livekit-server-sdk).
6+
7+
## Sandbox
8+
9+
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:
10+
11+
| **Request format** | | |
12+
| ------------------ | ---------------------------- | ------------------------------ |
13+
| Method | `POST` | |
14+
| URL | `https://cloud-api.livekit.io/api/sandbox/connection-details` |
15+
| Query Parameters | `roomName` (optional) | (your room name) |
16+
| | `participantName` (optional) | (your participant name) |
17+
| Headers | `X-Sandbox-ID` | (your Sandbox ID) |
18+
19+
Generated tokens will have a default TTL of 15 minutes. Optional parameters not included in the request will be generated by the token server.
20+
21+
## Installation
22+
23+
To use this template locally, clone the repo or use the LiveKit CLI:
24+
25+
```console
26+
lk app create --template token-server-node my-server
27+
```
28+
29+
You can also bootstrap a minimal token server yourself with the following code:
30+
31+
```ts
32+
import express from 'express';
33+
import { AccessToken } from 'livekit-server-sdk';
34+
35+
async function createToken({ roomName, participantName }) {
36+
const at = new AccessToken('my-key', 'my-secret', {
37+
identity: participantName,
38+
ttl: '10m',
39+
});
40+
at.addGrant({ roomJoin: true, room: roomName });
41+
return await at.toJwt();
42+
}
43+
44+
const app = express();
45+
const port = 3000;
46+
47+
app.post('/token', async (req, res) => {
48+
const { roomName = 'demo-room', participantName = 'demo-user' } = req.body || {};
49+
const token = await createToken({ roomName, participantName });
50+
res.send(token);
51+
});
52+
53+
app.listen(port, () => {
54+
console.log(\`Server listening on port ${port}`);
55+
});
56+
```
57+
58+
For more information on tokens, see our documentation on [generating tokens](https://docs.livekit.io/home/server/generating-tokens/).
59+

0 commit comments

Comments
 (0)