Kotlin SDK reference

Package layout, configuration, and error model for the Spectron Kotlin client.

ItemValue
Packagecom.surrealdb.kotlin.spectron
DistributionBundled in com.surrealdb:kotlin
Modelscom.surrealdb.kotlin.spectron.model
Sub-clientscom.surrealdb.kotlin.spectron.ns

The Spectron client ships inside the SurrealDB Kotlin SDK — there is no separate artifact. It is independent of the SurrealDB RPC engine and speaks Spectron's HTTP API directly.

import com.surrealdb.kotlin.spectron.Spectron
import kotlin.time.Duration.Companion.seconds

val memory = Spectron(
contextId = "acme-prod",
apiKey = "sk-spec-...",
endpoint = "https://api.spectron.example",
timeout = 30.seconds,
maxRetries = 3,
)
ParameterTypeDefaultNotes
contextIdStringrequiredThe context to operate in.
apiKeyStringrequiredBearer token; mutable var, applied on next request.
endpointStringrequiredBase URL; mutable var.
timeoutDuration30sPer-request timeout.
maxRetriesInt3GET-only retries.
httpClientHttpClient?nullInject a Ktor client.
jsonJsonlenientignoreUnknownKeys = true, explicitNulls = false.

Authentication uses the Authorization: Bearer header. Call close() to release the HTTP client.

Top-level suspend verbs on Spectron: remember, rememberMany, recall, queryContext, state, profile, reflect, forget, chat, consolidate, elaborate, inspect, audit, whoami, health.

Namespaced sub-clients:

PropertyTypePurpose
documentsSpectronDocumentsUpload, query, chunk, and manage documents (plus documents.keywords).
sessionsSpectronSessionsCreate and drive chat sessions.
entitiesSpectronEntitiesList, fetch, and delete extracted entities.
lifecycleSpectronLifecycleExpire, decay, and fsck maintenance.
tracesSpectronTracesInspect decision traces and stats.
principalsSpectronPrincipalsList grants and grant/revoke access.
scopesSpectronScopesRegister and manage scope nodes.
keysSpectronKeysSelf-service API key creation and rotation.

Scopes are hierarchical key/value slash-paths passed as a List<String>; an empty list targets the caller's default write region. Build them with scopePaths:

import com.surrealdb.kotlin.spectron.scopePaths

scopePaths(listOf("org/acme")) // ["org/acme"]
scopePaths(mapOf("org" to "acme")) // ["org/acme"]
scopePaths("team" to "eng", "org" to "acme") // ["team/eng", "org/acme"]

Every method takes an optional onBehalfOf: String?, sent as the X-Spectron-On-Behalf-Of header so a privileged caller acts as another principal.

GET requests retry on connection errors and 5xx responses with 250 ms / 500 ms / 1 s backoff, capped at maxRetries (default 3). Writes are never retried.

All failures throw a subclass of the sealed SpectronException, modelled on RFC 7807 problem details. Each carries status, title, detail, typeUri, instance, and extensions: Map<String, JsonElement>.

ExceptionHTTP status
SpectronAuthException401
SpectronScopeException403
SpectronNotFoundException404
SpectronValidationException400, 422
SpectronRateLimitException429 (with retryAfter: Duration?)
SpectronServerException5xx and unmatched
SpectronTransportException0 (connection or parse failure)

Response and request types live under com.surrealdb.kotlin.spectron.model, all @Serializable:

  • EnumsQueryMode, DocumentStatus, TurnRole, MemoryCategory, InferMode, GraphEdgeKind, and others.

  • FactsTriple, TripleEntity, BatchMessage, FactsResponseJson, FactsBatchResponseJson.

  • DocumentsDocumentJson, ChunkJson, KeywordJson, QueryFilter, UploadResponse, QueryResponseJson.

  • MemoryChatResponseJson, QueryMemoryResponseJson, ContextQueryResponseJson, StateResponseJson, ProfileResponseJson.

  • GovernancePrincipalJson, EffectiveGrantsJson, WhoamiResponse, ScopeNodeJson, AuditRowJson.

  • MaintenanceConsolidateResponseJson, ElaborateResponseJson, InspectResponseJson, FsckReportJson.

  • KeysMintedKey (secret returned once), KeyDetail.

Kotlin SDK

Was this page helpful?