Skip to main content
Version: 2.0.0-rc1

SDK API Overview

The SpacetimeDB client SDKs provide a comprehensive API for interacting with your database. After generating client bindings and establishing a connection, you can query data, invoke server functions, and observe real-time changes.

This page describes the core concepts and patterns that apply across all client SDKs. For language-specific details and complete API documentation, see the reference pages for Rust, C#, TypeScript, or Unreal Engine.

Prerequisites

Before using the SDK API, you must:

  1. Generate client bindings using spacetime generate
  2. Create a connection to your database

Subscriptions

Subscriptions replicate a subset of the database to your client, maintaining a local cache that automatically updates as the server state changes. Clients should subscribe to the data they need, then query the local cache.

Typical flow:

  1. Create a subscription with the SDK builder API
  2. Wait for onApplied/OnApplied to know initial rows are present
  3. Read from the local cache and register callbacks
  4. Unsubscribe when the data is no longer needed

For lifecycle guarantees and semantics, see Subscriptions and Subscription Semantics.

Example

import { tables } from './module_bindings';

const handle = conn
  .subscriptionBuilder()
  .onApplied(ctx => {
    console.log(`Ready with ${ctx.db.user.count()} users`);
  })
  .onError((ctx, error) => {
    console.error(`Subscription failed: ${error}`);
  })
  .subscribe([tables.user.where(r => r.online.eq(true))]);

Querying the Local Cache

After a subscription is applied, reads are local and do not require network round-trips.

const userCount = conn.db.user.count();
const user = conn.db.user.name.find('Alice');

Reacting to Cache Changes

Use row callbacks to react when subscribed rows are inserted, updated, or deleted.

conn.db.user.onInsert((ctx, row) => {});
conn.db.user.onUpdate((ctx, oldRow, newRow) => {});
conn.db.user.onDelete((ctx, row) => {});

Canonical API References