You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/livekit-server-sdk/README.md
+82-15Lines changed: 82 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,6 +42,8 @@ You may store credentials in environment variables. If api-key or api-secret is
42
42
-`LIVEKIT_API_KEY`
43
43
-`LIVEKIT_API_SECRET`
44
44
45
+
`LiveKitAPI` additionally falls back to `LIVEKIT_URL` for the host and `LIVEKIT_TOKEN` for a pre-signed token. 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.
46
+
45
47
### Creating Access Tokens
46
48
47
49
Creating a token for participant to join a room.
@@ -86,36 +88,101 @@ at.addGrant({
86
88
87
89
This will allow the participant to subscribe to tracks, but not publish their own to the room.
88
90
91
+
### Authentication
92
+
93
+
Every request to the server APIs is authenticated. `LiveKitAPI` (and each service client) supports two modes:
94
+
95
+
-**API key & secret** — recommended for backend use. The SDK signs a short-lived token per request from your key and secret. Keep your API secret on the server; never ship it to a client.
96
+
-**Access token** — for frontend / client-side use, where the API secret must not be exposed. Pass a pre-signed [access token](https://docs.livekit.io/frontends/reference/tokens-grants/) that already carries the grants for the operations you'll perform; the SDK sends it verbatim. Mint it on your backend and hand it to the client.
97
+
98
+
```typescript
99
+
import { LiveKitAPI } from'livekit-server-sdk';
100
+
101
+
// Backend (API key & secret): set LIVEKIT_URL, LIVEKIT_API_KEY, and
102
+
// LIVEKIT_API_SECRET as env vars, then construct with no arguments:
103
+
const api =newLiveKitAPI();
104
+
105
+
// ...or pass any of them explicitly to override the corresponding env var:
106
+
const api =newLiveKitAPI({ host: 'https://my.livekit.host', apiKey: 'api-key', secret: 'secret-key' });
107
+
108
+
// Frontend (pre-signed access token): with LIVEKIT_URL set, pass just the token
109
+
// (or override the host too). Its grants must cover the calls you make.
110
+
const api =newLiveKitAPI({ token });
111
+
```
112
+
89
113
### Managing Rooms
90
114
91
-
`RoomServiceClient` gives you APIs to list, create, and delete rooms. It also requires a pair of api key/secret key to operate.
115
+
`LiveKitAPI` is a single entry point to every server API, exposing each service as a property: `room`, `egress`, `ingress`, `sip`, `agentDispatch`, and `connector`. Construct it with your credentials (see [Authentication](#authentication)).
116
+
117
+
`RoomServiceClient`, reached via `api.room`, gives you APIs to list, create, and delete rooms and to moderate their participants.
// authenticate with an API key and secret, or `{ token }` for a pre-signed token
123
+
const api =newLiveKitAPI({
124
+
host: 'https://my.livekit.host',
125
+
apiKey: 'api-key',
126
+
secret: 'secret-key',
127
+
});
98
128
99
129
// list rooms
100
-
svc.listRooms().then((rooms:Room[]) => {
101
-
console.log('existing rooms', rooms);
102
-
});
130
+
const rooms =awaitapi.room.listRooms();
131
+
console.log('existing rooms', rooms);
103
132
104
133
// create a new room
105
-
constopts= {
134
+
constroom=awaitapi.room.createRoom({
106
135
name: 'myroom',
107
-
// timeout in seconds
108
-
emptyTimeout: 10*60,
136
+
emptyTimeout: 10*60, // timeout in seconds
109
137
maxParticipants: 20,
110
-
};
111
-
svc.createRoom(opts).then((room:Room) => {
112
-
console.log('room created', room);
113
138
});
139
+
console.log('room created', room);
114
140
115
141
// delete a room
116
-
svc.deleteRoom('myroom').then(() => {
117
-
console.log('room deleted');
142
+
awaitapi.room.deleteRoom('myroom');
143
+
144
+
// other services are reached the same way, e.g. api.egress, api.sip
145
+
awaitapi.egress.listEgress({});
146
+
```
147
+
148
+
### Agent dispatch
149
+
150
+
[Agent dispatch](https://docs.livekit.io/agents/server/agent-dispatch/) assigns an agent to a room. Explicit dispatch, via `api.agentDispatch`, gives you full control over when and how agents join and lets you pass job-specific metadata. The target agent is selected by its `agentName`, and the room is created if it doesn't exist. The example below reuses the `api` from above.
A failed server API call throws a `ServerError`, which carries the error `code`, `message`, and any server-provided `metadata`. SIP dialing calls throw a `SipCallError` (a `ServerError` subclass) that also exposes the SIP response status:
0 commit comments