Skip to main content
Session middleware provides persistent data storage for your bot, allowing you to remember information across updates. It attaches session data to every chat and makes it available on the context object under ctx.session.

session

Creates session middleware that loads and saves session data automatically.
SessionOptions<S, C> | MultiSessionOptions<S, C>
Configuration options for session middleware
Returns: Middleware function that adds ctx.session to the context.

Example

SessionOptions

Configuration options for single session middleware.
'single'
Session type. Defaults to 'single' if omitted.
() => S
Recommended. Function that produces an initial value for ctx.session. Called when the storage returns undefined for a session key. Must return a new value each time to avoid sharing session data between different chats.
string
Optional prefix to prepend to session keys. Useful for namespacing session data.
(ctx: Context) => string | undefined
Custom function to generate session keys. By default, sessions are stored per chat using ctx.chatId.Return undefined to skip session loading for an update.
StorageAdapter<S>
Storage adapter for reading and writing session data. Defaults to in-memory storage (data is lost on restart).See known storage adapters for database integrations.

Example with Options

MultiSessionOptions

Configuration for managing multiple independent sessions per update.
'multi'
required
Must be set to 'multi' for multi sessions
Each property of the session data object gets its own SessionOptions configuration.

Example

SessionFlavor

Context flavor that adds the session property.
S
Session data for the current update. Reading or writing throws if getSessionKey returns undefined.Set to null or undefined to delete the session.
Warning: The type system does not include | undefined to avoid cumbersome null checks. Ensure session data is always initialized via the initial option or by assigning a value if empty.

Example

lazySession

Creates lazy session middleware that loads session data on-demand.
SessionOptions<S, C>
Configuration options (same as regular session, but multi sessions are not supported)
Returns: Middleware function that adds lazy ctx.session to the context. Lazy sessions load data only when you access ctx.session, reducing unnecessary database queries for updates your bot ignores.

Example

LazySessionFlavor

Context flavor for lazy sessions.
MaybePromise<S>
Session data or a Promise of session data. First access triggers the database query; subsequent accesses return the cached value.

StorageAdapter

Interface for implementing custom storage backends.
(key: string) => MaybePromise<T | undefined>
required
Reads a value from storage. Returns undefined if the key doesn’t exist.
(key: string, value: T) => MaybePromise<void>
required
Writes a value to storage.
(key: string) => MaybePromise<void>
required
Deletes a value from storage.
(key: string) => MaybePromise<boolean>
Optional. Checks if a key exists in storage.
() => Iterable<string> | AsyncIterable<string>
Optional. Lists all keys in storage.
() => Iterable<T> | AsyncIterable<T>
Optional. Lists all values in storage.
() => Iterable<[string, T]> | AsyncIterable<[string, T]>
Optional. Lists all key-value pairs in storage.

Example Storage Adapter

MemorySessionStorage

Built-in in-memory storage adapter.
number
Optional TTL in milliseconds. Sessions older than this are automatically discarded. Defaults to Infinity (never expire).
Note: This adapter stores data in RAM. All data is lost when your bot process restarts. Use a database adapter for production.

Example

enhanceStorage

Enhances a storage adapter with session migrations and expiry dates.
MigrationOptions<T>
required
Enhancement options

Example with Migrations

Complete Example

See Also