Surreal / AsyncSurreal
The Surreal and AsyncSurreal factory functions create a connection to a SurrealDB instance. They inspect the URL scheme and return the appropriate connection class — WebSocket, HTTP, or embedded — so you use the same interface regardless of protocol.
Surreal(url) returns a blocking (synchronous) connection. AsyncSurreal(url) returns an asynchronous connection. Both expose the same set of methods; the async variants must be awaited.
Source: surrealdb.py
Factory Functions
Surreal(url)
Creates a synchronous connection based on the URL scheme.
| Parameter | Type | Description |
|---|---|---|
url | str | The connection URL. The scheme determines the connection type. |
Returns: BlockingWsSurrealConnection | BlockingHttpSurrealConnection | BlockingEmbeddedSurrealConnection
AsyncSurreal(url)
Creates an asynchronous connection based on the URL scheme.
| Parameter | Type | Description |
|---|---|---|
url | str | The connection URL. The scheme determines the connection type. |
Returns: AsyncWsSurrealConnection | AsyncHttpSurrealConnection | AsyncEmbeddedSurrealConnection
URL Schemes
| Scheme | Connection Type | Description |
|---|---|---|
ws://, wss:// | WebSocket | Full-featured stateful connection. Supports live queries, sessions, and transactions. |
http://, https:// | HTTP | Stateless connection. Each request is independent. |
mem://, memory:// | Embedded (in-memory) | In-process database that does not persist data. |
file://, surrealkv:// | Embedded (on-disk) | In-process database backed by SurrealKV storage. |
Examples
Connection Methods
.connect()
Opens the connection to the SurrealDB instance. The URL can optionally be overridden here.
| Parameter | Type | Description |
|---|---|---|
url | str | None | An optional URL to override the one provided to the factory function. Defaults to None. |
Returns: None
Examples
.close()
Closes the active connection and releases resources.
Returns: None
Examples
.use()
Switches to a specific namespace and database.
| Parameter | Type | Description |
|---|---|---|
namespace | str | The namespace to use. |
database | str | The database to use. |
Returns: None
Examples
.version()
Returns the version string of the connected SurrealDB instance.
Returns: str
Examples
Authentication Methods
.signup()
Signs up a user to a specific access method.
| Parameter | Type | Description |
|---|---|---|
vars | dict[str, Value] | Variables used for signup, including namespace, database, access, and any additional fields required by the access method. |
Returns: Tokens
Examples
.signin()
Signs in to the database with the given credentials.
| Parameter | Type | Description |
|---|---|---|
vars | dict[str, Value] | Credentials for authentication. For root access, provide username and password. For scoped access, also include namespace, database, and access. |
Returns: Tokens
Examples
.authenticate()
Authenticates the current connection with a JWT token.
| Parameter | Type | Description |
|---|---|---|
token | str | The JWT token to authenticate with. |
Returns: None
Examples
.invalidate()
Invalidates the authentication for the current connection, removing the associated JWT token.
Returns: None
Examples
.info()
Returns the record of the currently authenticated user.
Returns: Value
Examples
Variables
.let()
Defines a variable on the current connection that can be used in subsequent queries.
| Parameter | Type | Description |
|---|---|---|
key | str | The name of the variable (without the $ prefix). |
value | Value | The value to assign to the variable. |
Returns: None
Examples
.unset()
Removes a previously defined variable from the current connection.
| Parameter | Type | Description |
|---|---|---|
key | str | The name of the variable to remove. |
Returns: None
Examples
Query Methods
.query()
Runs a set of SurrealQL statements against the database.
Important
| Parameter | Type | Description |
|---|---|---|
query | str | The SurrealQL query string to execute. |
vars | dict[str, Value] | None | Variables to bind into the query. Defaults to None. |
Returns: Value
Examples
.query_raw()
Runs a set of SurrealQL statements and returns the raw RPC response, including per-statement results, statuses, and execution times. Unlike .query(), errors in individual statements are returned in the response rather than raised as exceptions.
| Parameter | Type | Description |
|---|---|---|
query | str | The SurrealQL query string to execute. |
params | dict[str, Value] | None | Variables to bind into the query. Defaults to None. |
Returns: dict[str, Any]
Examples
CRUD Methods
.select()
Selects all records in a table, or a specific record by its ID.
| Parameter | Type | Description |
|---|---|---|
record | RecordIdType | A table name (str) or a RecordID to select. |
Returns: Value
Examples
.create()
Creates a record in a table. If a RecordID is passed, the record is created with that specific ID. If a table name is passed, a random ID is generated.
| Parameter | Type | Description |
|---|---|---|
record | RecordIdType | The table name or RecordID to create. |
data | Value | None | The record data. Defaults to None. |
Returns: Value
Examples
.update()
Replaces the entire record with the given data. Fields not present in data are removed.
| Parameter | Type | Description |
|---|---|---|
record | RecordIdType | The table name or RecordID to update. |
data | Value | None | The new record data. Defaults to None. |
Returns: Value
Examples
.upsert()
Updates an existing record or creates a new one if it does not exist. Replaces the entire record content.
| Parameter | Type | Description |
|---|---|---|
record | RecordIdType | The table name or RecordID to upsert. |
data | Value | None | The record data. Defaults to None. |
Returns: Value
Examples
.merge()
Merges the given data into an existing record. Existing fields not present in data are preserved.
| Parameter | Type | Description |
|---|---|---|
record | RecordIdType | The table name or RecordID to merge into. |
data | Value | None | The data to merge. Defaults to None. |
Returns: Value
Examples
.patch()
Applies JSON Patch operations to a record.
| Parameter | Type | Description |
|---|---|---|
record | RecordIdType | The table name or RecordID to patch. |
data | Value | None | A list of JSON Patch operations. Defaults to None. |
Returns: Value
Examples
.delete()
Deletes all records in a table, or a specific record by its ID.
| Parameter | Type | Description |
|---|---|---|
record | RecordIdType | The table name or RecordID to delete. |
Returns: Value
Examples
Insert Methods
.insert()
Inserts one or more records into a table.
| Parameter | Type | Description |
|---|---|---|
table | str | Table | The table to insert into. |
data | Value | A single record dict or a list of record dicts to insert. |
Returns: Value
Examples
.insert_relation()
Inserts one or more relation records into a relation table.
| Parameter | Type | Description |
|---|---|---|
table | str | Table | The relation table to insert into. |
data | Value | A single relation dict or a list of relation dicts. Each must include in and out fields. |
Returns: Value
Examples
Live Queries
Note
.live()
Initiates a live query for a table. Returns a UUID that identifies the live query and can be passed to .subscribe_live() and .kill().
| Parameter | Type | Description |
|---|---|---|
table | str | Table | The table to watch for changes. |
diff | bool | If True, notifications include JSON Patch diffs instead of full records. Defaults to False. |
Returns: UUID
Examples
.subscribe_live()
Returns a generator that yields live query notifications for the given query UUID. Each notification is a dict containing the action ("CREATE", "UPDATE", "DELETE"), the record data, and the record ID.
| Parameter | Type | Description |
|---|---|---|
query_uuid | str | UUID | The UUID of the live query returned by .live(). |
Returns (sync): Generator[dict[str, Value], None, None]
Returns (async): AsyncGenerator[dict[str, Value], None]
Examples
.kill()
Terminates a running live query by its UUID.
| Parameter | Type | Description |
|---|---|---|
query_uuid | str | UUID | The UUID of the live query to kill. |
Returns: None
Examples
Sessions
Note
Sessions allow you to create isolated contexts on a single connection, each with its own namespace, database, variables, and authentication state.
.new_session()
Creates a new isolated session on the current connection.
Returns (sync): BlockingSurrealSession
Returns (async): AsyncSurrealSession
Examples
.attach()
Attaches to the server-side session associated with this connection and returns its session ID.
Returns: UUID
Examples
.detach()
Detaches from a server-side session.
| Parameter | Type | Description |
|---|---|---|
session_id | UUID | The session ID to detach from. |
Returns: None
Examples
Transactions
Note
Transactions let you group multiple operations into an atomic unit. Changes are only applied when the transaction is committed, and can be rolled back with cancel.
.begin()
Begins a new transaction, optionally within a specific session.
| Parameter | Type | Description |
|---|---|---|
session_id | UUID | None | The session to start the transaction in. If None, uses the default session. Defaults to None. |
Returns: UUID — the transaction ID
Examples
.commit()
Commits a transaction, applying all changes made within it.
| Parameter | Type | Description |
|---|---|---|
txn_id | UUID | The transaction ID returned by .begin(). |
session_id | UUID | None | The session the transaction belongs to. Defaults to None. |
Returns: None
Examples
.cancel()
Cancels a transaction, discarding all changes made within it.
| Parameter | Type | Description |
|---|---|---|
txn_id | UUID | The transaction ID returned by .begin(). |
session_id | UUID | None | The session the transaction belongs to. Defaults to None. |
Returns: None
Examples
Context Manager
Both Surreal and AsyncSurreal support the context manager protocol. The connection is automatically opened on entry and closed on exit.
Synchronous
Asynchronous
Complete Example
See Also
SurrealSession — Session management reference
SurrealTransaction — Transaction reference
Data Types — Type aliases and value types
Errors — Error classes reference
Connecting to SurrealDB — Connection protocols and patterns