Skip to content

Commit 0dbe1c6

Browse files
authored
readme updates (#696)
* readme updates * updates
1 parent 5c0721a commit 0dbe1c6

1 file changed

Lines changed: 82 additions & 15 deletions

File tree

packages/livekit-server-sdk/README.md

Lines changed: 82 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ You may store credentials in environment variables. If api-key or api-secret is
4242
- `LIVEKIT_API_KEY`
4343
- `LIVEKIT_API_SECRET`
4444

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+
4547
### Creating Access Tokens
4648

4749
Creating a token for participant to join a room.
@@ -86,36 +88,101 @@ at.addGrant({
8688

8789
This will allow the participant to subscribe to tracks, but not publish their own to the room.
8890

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 = new LiveKitAPI();
104+
105+
// ...or pass any of them explicitly to override the corresponding env var:
106+
const api = new LiveKitAPI({ 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 = new LiveKitAPI({ token });
111+
```
112+
89113
### Managing Rooms
90114

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.
92118

93119
```typescript
94-
import { Room, RoomServiceClient } from 'livekit-server-sdk';
120+
import { LiveKitAPI } from 'livekit-server-sdk';
95121

96-
const livekitHost = 'https://my.livekit.host';
97-
const svc = new RoomServiceClient(livekitHost, 'api-key', 'secret-key');
122+
// authenticate with an API key and secret, or `{ token }` for a pre-signed token
123+
const api = new LiveKitAPI({
124+
host: 'https://my.livekit.host',
125+
apiKey: 'api-key',
126+
secret: 'secret-key',
127+
});
98128

99129
// list rooms
100-
svc.listRooms().then((rooms: Room[]) => {
101-
console.log('existing rooms', rooms);
102-
});
130+
const rooms = await api.room.listRooms();
131+
console.log('existing rooms', rooms);
103132

104133
// create a new room
105-
const opts = {
134+
const room = await api.room.createRoom({
106135
name: 'myroom',
107-
// timeout in seconds
108-
emptyTimeout: 10 * 60,
136+
emptyTimeout: 10 * 60, // timeout in seconds
109137
maxParticipants: 20,
110-
};
111-
svc.createRoom(opts).then((room: Room) => {
112-
console.log('room created', room);
113138
});
139+
console.log('room created', room);
114140

115141
// delete a room
116-
svc.deleteRoom('myroom').then(() => {
117-
console.log('room deleted');
142+
await api.room.deleteRoom('myroom');
143+
144+
// other services are reached the same way, e.g. api.egress, api.sip
145+
await api.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.
151+
152+
```typescript
153+
// dispatch an agent into a room
154+
const dispatch = await api.agentDispatch.createDispatch('myroom', 'my-agent', {
155+
metadata: '{}',
118156
});
157+
158+
// list dispatches in a room
159+
const dispatches = await api.agentDispatch.listDispatch('myroom');
160+
161+
// delete a dispatch
162+
await api.agentDispatch.deleteDispatch(dispatch.id, 'myroom');
163+
```
164+
165+
### Error handling
166+
167+
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:
168+
169+
```typescript
170+
import { ServerError, SipCallError } from 'livekit-server-sdk';
171+
172+
try {
173+
await api.sip.createSipParticipant('trunk-id', '+15105550100', 'my-room', {
174+
waitUntilAnswered: true,
175+
});
176+
} catch (e) {
177+
if (e instanceof SipCallError) {
178+
console.log(e.message); // e.g. "SIP call failed: 486 Busy Here (resource_exhausted)"
179+
if (e.sipStatusCode === 486) {
180+
// callee is busy
181+
}
182+
} else if (e instanceof ServerError) {
183+
console.log(e.code, e.message); // any other API error
184+
}
185+
}
119186
```
120187

121188
## Webhooks

0 commit comments

Comments
 (0)