Skip to content
Draft
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
10 changes: 5 additions & 5 deletions token_source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ get_filename_component(_lk_cmake_dir "${LiveKit_DIR}" DIRECTORY) # .../lib/cmak
get_filename_component(_lk_lib_dir "${_lk_cmake_dir}" DIRECTORY) # .../lib

set(_token_source_examples
token_source_literal token_source_literal.cpp
token_source_endpoint token_source_endpoint.cpp
token_source_sandbox token_source_sandbox.cpp
token_source_custom token_source_custom.cpp
token_source_caching token_source_caching.cpp
token_source_literal token_source_literal.cpp
token_source_endpoint token_source_endpoint.cpp
token_source_development token_source_development.cpp
token_source_custom token_source_custom.cpp
token_source_caching token_source_caching.cpp
)

list(LENGTH _token_source_examples _count)
Expand Down
25 changes: 12 additions & 13 deletions token_source/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ disconnects.
| --- | --- | --- |
| `LiteralTokenSource` | `token_source_literal.cpp` | You already have a URL + JWT (minted out of band, e.g. `lk token create`). The SDK consumes them as-is. |
| `EndpointTokenSource` | `token_source_endpoint.cpp` | Recommended for production. The SDK POSTs request options to your backend token endpoint, which returns the URL + a fresh JWT. API keys stay server-side. |
| `SandboxTokenSource` | `token_source_sandbox.cpp` | Local development only. Uses LiveKit Cloud's sandbox token server. **Not for production.** |
| `DevelopmentTokenSource` | `token_source_development.cpp` | Local development only. Uses LiveKit Cloud's development token server. **Not for production.** |
| `CustomTokenSource` | `token_source_custom.cpp` | You have an internal auth/token system. Plug in your own async callback that returns credentials. |
| `CachingTokenSource` | `token_source_caching.cpp` | A decorator that adds JWT-aware caching around any configurable source (endpoint/sandbox/custom) to cut down on fetch calls. |
| `CachingTokenSource` | `token_source_caching.cpp` | A decorator that adds JWT-aware caching around any configurable source (endpoint/development/custom) to cut down on fetch calls. |

`LiteralTokenSource` is *fixed* (no per-call options); the others are
*configurable* and accept
Expand All @@ -29,7 +29,7 @@ disconnects.

## Configuring the Examples

All inputs come from environment variables, so no secrets or sandbox IDs are committed to source.
All inputs come from environment variables, so no secrets or token server IDs are committed to source.

| Variable | Used by | Notes |
| --- | --- | --- |
Expand All @@ -38,9 +38,9 @@ All inputs come from environment variables, so no secrets or sandbox IDs are com
| `LIVEKIT_TOKEN_ENDPOINT` | endpoint, caching | Token endpoint URL. Default `http://127.0.0.1:3000/createToken`. |
| `LIVEKIT_TOKEN_ENDPOINT_METHOD` | endpoint, caching | Optional HTTP method (default `POST`). |
| `LIVEKIT_TOKEN_ENDPOINT_HEADERS` | endpoint, caching | Optional newline-separated `Name: Value` headers. |
| `LIVEKIT_SANDBOX_ID` | sandbox | Required. Sandbox ID from LiveKit Cloud. |
| `LIVEKIT_AGENT_NAME` | sandbox | Optional agent to dispatch into the room. |
| `LIVEKIT_AGENT_METADATA` | sandbox | Optional metadata for the dispatched agent. |
| `LIVEKIT_TOKEN_SERVER_ID` | development | Required. Development token server ID from LiveKit Cloud. |
| `LIVEKIT_AGENT_NAME` | development | Optional agent to dispatch into the room. |
| `LIVEKIT_AGENT_METADATA` | development | Optional metadata for the dispatched agent. |

