The SurrealClient class is the main entry point for the Kotlin SDK. It connects to a SurrealDB instance, authenticates, queries, and manages data. It extends SurrealSession and implements AutoCloseable. Almost every networked method is a suspend function and has a ...Result companion that returns a Result instead of throwing.
Source: surrealdb.kotlin
Connection methods
SurrealClient(config)
Creates a new client from a SurrealClientConfig. With autoConnect = true (the default) the client connects lazily on the first request.
| Parameter | Type | Description |
|---|---|---|
config | SurrealClientConfig | The client configuration, including the connection url. |
Returns: SurrealClient
.connect()
Establishes the connection explicitly. Rarely needed when autoConnect is enabled.
Returns: Unit
.close()
Closes the active connection and releases all associated resources.
Returns: Unit
.use(namespace, database)
Selects the namespace and database for subsequent operations.
| Parameter | Type | Description |
|---|---|---|
namespace | String | The namespace to select. |
database | String | The database to select. |
Returns: JsonElement
.ping()
Pings the server to verify connectivity.
Returns: JsonElement
.version()
Returns the version of the connected SurrealDB server.
Returns: JsonElement
.supports(feature)
Returns whether the current transport supports the given SurrealFeature.
| Parameter | Type | Description |
|---|---|---|
feature | SurrealFeature | The feature to check. |
Returns: Boolean
Authentication methods
.signup(params)
Signs up against a record access method and returns a token.
| Parameter | Type | Description |
|---|---|---|
params | JsonObject | The sign-up parameters (namespace, database, access, and any record variables). |
Returns: JsonElement
.signin(params)
Signs in with the given credentials and returns a token.
| Parameter | Type | Description |
|---|---|---|
params | JsonObject | The sign-in credentials. |
Returns: JsonElement
.authenticate(token)
Authenticates the session with an existing token.
| Parameter | Type | Description |
|---|---|---|
token | String | A JWT previously issued by SurrealDB. |
Returns: JsonElement
.auth()
Returns the record of the currently authenticated user.
Returns: JsonElement
.invalidate()
Invalidates the current authentication for the session.
Returns: JsonElement
.reset()
Resets the session to its initial, unauthenticated state.
Returns: JsonElement
Query methods
.query(sql, vars)
Runs a raw SurrealQL statement with optional bound parameters.
| Parameter | Type | Description |
|---|---|---|
sql | String | The SurrealQL to execute. |
vars | JsonObject? | Optional parameters bound as $name in the query. |
Returns: JsonElement
There is also an overload that accepts a BoundQuery built with the surql DSL.
.queryAs<T>(sql, vars)
Runs SurrealQL and decodes the result into T using kotlinx.serialization.
Returns: T
.decode<T>(element)
Decodes a JsonElement into T using the client's configured Json instance.
Returns: T
.let(key, value)
Defines a session parameter referenced as $key in subsequent queries.
| Parameter | Type | Description |
|---|---|---|
key | String | The parameter name. |
value | JsonElement | The parameter value. |
Returns: JsonElement
.unset(key)
Removes a previously defined session parameter.
Returns: JsonElement
CRUD methods
These methods return a query builder that you refine and terminate with await() or awaitAs<T>(). The what argument accepts a Table, RecordId, or RecordIdRange.
.select(what)
Returns a SelectQuery for reading records.
Returns: SelectQuery
.create(what)
Returns a CreateQuery for inserting a record.
Returns: CreateQuery
.update(what)
Returns an UpdateQuery for replacing record content.
Returns: UpdateQuery
.upsert(what)
Returns an UpsertQuery for creating or updating records.
Returns: UpsertQuery
.merge(what, data)
Returns a MergeQuery that merges data into the matched records.
Returns: MergeQuery
.patch(what, patches, diff)
Returns a PatchQuery that applies JSON Patch operations.
Returns: PatchQuery
.delete(what)
Returns a DeleteQuery for removing records.
Returns: DeleteQuery
.relate(in, relation, out)
Returns a RelateQuery that creates a graph edge between two records.
Returns: RelateQuery
.insert(into, data)
Returns an InsertQuery that inserts one or more records into a Table.
Returns: InsertQuery
.insertRelation(into, data)
Returns an InsertRelationQuery that inserts relation records into a Table.
Returns: InsertRelationQuery
.run(function)
Returns a RunQuery that invokes a built-in or custom function.
Returns: RunQuery
Live query methods
.live(table, diff)
Starts a live query and returns a LiveQuerySubscription. WebSocket only.
| Parameter | Type | Description |
|---|---|---|
table | String | The table to watch. |
diff | Boolean? | When true, emit JSON Patch diffs instead of full records. |
Returns: LiveQuerySubscription
.kill(liveQueryId)
Kills a live query by its ID. WebSocket only.
Returns: JsonElement
Sessions
.newSession()
Creates a new isolated SurrealSession over the same connection. WebSocket only.
Returns: SurrealSession
.closeSession(session)
Closes a session previously created with .newSession().
Returns: Unit
Properties
| Property | Type | Description |
|---|---|---|
config | SurrealClientConfig | The configuration the client was created with. |
features | Set<SurrealFeature> | The features supported by the active transport. |
connectionEvents | SharedFlow<SurrealConnectionEvent> | A flow of connection lifecycle events. |
json | Json | The kotlinx.serialization instance used for encoding and decoding. |