clockworklabs/SpacetimeDB · error · Exception
Token not initialized. Call AuthToken.Init() first.
Error message
Token not initialized. Call AuthToken.Init() first.
What it means
Client-SDK side (non-Unity, non-Godot build) of AuthToken: Init(configFolder, configFile, configRoot) sets the private settingsPath and loads a previously saved token; the Token getter throws a plain Exception when settingsPath is still null, i.e. Token was read before AuthToken.Init() ran. The intended flow (see the file's header example) is Init() once at startup, then use Token when connecting and SaveToken in onIdentityReceived.
Source
Thrown at sdks/csharp/src/AuthToken.cs:136
{
token =
File.ReadLines(settingsPath)
.FirstOrDefault(line => line.StartsWith(PREFIX))
?[PREFIX.Length..];
}
}
/// <summary>
/// This is the auth token that was saved to local storage. Null if not never saved.
/// When you specify null to the SpacetimeDBClient, SpacetimeDB will generate a new identity for you.
/// </summary>
public static string? Token
{
get
{
if (settingsPath == null)
{
throw new Exception("Token not initialized. Call AuthToken.Init() first.");
}
return token;
}
}
/// <summary>
/// Save the auth token to local storage.
/// SpacetimeDBClient provides this token to you in the onIdentityReceived callback.
/// </summary>
public static void SaveToken(string token)
{
if (settingsPath == null)
{
throw new Exception("Token not initialized. Call AuthToken.Init() first.");
}
Directory.CreateDirectory(Path.GetDirectoryName(settingsPath)!);
var newAuthLine = PREFIX + token;
var lines = File.Exists(settingsPath) ? File.ReadAllLines(settingsPath).ToList() : new();View on GitHub (pinned to 9e0d92412f)
Solutions
- Call AuthToken.Init(".my_app_name") once at process start, before any Token/SaveToken use
- Centralize SDK startup (Init + Connect) in one bootstrap method invoked from every entry point
- If you don't want persistence, skip AuthToken entirely and pass null to Connect so the server mints a fresh identity
Example fix
// before
SpacetimeDBClient.instance.Connect(AuthToken.Token, "localhost:3000", "basicchat", false); // throws
// after
AuthToken.Init(".my_app_name");
SpacetimeDBClient.instance.Connect(AuthToken.Token, "localhost:3000", "basicchat", false); Defensive patterns
Strategy: validation
Validate before calling
// Single bootstrap for every entry point:
static void InitSdk()
{
AuthToken.Init(".my_app_name"); // must precede any AuthToken.Token read
} Try / catch
string? token;
try { token = AuthToken.Token; }
catch (Exception) { token = null; } // not initialized: connect anonymous, server mints a new identity
SpacetimeDBClient.instance.Connect(token, host, db, false); Prevention
- Call AuthToken.Init() first thing in Main/Startup, before Connect or event subscriptions
- Centralize SDK initialization in one method used by app, CLI and tests
- If you don't need persistence, skip AuthToken and pass null to Connect
When it happens
Trigger: Calling SpacetimeDBClient.instance.Connect(AuthToken.Token, ...) before any AuthToken.Init(...); reading AuthToken.Token in a field initializer or constructor that runs before startup code; process restarted and Init call removed during refactor.
Common situations: New desktop/console projects wired up following docs that omit Init; reordering startup so Connect precedes initialization; multiple entry points (CLI args, tests) that skip the Init path; upgrading the SDK from a version where Token did not require Init.
Related errors
- Invalid row type for table {RemoteTableName}: {value.GetType
- Invalid row type for table {RemoteTableName}: {oldValue.GetT
- Invalid row type for table {RemoteTableName}: {newValue.GetT
- While table `{RemoteTableName}` was applying: {deltaString}
- The selected C# build path requires the .NET {desired_sdk_ma
AI-assisted analysis of clockworklabs/SpacetimeDB@9e0d92412f (2026-08-30).
Data as JSON: /api/errors/59ff883db1f67de4.
Report an issue: GitHub.