Skip to content

Improved token handling#219

Merged
MaxHeimbrock merged 19 commits intomainfrom
max/improved-token-handling
Apr 13, 2026
Merged

Improved token handling#219
MaxHeimbrock merged 19 commits intomainfrom
max/improved-token-handling

Conversation

@MaxHeimbrock
Copy link
Copy Markdown
Contributor

@MaxHeimbrock MaxHeimbrock commented Apr 8, 2026

Background:

Token usage was a pain for first time developers of LiveKit. Especially Unity devs are not always good at web technologies. I implemented a version of the TokenSource concept we have in other SDKs as well to lower the entry barrier for new devs.

Changes:

Reuses TokenService.cs and utils from our Agent Example (thanks @ladvoc) and adopt it into TokenSource.cs.

You can create new config assets, one empty one is in the Meet project already.

How to test:

Read the README.md paragraph about tokens and see if this makes sense.

Use the LiveKit Meet sample scene. Put an auth config instance into the TokenService:
Screenshot 2026-04-10 at 14 44 48

This is how the UI of the assets look like:
Screenshot 2026-04-10 at 14 45 34
Screenshot 2026-04-10 at 14 45 27
Screenshot 2026-04-10 at 14 45 19

New README Paragraph:

Tokens

You need a token to join a LiveKit room as a participant. Read more about tokens here: https://docs.livekit.io/frontends/reference/tokens-grants/

To help getting started with tokens, use TokenSource.cs with a TokenSourceConfig ScriptableObject (see https://docs.livekit.io/frontends/build/authentication/#tokensource). Create a config asset via Right Click > Create > LiveKit > Token Source Config and select one of three token source types:

1. Literal

Use this to pass a pregenerated server URL and token. Generate tokens via the LiveKit CLI or from your LiveKit Cloud project's API key page.

2. Sandbox

For development and testing. Follow the sandbox token server guide to enable your project's sandbox and get the sandbox ID. Optional connection fields (room name, participant name, agent name, etc.) can be configured in the inspector — leave blank for server defaults.

3. Endpoint

For production. Point to your own token endpoint URL and add any required authentication headers. Uses the same connection options as Sandbox. See the endpoint token generation guide.

Usage

Add a TokenSource component to a GameObject, assign your TokenSourceConfig asset, then fetch connection details before connecting:

var connectionDetailsTask = _tokenSource.FetchConnectionDetails();
yield return new WaitUntil(() => connectionDetailsTask.IsCompleted);

if (connectionDetailsTask.IsFaulted)
{
    Debug.LogError($"Failed to fetch connection details: {connectionDetailsTask.Exception?.InnerException?.Message}");
    yield break;
}

var details = connectionDetailsTask.Result;
_room = new Room();
var connect = _room.Connect(details.ServerUrl, details.ParticipantToken, new RoomOptions());

@MaxHeimbrock MaxHeimbrock force-pushed the max/improved-token-handling branch from 92ba1e9 to 9f0332b Compare April 8, 2026 13:20
Comment thread Runtime/Scripts/TokenService/TokenService.cs Outdated
Copy link
Copy Markdown

@1egoman 1egoman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are definitely fairly uninformed comments given my lack of unity expertise, but hopefully at least somewhat helpful!

Comment thread Runtime/Scripts/TokenSource/TokenSource.cs Outdated
Comment thread Runtime/Scripts/TokenSource/TokenSource.cs Outdated
Comment thread Runtime/Scripts/TokenSource/TokenSource.cs
Copy link
Copy Markdown

@1egoman 1egoman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me at a high level! If you want a deeper review of your c# code it might be worth waiting for a review from the other reviewers.

@MaxHeimbrock MaxHeimbrock merged commit 9dd1db4 into main Apr 13, 2026
14 checks passed
@MaxHeimbrock MaxHeimbrock deleted the max/improved-token-handling branch April 13, 2026 07:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants