Skip to main content
Version: 1.12.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:

  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.

Creating Subscriptions​

Subscribe to tables or queries using SQL:

// Subscribe with callbacks
conn
  .subscriptionBuilder()
  .onApplied(ctx => {
    console.log(`Subscription ready with ${ctx.db.User.count()} users`);
  })
  .onError((ctx, error) => {
    console.error(`Subscription failed: ${error}`);
  })
  .subscribe(['SELECT * FROM user']);

See the Subscriptions documentation for detailed information on subscription queries and semantics. Subscribe to tables for row data, or to views for computed query results.

Querying the Local Cache​

Once subscribed, query the local cache without network round-trips:

// Iterate all cached rows
for (const user of conn.db.user.iter()) {
  console.log(`${user.id}: ${user.name}`);
}

// Count cached rows
const userCount = conn.db.user.count();

// Find by unique column (if indexed)
const user = conn.db.user.name.find('Alice');
if (user) {
  console.log(`Found: ${user.email}`);
}

// Filter cached rows
const adminUsers = [...conn.db.user.iter()].filter(u => u.isAdmin);

Row Update Callbacks​

Register callbacks to observe insertions, updates, and deletions in the local cache:

// Called when a row is inserted
conn.db.User.onInsert((ctx, user) => {
  console.log(`User inserted: ${user.name}`);
});

// Called when a row is updated
conn.db.User.onUpdate((ctx, oldUser, newUser) => {
  console.log(`User ${newUser.id} updated: ${oldUser.name} -> ${newUser.name}`);
});

// Called when a row is deleted
conn.db.User.onDelete((ctx, user) => {
  console.log(`User deleted: ${user.name}`);
});

These callbacks fire whenever the local cache changes due to subscription updates, providing real-time reactivity.

Complete Examples​

For complete working examples, see the language-specific reference pages: