• Start

API Reference

/

Core

SurrealClient

The SurrealClient class is the main entry point for connecting to and interacting with a SurrealDB instance from Kotlin.

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

Import
import com.surrealdb.kotlin.SurrealClient
import com.surrealdb.kotlin.SurrealClientConfig

Creates a new client from a SurrealClientConfig. With autoConnect = true (the default) the client connects lazily on the first request.

Method Syntax
SurrealClient(config)

Parameter

Type

Description

configSurrealClientConfig

The client configuration, including the connection

url

.

Returns: SurrealClient

Example
val client = SurrealClient(SurrealClientConfig(url = "ws://localhost:8000"))

Establishes the connection explicitly. Rarely needed when autoConnect is enabled.

Method Syntax
client.connect()

Returns: Unit

Example
client.connect()

Closes the active connection and releases all associated resources.

Method Syntax
client.close()

Returns: Unit

Example
client.close()

Selects the namespace and database for subsequent operations.

Method Syntax
client.use(namespace, database)

Parameter

Type

Description

namespaceString

The namespace to select.

databaseString

The database to select.

Returns: JsonElement

Example
client.use("surrealdb", "docs")

Pings the server to verify connectivity.

Method Syntax
client.ping()

Returns: JsonElement

Returns the version of the connected SurrealDB server.

Method Syntax
client.version()

Returns: JsonElement

Example
val version = client.version()

Returns whether the current transport supports the given SurrealFeature.

Method Syntax
client.supports(feature)

Parameter

Type

Description

featureSurrealFeature

The feature to check.

Returns: Boolean

Example
if (client.supports(SurrealFeature.LiveQueries)) { /* ... */ }

Signs up against a record access method and returns a token.

Method Syntax
client.signup(params)

Parameter

Type

Description

paramsJsonObject

The sign-up parameters (namespace, database, access, and any record variables).

Returns: JsonElement

Signs in with the given credentials and returns a token.

Method Syntax
client.signin(params)

Parameter

Type

Description

paramsJsonObject

The sign-in credentials.

Returns: JsonElement

Example
client.signin(buildJsonObject {
    put("user", "root")
    put("pass", "root")
})

Authenticates the session with an existing token.

Method Syntax
client.authenticate(token)

Parameter

Type

Description

tokenString

A JWT previously issued by SurrealDB.

Returns: JsonElement

Returns the record of the currently authenticated user.

Method Syntax
client.auth()

Returns: JsonElement

Invalidates the current authentication for the session.

Method Syntax
client.invalidate()

Returns: JsonElement

Resets the session to its initial, unauthenticated state.

Method Syntax
client.reset()

Returns: JsonElement

Runs a raw SurrealQL statement with optional bound parameters.

Method Syntax
client.query(sql, vars)

Parameter

Type

Description

sqlString

The SurrealQL to execute.

varsJsonObject?

Optional parameters bound as

$name

in the query.

Returns: JsonElement

Example
val result = client.query(
    "SELECT * FROM person WHERE age > \$min",
    buildJsonObject { put("min", 18) },
)

There is also an overload that accepts a BoundQuery built with the surql DSL.

Runs SurrealQL and decodes the result into T using kotlinx.serialization.

Method Syntax
client.queryAs<T>(sql, vars)

Returns: T

Example
val people: List<Person> = client.queryAs("SELECT * FROM person")

Decodes a JsonElement into T using the client's configured Json instance.

Method Syntax
client.decode<T>(element)

Returns: T

Defines a session parameter referenced as $key in subsequent queries.

Method Syntax
client.let(key, value)

Parameter

Type

Description

keyString

The parameter name.

valueJsonElement

The parameter value.

Returns: JsonElement

Removes a previously defined session parameter.

Method Syntax
client.unset(key)

Returns: JsonElement

These methods return a [query builder](/docs/reference/kotlin/api/core/query-builder) that you refine and terminate with `await()` or [`awaitAs()`](/docs/reference/kotlin/api/core/query-builder#await-as). The `what` argument accepts a [`Table`](/docs/reference/kotlin/api/values/table), [`RecordId`](/docs/reference/kotlin/api/values/record-id), or [`RecordIdRange`](/docs/reference/kotlin/api/values/record-id-range).

Returns a SelectQuery for reading records.

Method Syntax
client.select(what)

Returns: SelectQuery

Example
val all: List<Person> = client.select(Table("person")).awaitAs()

Returns a CreateQuery for inserting a record.

Method Syntax
client.create(what)

Returns: CreateQuery

Returns an UpdateQuery for replacing record content.

Method Syntax
client.update(what)

Returns: UpdateQuery

Returns an UpsertQuery for creating or updating records.

Method Syntax
client.upsert(what)

Returns: UpsertQuery

Returns a MergeQuery that merges data into the matched records.

Method Syntax
client.merge(what, data)

Returns: MergeQuery

Returns a PatchQuery that applies JSON Patch operations.

Method Syntax
client.patch(what, patches, diff = false)

Returns: PatchQuery

Returns a DeleteQuery for removing records.

Method Syntax
client.delete(what)

Returns: DeleteQuery

Returns a RelateQuery that creates a graph edge between two records.

Method Syntax
client.relate(`in`, relation, out)

Returns: RelateQuery

Returns an InsertQuery that inserts one or more records into a Table.

Method Syntax
client.insert(into, data)

Returns: InsertQuery

Returns an InsertRelationQuery that inserts relation records into a Table.

Method Syntax
client.insertRelation(into, data)

Returns: InsertRelationQuery

Returns a RunQuery that invokes a built-in or custom function.

Method Syntax
client.run(function)

Returns: RunQuery

Starts a live query and returns a LiveQuerySubscription. WebSocket only.

Method Syntax
client.live(table, diff = null)

Parameter

Type

Description

tableString

The table to watch.

diffBoolean?

When

true

, emit JSON Patch diffs instead of full records.

Returns: LiveQuerySubscription

Kills a live query by its ID. WebSocket only.

Method Syntax
client.kill(liveQueryId)

Returns: JsonElement

Creates a new isolated SurrealSession over the same connection. WebSocket only.

Method Syntax
client.newSession()

Returns: SurrealSession

Closes a session previously created with .newSession().

Method Syntax
client.closeSession(session)

Returns: Unit

Property

Type

Description

configSurrealClientConfig

The configuration the client was created with.

featuresSet<SurrealFeature>

The features supported by the active transport.

connectionEventsSharedFlow<SurrealConnectionEvent>

A flow of connection lifecycle events.

jsonJson

The

kotlinx.serialization

instance used for encoding and decoding.

Was this page helpful?