Skip to content

Commit e966a3f

Browse files
authored
update readme to reflect LKAPI interfaces (livekit#748)
1 parent 8d979d6 commit e966a3f

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,21 @@ async def main():
6565
asyncio.run(main())
6666
```
6767

68+
### Authentication
69+
70+
Authenticate with an API key and secret (recommended for backend use), or with a
71+
pre-signed token for client-side use, where the API secret must not be exposed.
72+
Any omitted value falls back to `LIVEKIT_URL`, `LIVEKIT_API_KEY`,
73+
`LIVEKIT_API_SECRET`, and `LIVEKIT_TOKEN`.
74+
75+
```python
76+
# API key & secret (backend)
77+
lkapi = api.LiveKitAPI("https://my-project.livekit.cloud", api_key="...", api_secret="...")
78+
79+
# pre-signed token (client-side); the token must already carry the grants for the calls
80+
lkapi = api.LiveKitAPI.with_token(my_token, "https://my-project.livekit.cloud")
81+
```
82+
6883
### Using other APIs
6984

7085
Services can be accessed via the LiveKitAPI object.
@@ -91,6 +106,38 @@ dispatch_svc = lkapi.agent_dispatch
91106
connector_svc = lkapi.connector
92107
```
93108

109+
### Error handling
110+
111+
A failed server API call raises `api.ServerError`, which exposes the error
112+
`code`, `message`, and any server-provided `metadata`.
113+
114+
```python
115+
try:
116+
await lkapi.room.create_room(api.CreateRoomRequest(name="my-room"))
117+
except api.ServerError as e:
118+
print(e.code, e.message)
119+
```
120+
121+
A failed SIP dial (e.g. the callee is busy or doesn't answer) raises
122+
`api.SipCallError`, a `ServerError` subclass that also exposes the SIP response
123+
status:
124+
125+
```python
126+
try:
127+
await lkapi.sip.create_sip_participant(api.CreateSIPParticipantRequest(
128+
sip_trunk_id="ST_...",
129+
sip_call_to="+15105550100",
130+
room_name="my-room",
131+
wait_until_answered=True,
132+
))
133+
except api.SipCallError as e:
134+
print(e) # e.g. "SIP call failed: 486 Busy Here (resource_exhausted)"
135+
if e.sip_status_code == 486:
136+
... # busy
137+
except api.ServerError as e:
138+
print(e.code, e.message) # any other API error
139+
```
140+
94141
## Using Real-time SDK
95142

96143
```shell

livekit-api/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,26 @@ Access LiveKit server APIs and generate access tokens.
44

55
See https://docs.livekit.io/reference/server/server-apis for more information.
66

7+
## Authentication
8+
9+
Every request to the server APIs is authenticated. `LiveKitAPI` supports two modes:
10+
11+
- **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.
12+
- **Access token** — for client-side use where the API secret must not be exposed. Pass a pre-signed [access token](https://docs.livekit.io/home/get-started/authentication/) that already carries the grants for the operations you'll perform; the SDK sends it verbatim.
13+
14+
```python
15+
from livekit import api
16+
17+
# API key & secret: set LIVEKIT_URL, LIVEKIT_API_KEY, and LIVEKIT_API_SECRET,
18+
# then construct with no arguments...
19+
lkapi = api.LiveKitAPI()
20+
21+
# ...or pass any of them explicitly to override the corresponding env var:
22+
lkapi = api.LiveKitAPI("https://my.livekit.host", api_key="api-key", api_secret="api-secret")
23+
24+
# Pre-signed access token (client-side): with LIVEKIT_URL set, pass just the token:
25+
lkapi = api.LiveKitAPI.with_token("a-pre-signed-token")
26+
```
27+
28+
The url and credentials fall back to the `LIVEKIT_URL`, `LIVEKIT_API_KEY`, `LIVEKIT_API_SECRET`, and `LIVEKIT_TOKEN` environment variables. 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.
29+

0 commit comments

Comments
 (0)