The Kotlin SDK for SurrealDB makes it straightforward to connect to your instance and start querying data with coroutines. This guide walks you through connecting, authenticating, and performing basic operations.
Note
1. Install the SDK
Follow the installation guide to add the SDK as a dependency in your project. Once installed, import the client to start using it.
2. Connect to SurrealDB
Create a SurrealClient with a SurrealClientConfig, then call .use() to select a namespace and database, and .signin() to authenticate.
The transport is selected automatically from the URL scheme:
WebSocket (
ws://,wss://) for long-lived stateful connections that support live queries, transactions, and multiple sessionsHTTP (
http://,https://) for short-lived stateless connections
The client connects automatically on first use (autoConnect defaults to true). Call .close() when you are finished to release resources.
3. Inserting data
To represent records in your application, define @Serializable data classes that match your table structure.
Use the .create() builder to insert a record. Pass a Table to create in a table with a generated ID, or a RecordId to create a record with a specific ID. Builders are terminated with await() (raw JSON) or the typed awaitAs<T>() extension.
4. Retrieving data
Selecting records
The .select() builder retrieves records from a table or a single record by its RecordId. Refine it with .where() using the expression helpers.
Running SurrealQL queries
For more advanced use cases, use .query() to execute SurrealQL statements with bound parameters, or .queryAs<T>() to decode the result directly.
5. Closing the connection
Call .close() to release the connection and all associated resources.
What's next?
You have learned how to install the SDK, connect to SurrealDB, create records, and retrieve data. There is a lot more you can do with the SDK, including authentication, live queries, transactions, and multiple sessions.
Connecting to SurrealDB
Learn how to manage connections, protocols, and reconnection.
Authentication
Read more about authentication and how to integrate it into your application.
Data manipulation
Learn how to create, read, update, and delete records using the builders.
API Reference
Complete reference for the client, builders, types, and errors.