Session Storage
Part of the API Reference. How TelegramClient persists (or doesn’t persist) a logged-in session between runs. See Configuration for wiring one up, and Custom Session Store for writing your own.
SessionStore
The abstract interface TelegramClient reads and writes through. Implement this yourself to persist sessions to a database, secrets manager, or anywhere else FileSessionStore doesn’t reach.
abstract class SessionStore {
Future<PtgcSession?> load();
Future<void> save(PtgcSession session);
Future<void> clear();
}
| Method | Called |
|---|---|
load |
Once, at the start of TelegramClient.connect(). Return null if there’s no saved session yet. |
save |
After a successful login, and after any data-center migration. |
clear |
By auth.logOut(), and anywhere you handle AuthRequiredException by discarding a dead session — see Handling an Expired Session. |
Treat whatever a SessionStore holds as sensitive: it lets whoever has it act as the logged-in account without a password or code.
FileSessionStore
class FileSessionStore implements SessionStore {
const FileSessionStore(this.path);
final String path;
}
The default implementation — persists to a JSON file on disk. TelegramClient.fromEnv() uses FileSessionStore('ptgc.session.json') unless you pass a different sessionStore.
MemorySessionStore
class MemorySessionStore implements SessionStore {
MemorySessionStore();
}
Keeps the session only in memory for the life of the process — nothing touches disk. Useful for tests and short-lived scripts. See In-Memory Session Store.
PtgcSession
The value SessionStore implementations actually read and write — an opaque bundle of the auth key, data center, and signed-in user ID. Treat it as a value to persist verbatim; ptgc doesn’t expect you to inspect or modify its fields.