|
| 1 | +# Token Source Examples |
| 2 | + |
| 3 | +These examples show the different ways to obtain the credentials |
| 4 | +(**WebSocket URL** + **participant JWT**) the SDK needs to join a room. Each |
| 5 | +example builds its own executable, constructs a single kind of |
| 6 | +[`TokenSource`](https://github.com/livekit/client-sdk-cpp/blob/main/include/livekit/token_source.h), |
| 7 | +connects to a room, logs participant join/leave for a few seconds, then |
| 8 | +disconnects. |
| 9 | + |
| 10 | +> Token sources are for the **initial connection only**. Once connected, the |
| 11 | +> LiveKit server refreshes the session token internally — your token source is |
| 12 | +> not called again unless you connect again. See the SDK's |
| 13 | +> [authentication docs](https://github.com/livekit/client-sdk-cpp/blob/main/docs/authentication.md). |
| 14 | +
|
| 15 | +## Types |
| 16 | + |
| 17 | +| Type | Example | When to use | |
| 18 | +| --- | --- | --- | |
| 19 | +| `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. | |
| 20 | +| `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. | |
| 21 | +| `SandboxTokenSource` | `token_source_sandbox.cpp` | Local development only. Uses LiveKit Cloud's sandbox token server. **Not for production.** | |
| 22 | +| `CustomTokenSource` | `token_source_custom.cpp` | You have an internal auth/token system. Plug in your own async callback that returns credentials. | |
| 23 | +| `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. | |
| 24 | + |
| 25 | +`LiteralTokenSource` is *fixed* (no per-call options); the others are |
| 26 | +*configurable* and accept |
| 27 | +[`TokenRequestOptions`](https://github.com/livekit/client-sdk-cpp/blob/main/include/livekit/token_source.h) |
| 28 | +(room name, participant identity, agent dispatch, ...). |
| 29 | + |
| 30 | +## Configuring the Examples |
| 31 | + |
| 32 | +All inputs come from environment variables, so no secrets or sandbox IDs are committed to source. |
| 33 | + |
| 34 | +| Variable | Used by | Notes | |
| 35 | +| --- | --- | --- | |
| 36 | +| `LIVEKIT_URL` | literal, custom | WebSocket URL, e.g. `ws://localhost:7880`. | |
| 37 | +| `LIVEKIT_TOKEN` | literal, custom | Participant JWT. | |
| 38 | +| `LIVEKIT_TOKEN_ENDPOINT` | endpoint, caching | Token endpoint URL. Default `http://127.0.0.1:3000/createToken`. | |
| 39 | +| `LIVEKIT_TOKEN_ENDPOINT_METHOD` | endpoint, caching | Optional HTTP method (default `POST`). | |
| 40 | +| `LIVEKIT_TOKEN_ENDPOINT_HEADERS` | endpoint, caching | Optional newline-separated `Name: Value` headers. | |
| 41 | +| `LIVEKIT_SANDBOX_ID` | sandbox | Required. Sandbox ID from LiveKit Cloud. | |
| 42 | +| `LIVEKIT_AGENT_NAME` | sandbox | Optional agent to dispatch into the room. | |
| 43 | +| `LIVEKIT_AGENT_METADATA` | sandbox | Optional metadata for the dispatched agent. | |
| 44 | + |
| 45 | +These examples require LiveKit C++ SDK **v1.3.0** or newer. Build them with the |
| 46 | +rest of the repo — see the root [README](../README.md#building-the-examples). |
| 47 | + |
| 48 | +## Running the Examples |
| 49 | + |
| 50 | +### Prerequisites |
| 51 | + |
| 52 | +#### LiveKit Server |
| 53 | + |
| 54 | +Start a development server via: |
| 55 | + |
| 56 | +```bash |
| 57 | +livekit-server --dev |
| 58 | +``` |
| 59 | + |
| 60 | +#### Token Sources |
| 61 | + |
| 62 | +For literal and custom, generate a development token with the |
| 63 | +[LiveKit CLI](https://docs.livekit.io/home/cli/cli-setup/) (a dev server started |
| 64 | +with `livekit-server --dev` uses `devkey` / `secret`): |
| 65 | + |
| 66 | +```bash |
| 67 | +export LIVEKIT_TOKEN=$(lk token create \ |
| 68 | + --api-key devkey --api-secret secret \ |
| 69 | + -i my-participant --join --room my-room \ |
| 70 | + --valid-for 24h --token-only) |
| 71 | +``` |
| 72 | + |
| 73 | +For the endpoint and caching examples, run a local token server such as |
| 74 | +[token-server-node](https://github.com/livekit-examples/token-server-node). This can be run via: |
| 75 | + |
| 76 | +```bash |
| 77 | +cd <path-to>/token-server-node |
| 78 | +LIVEKIT_URL=ws://localhost:7880 LIVEKIT_API_KEY=devkey LIVEKIT_API_SECRET=secret PORT=3000 pnpm start |
| 79 | +``` |
| 80 | + |
| 81 | +For sandbox, create a **Token Server** sandbox in [LiveKit Cloud](https://cloud.livekit.io) |
| 82 | +(Sandbox → create from the token-server template), then copy the sandbox ID |
| 83 | +(`token-server-xxxxxx`). See the |
| 84 | +[sandbox token server docs](https://docs.livekit.io/frontends/build/authentication/sandbox-token-server/) |
| 85 | +for setup details. Do not use this in production. |
| 86 | + |
| 87 | +```bash |
| 88 | +export LIVEKIT_SANDBOX_ID=token-server-xxxxxx |
| 89 | +# optional: dispatch a registered agent into the room with metadata |
| 90 | +# export LIVEKIT_AGENT_NAME=my-agent |
| 91 | +# export LIVEKIT_AGENT_METADATA='{"greeting": "hello from cpp"}' |
| 92 | +``` |
| 93 | + |
| 94 | +### Executing |
| 95 | + |
| 96 | +The built binaries live under `build/token_source/`: |
| 97 | + |
| 98 | +```bash |
| 99 | +# Literal: bring your own URL + token |
| 100 | +export LIVEKIT_URL=ws://localhost:7880 |
| 101 | +export LIVEKIT_TOKEN=<participant-jwt> |
| 102 | +./build/token_source/token_source_literal |
| 103 | +``` |
| 104 | + |
| 105 | +```bash |
| 106 | +# Endpoint / caching: point at your token endpoint |
| 107 | +export LIVEKIT_TOKEN_ENDPOINT=http://127.0.0.1:3000/createToken |
| 108 | +./build/token_source/token_source_endpoint |
| 109 | +./build/token_source/token_source_caching |
| 110 | +``` |
| 111 | + |
| 112 | +```bash |
| 113 | +# Sandbox: development-only, ID from the environment |
| 114 | +export LIVEKIT_SANDBOX_ID=<your-sandbox-id> |
| 115 | +export LIVEKIT_AGENT_NAME=<your-agent-name> # optional |
| 116 | +export LIVEKIT_AGENT_METADATA=<your-agent-metadata> # optional |
| 117 | +./build/token_source/token_source_sandbox |
| 118 | +``` |
| 119 | + |
| 120 | +```bash |
| 121 | +# Custom: callback returns credentials (this example reads the env) |
| 122 | +export LIVEKIT_URL=ws://localhost:7880 |
| 123 | +export LIVEKIT_TOKEN=<participant-jwt> |
| 124 | +./build/token_source/token_source_custom |
| 125 | +``` |
0 commit comments