{"record":{"id":"59ff883db1f67de4","repo":"clockworklabs/SpacetimeDB","slug":"token-not-initialized-call-authtoken-init-first","errorCode":null,"errorMessage":"Token not initialized. Call AuthToken.Init() first.","messagePattern":"Token not initialized\\. Call AuthToken\\.Init\\(\\) first\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"sdks/csharp/src/AuthToken.cs","lineNumber":136,"sourceCode":"            {\n                token =\n                    File.ReadLines(settingsPath)\n                    .FirstOrDefault(line => line.StartsWith(PREFIX))\n                    ?[PREFIX.Length..];\n            }\n        }\n\n        /// <summary>\n        /// This is the auth token that was saved to local storage. Null if not never saved.\n        /// When you specify null to the SpacetimeDBClient, SpacetimeDB will generate a new identity for you.\n        /// </summary>\n        public static string? Token\n        {\n            get\n            {\n                if (settingsPath == null)\n                {\n                    throw new Exception(\"Token not initialized. Call AuthToken.Init() first.\");\n                }\n                return token;\n            }\n        }\n\n        /// <summary>\n        /// Save the auth token to local storage.\n        /// SpacetimeDBClient provides this token to you in the onIdentityReceived callback.\n        /// </summary>\n        public static void SaveToken(string token)\n        {\n            if (settingsPath == null)\n            {\n                throw new Exception(\"Token not initialized. Call AuthToken.Init() first.\");\n            }\n            Directory.CreateDirectory(Path.GetDirectoryName(settingsPath)!);\n            var newAuthLine = PREFIX + token;\n            var lines = File.Exists(settingsPath) ? File.ReadAllLines(settingsPath).ToList() : new();","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/9e0d92412ff2248f401a8ad12d535f2b5ac30912/sdks/csharp/src/AuthToken.cs#L118-L154","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nSpacetimeDBClient.instance.Connect(AuthToken.Token, \"localhost:3000\", \"basicchat\", false); // throws\n\n// after\nAuthToken.Init(\".my_app_name\");\nSpacetimeDBClient.instance.Connect(AuthToken.Token, \"localhost:3000\", \"basicchat\", false);","handlingStrategy":"validation","validationCode":"// Single bootstrap for every entry point:\nstatic void InitSdk()\n{\n    AuthToken.Init(\".my_app_name\"); // must precede any AuthToken.Token read\n}","typeGuard":null,"tryCatchPattern":"string? token;\ntry { token = AuthToken.Token; }\ncatch (Exception) { token = null; } // not initialized: connect anonymous, server mints a new identity\nSpacetimeDBClient.instance.Connect(token, host, db, false);","preventionTips":["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"],"tags":["csharp","sdk","auth-token","initialization","client"],"backgroundTag":"sdk-not-initialized","analyzedSha":"9e0d92412ff2248f401a8ad12d535f2b5ac30912","analyzedAt":"2026-08-30T19:52:08.972Z","contentChangedAt":"2026-08-30T19:52:08.972Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}