Default behavior
The client usesMemoryStorage by default. Tokens live in memory and are lost when the page reloads or the process exits. This works for quick prototypes and single-session CLI tools, but most production applications need persistent storage.
KontextStorage interface
Any object that implementsgetJson and setJson works as a storage backend:
undefined as the value to delete a key.
localStorage adapter
For browser applications where tokens should survive page reloads:sessionStorage adapter
Same pattern, scoped to the browser tab. Tokens are cleared when the tab closes:Database-backed storage
For server-side applications that manage multiple users, store tokens in a database. This example uses a generic key-value table:Session keys
ThesessionKey config option namespaces all storage keys. Use it when multiple users share the same storage backend but you want to keep their tokens separate.
sessionKey set to "user-123", storage keys look like kontext:your-client-id:user-123:tokens. Without an explicit sessionKey, the SDK defaults to "default", producing keys like kontext:your-client-id:default:tokens.
In hybrid mode (the default when no url is provided), the SDK appends a suffix per connection:
- Gateway:
kontext:your-client-id:default:gateway:tokens - Internal integrations:
kontext:your-client-id:default:internal:<integrationId>:tokens
url is set), the base format above is used as-is.
This prevents token collisions when switching between users on the same device.
When to use custom storage
- Browser SPA: Use
localStorageso users stay signed in across page reloads. - Browser with multiple accounts: Use
localStorage+sessionKeyto isolate per user. - Server-side with multiple users: Use database storage with a user-scoped factory.
- CLI tool: Default
MemoryStorageis fine for single-session use. For persistent CLI auth, write to a file in the user’s config directory. - Testing: Default
MemoryStorageworks. Each test gets a fresh instance.
Next steps
- Authentication — OAuth flows that use your storage backend.
- Client Types — Full
KontextStorageinterface reference.