Skip to main content
Reference engineering Archived 31 min read Updated

docs-clients-api

\ SDK API Overview | SpacetimeDB docs \ \document.documentElement.setAttribute("data-theme","light"),document.documentElement.setAttribute("data-theme-choice","light"),function(){try{const c=new URLSearchParams(window.location.search).entries();for(var[t,e]of c)if(t.startsWith("docusaurus-data-")){var a=t.replace("docusaurus-data-","data-");document.documentElement.setAttribute(a,e)}}catch(t){}}()
\Version: 2.0.0
\\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:

\
  • Generate client bindings using spacetime generate
  • 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:

    \
  • Create a subscription with the SDK builder API
  • Wait for onApplied/OnApplied to know initial rows are present
  • Read from the local cache and register callbacks
  • 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​

    \
  • Subscriptions - Lifecycle, usage patterns, and semantics
  • Subscription Semantics - Detailed consistency and ordering behavior
  • TypeScript Reference - SubscriptionBuilder, SubscriptionHandle, query builder API
  • C# Reference - SubscriptionBuilder, SubscriptionHandle
  • C# Query Builder API - Typed subscription query builder
  • Rust Reference - SubscriptionBuilder, SubscriptionHandle
  • Rust Query Builder API - Typed subscription query builder
  • Unreal Reference - Unreal subscription APIs