Skip to main content
Reference engineering Archived 30 min read Updated

docs-quickstarts-c-sharp

\ C# Quickstart | 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
\\C# Quickstart

Get a SpacetimeDB C# app running in under 5 minutes.

Prerequisites​

\
  • .NET 10 SDK installed (.NET 8 is supported but will be deprecated soon). Please note that target .NET 10 is currently unsupported on Mac.
  • SpacetimeDB CLI installed.
  • note

    If you are building C# modules on macOS and need to target .NET 10, run SpacetimeDB inside a Linux environment, such as a Linux VM or container, and build and publish your module from there.

    \
    Create your project

    Run the spacetime dev command to create a new project with a C# SpacetimeDB module.

    This will start the local SpacetimeDB server, compile and publish your module, and generate C# client bindings.

    spacetime dev --template basic-cs
    Explore the project structure

    Your project contains both server and client code.

    Edit spacetimedb/Lib.cs to add tables and reducers. Use the generated bindings in the client project.

    my-spacetime-app/
    ├── spacetimedb/ # Your SpacetimeDB module
    │ ├── StdbModule.csproj
    │ └── Lib.cs # Server-side logic
    ├── client.csproj
    ├── Program.cs # Client application
    └── module_bindings/ # Auto-generated types
    Understand tables and reducers

    Open spacetimedb/Lib.cs to see the module code. The template includes a Person table and two reducers: Add to insert a person, and SayHello to greet everyone.

    Tables store your data. Reducers are functions that modify data — they're the only way to write to the database.

    using SpacetimeDB;
    
    public static partial class Module
    {
        [SpacetimeDB.Table(Accessor = "Person", Public = true)]
        public partial struct Person
        {
            public string Name;
        }
    
        [SpacetimeDB.Reducer]
        public static void Add(ReducerContext ctx, string name)
        {
            ctx.Db.Person.Insert(new Person { Name = name });
        }
    
        [SpacetimeDB.Reducer]
        public static void SayHello(ReducerContext ctx)
        {
            foreach (var person in ctx.Db.Person.Iter())
            {
                Log.Info($"Hello, {person.Name}!");
            }
            Log.Info("Hello, World!");
        }
    }
    Test with the CLI

    Open a new terminal and navigate to your project directory. Then use the SpacetimeDB CLI to call reducers and query your data directly.

    cd my-spacetime-app
    
    # Call the add reducer to insert a person
    spacetime call add Alice
    
    # Query the person table
    spacetime sql "SELECT * FROM Person"
     name
    ---------
     "Alice"
    
    # Call say_hello to greet everyone
    spacetime call say_hello
    
    # View the module logs
    spacetime logs
    2025-01-13T12:00:00.000000Z  INFO: Hello, Alice!
    2025-01-13T12:00:00.000000Z  INFO: Hello, World!

    Next steps​

    \
  • Enable automatic reconnect with .WithAutomaticReconnect() on your client's connection builder. Keep calling FrameTick() during outages. Register subscriptions once in OnConnect, which runs only on the first connection; the SDK replays them after recovery. Use OnAutomaticReconnect for recovery-specific work and subscription OnApplied for cache readiness. For expiring authentication tokens, also use .WithTokenProvider(() => RefreshTokenAsync()).
  • See the Chat App Tutorial for a complete example
  • Read the C# SDK Reference for detailed API docs