Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ async def main():
asyncio.run(main())
```

### Authentication

Authenticate with an API key and secret (recommended for backend use), or with a
pre-signed token for client-side use, where the API secret must not be exposed.
Any omitted value falls back to `LIVEKIT_URL`, `LIVEKIT_API_KEY`,
`LIVEKIT_API_SECRET`, and `LIVEKIT_TOKEN`.

```python
# API key & secret (backend)
lkapi = api.LiveKitAPI("https://my-project.livekit.cloud", api_key="...", api_secret="...")

# pre-signed token (client-side); the token must already carry the grants for the calls
lkapi = api.LiveKitAPI.with_token(my_token, "https://my-project.livekit.cloud")
```

### Using other APIs

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

### Error handling

A failed server API call raises `api.ServerError`, which exposes the error
`code`, `message`, and any server-provided `metadata`.

```python
try:
await lkapi.room.create_room(api.CreateRoomRequest(name="my-room"))
except api.ServerError as e:
print(e.code, e.message)
```

A failed SIP dial (e.g. the callee is busy or doesn't answer) raises
`api.SipCallError`, a `ServerError` subclass that also exposes the SIP response
status:

```python
try:
await lkapi.sip.create_sip_participant(api.CreateSIPParticipantRequest(
sip_trunk_id="ST_...",
sip_call_to="+15105550100",
room_name="my-room",
wait_until_answered=True,
))
except api.SipCallError as e:
print(e) # e.g. "SIP call failed: 486 Busy Here (resource_exhausted)"
if e.sip_status_code == 486:
... # busy
except api.ServerError as e:
print(e.code, e.message) # any other API error
```

## Using Real-time SDK

```shell
Expand Down
23 changes: 23 additions & 0 deletions livekit-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,26 @@ Access LiveKit server APIs and generate access tokens.

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

## Authentication

Every request to the server APIs is authenticated. `LiveKitAPI` supports two modes:

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

```python
from livekit import api

# API key & secret: set LIVEKIT_URL, LIVEKIT_API_KEY, and LIVEKIT_API_SECRET,
# then construct with no arguments...
lkapi = api.LiveKitAPI()

# ...or pass any of them explicitly to override the corresponding env var:
lkapi = api.LiveKitAPI("https://my.livekit.host", api_key="api-key", api_secret="api-secret")

# Pre-signed access token (client-side): with LIVEKIT_URL set, pass just the token:
lkapi = api.LiveKitAPI.with_token("a-pre-signed-token")
```

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.

Loading