These examples require LiveKit C++ SDK **v1.3.0** or newer. Build them with the
rest of the repo — see the root [README](../README.md#building-the-examples).
Expand Down Expand Up @@ -78,14 +78,13 @@ cd <path-to>/token-server-node
LIVEKIT_URL=ws://localhost:7880 LIVEKIT_API_KEY=devkey LIVEKIT_API_SECRET=secret PORT=3000 pnpm start
```

For sandbox, create a **Token Server** sandbox in [LiveKit Cloud](https://cloud.livekit.io)
(Sandbox → create from the token-server template), then copy the sandbox ID
For development, enable the **Development Token Server** in [LiveKit Cloud](https://cloud.livekit.io), then copy the token server ID
(`token-server-xxxxxx`). See the
[sandbox token server docs](https://docs.livekit.io/frontends/build/authentication/sandbox-token-server/)
[development token server docs](https://docs.livekit.io/frontends/build/authentication/sandbox-token-server/)
for setup details. Do not use this in production.

```bash
export LIVEKIT_SANDBOX_ID=token-server-xxxxxx
export LIVEKIT_TOKEN_SERVER_ID=token-server-xxxxxx
# optional: dispatch a registered agent into the room with metadata
# export LIVEKIT_AGENT_NAME=my-agent
# export LIVEKIT_AGENT_METADATA='{"greeting": "hello from cpp"}'
Expand All @@ -110,11 +109,11 @@ export LIVEKIT_TOKEN_ENDPOINT=http://127.0.0.1:3000/createToken
```

```bash
# Sandbox: development-only, ID from the environment
export LIVEKIT_SANDBOX_ID=<your-sandbox-id>
# Development: development-only, ID from the environment
export LIVEKIT_TOKEN_SERVER_ID=<your-token-server-id>
export LIVEKIT_AGENT_NAME=<your-agent-name> # optional
export LIVEKIT_AGENT_METADATA=<your-agent-metadata> # optional
./build/token_source/token_source_sandbox
./build/token_source/token_source_development
```

```bash
Expand Down
2 changes: 1 addition & 1 deletion token_source/token_source_caching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

// Caching token source: a decorator that adds JWT-aware caching to any
// configurable token source (endpoint, sandbox, or custom).
// configurable token source (endpoint, development, or custom).
//
// Repeated fetches for the same request options reuse the cached token until it
// nears expiry or you call invalidate() to drop the cached credentials, cutting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
* limitations under the License.
*/

// Sandbox token source: use LiveKit Cloud's sandbox token server for quick
// Development token source: use LiveKit Cloud's development token server for quick
// local development. Do not use in production.
//
// The sandbox ID is read from the environment (never hard-coded) so this
// example does not link a personal sandbox into source control.
// The token server ID is read from the environment (never hard-coded) so this
// example does not link a personal token server ID into source control.
//
// Environment:
// LIVEKIT_SANDBOX_ID Sandbox identifier from LiveKit Cloud (required)
// LIVEKIT_TOKEN_SERVER_ID Token server identifier from LiveKit Cloud (required)
// LIVEKIT_AGENT_NAME Optional registered agent to dispatch into the room
// LIVEKIT_AGENT_METADATA Optional metadata passed to the dispatched agent

Expand All @@ -37,15 +37,15 @@ namespace {

using namespace token_source_example;

bool sandboxTokenSourceConnect() {
std::string sandbox_id;
if (!requireEnv("LIVEKIT_SANDBOX_ID", sandbox_id)) {
bool DevelopmentTokenSourceConnect() {
std::string token_server_id;
if (!requireEnv("LIVEKIT_TOKEN_SERVER_ID", token_server_id)) {
return false;
}

// POSTs to cloud-api.livekit.io/api/v2/sandbox/connection-details with
// X-Sandbox-ID set from LIVEKIT_SANDBOX_ID.
auto token_source = livekit::SandboxTokenSource::create(sandbox_id);
// X-Sandbox-ID set from LIVEKIT_TOKEN_SERVER_ID.
auto token_source = livekit::DevelopmentTokenSource::create(token_server_id);

livekit::TokenRequestOptions request_options;
request_options.participant_identity = "robot-a";
Expand All @@ -57,7 +57,7 @@ bool sandboxTokenSourceConnect() {
if (const std::string agent_metadata = getenvOrEmpty("LIVEKIT_AGENT_METADATA"); !agent_metadata.empty()) {
request_options.agent_metadata = agent_metadata;
}
std::cout << "Requesting sandbox token with agent dispatch: agent_name=" << *request_options.agent_name << "\n";
std::cout << "Requesting development token with agent dispatch: agent_name=" << *request_options.agent_name << "\n";
}
const auto credentials = token_source->fetch(request_options).get();
if (!credentials) {
Expand All @@ -72,7 +72,7 @@ bool sandboxTokenSourceConnect() {
std::cerr << "Failed to connect to room\n";
return false;
}
std::cout << "Connected to room: " << room.roomInfo().name << " (sandbox token source)\n";
std::cout << "Connected to room: " << room.roomInfo().name << " (development token source)\n";

return runConnectedSession(room);
}
Expand All @@ -81,7 +81,7 @@ bool sandboxTokenSourceConnect() {

int main() {
livekit::initialize(livekit::LogLevel::Info);
const bool ok = sandboxTokenSourceConnect();
const bool ok = DevelopmentTokenSourceConnect();
livekit::shutdown();
return ok ? 0 : 1;
}
Loading