A session wraps a WebSocket connection with an isolated session ID. Each session maintains its own namespace, database, variables, and authentication state, independent of other sessions on the same connection.
Sessions are created by calling .new_session() on a WebSocket connection. They are not available on HTTP or embedded connections.
Note
Source: async_ws.py · blocking_ws.py
Creating a session
Returns (sync): BlockingSurrealSession
Returns (async): AsyncSurrealSession
Examples
Inherited methods
A session exposes the same interface as the parent connection. All methods below delegate to the underlying connection, scoped to this session's ID. For full parameter tables and examples, see the Surreal reference.
| Method | Returns | Description |
|---|---|---|
.use(namespace, database) | None | Switch namespace and database for this session. |
.query(query, vars) | Awaitable / lazy Value or tuple[Value, ...] builder | Execute one or more SurrealQL statements. |
.signin(vars) | Tokens | Sign in within this session. |
.signup(vars) | Tokens | Sign up within this session. |
.authenticate(token) | None | Authenticate this session with a JWT. |
.invalidate() | None | Invalidate this session's authentication. |
.let(key, value) | None | Define a session-scoped variable. |
.unset(key) | None | Remove a session-scoped variable. |
.select(record) | Value | Select records. |
.create(record, data) | CRUD builder -> dict[str, Value] | Create a record (chain .content/.replace/.merge/.patch). |
.update(record, data) | CRUD builder -> dict or list | Update records (chain .content/.replace/.merge/.patch). |
.upsert(record, data) | CRUD builder -> dict or list | Upsert a record (chain .content/.replace/.merge/.patch). |
.delete(record) | CRUD builder -> dict or list | Delete records. |
.insert(table, data, relation=False) | Insert builder -> list[Value] | Insert records. Pass relation=True or chain .relation() for INSERT RELATION. |
.run(name, args, version) | Value | Call a SurrealDB function. |
.live(table, diff) | UUID | Start a live query. |
.kill(query_uuid) | None | Kill a live query. |
Session-specific methods
.begin_transaction()
Begins a new transaction scoped to this session. Returns a transaction object that provides query and CRUD methods within the transaction boundary.
Returns (sync): BlockingSurrealTransaction
Returns (async): AsyncSurrealTransaction
Examples
.close_session()
Closes this session on the server, releasing its session ID and any associated state. After calling this method, the session object should not be used.
Returns: None
Examples
Complete example
See also
Surreal — Connection reference with full method documentation
SurrealTransaction — Transaction reference
Data Types — Type aliases and value types
Errors — Error classes reference