Set up SurrealDB in the AppHost
This article is the reference for the Aspire SurrealDB Hosting integration. It enumerates the AppHost APIs that you use to model a SurrealDB server, namespace, and database as resources in your AppHost project.
If you’re new to the SurrealDB integration, start with the Get started with SurrealDB integrations guide. For how consuming apps read the connection information this page exposes, see Connect to SurrealDB.
Installation
Section titled “Installation”To start building an Aspire app that uses SurrealDB, install the 📦 CommunityToolkit.Aspire.Hosting.SurrealDb NuGet package:
aspire add communitytoolkit-surrealdbThe Aspire CLI is interactive, be sure to select the appropriate search result when prompted:
Select an integration to add:
> communitytoolkit-surrealdb (CommunityToolkit.Aspire.Hosting.SurrealDb)> Other results listed as selectable options...#:package CommunityToolkit.Aspire.Hosting.SurrealDb@*<PackageReference Include="CommunityToolkit.Aspire.Hosting.SurrealDb" Version="*" />Or, choose a manual installation approach:
#:package CommunityToolkit.Aspire.Hosting.SurrealDb@*<PackageReference Include="CommunityToolkit.Aspire.Hosting.SurrealDb" Version="*" />aspire add CommunityToolkit.Aspire.Hosting.SurrealDbLearn more about aspire add in the command reference.
This updates your aspire.config.json with the SurrealDB hosting integration package:
{ "packages": { "CommunityToolkit.Aspire.Hosting.SurrealDb": "*" }}Add SurrealDB resource
Section titled “Add SurrealDB resource”Once you’ve installed the hosting integration in your AppHost project, you can add a SurrealDB server, namespace, and database resource as shown in the following example:
var builder = DistributedApplication.CreateBuilder(args);
var db = builder.AddSurrealServer("surreal") .AddNamespace("ns") .AddDatabase("db");
builder.AddProject<Projects.ExampleProject>() .WithReference(db) .WaitFor(db);
builder.Build().Run();import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.aspire/modules/aspire.mjs';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const surreal: SurrealDbServerResource
surreal = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addSurrealServer(name: string, options?: { userName?: string | ParameterResource; password?: string | ParameterResource; port?: number; path?: string; strictMode?: boolean;}): SurrealDbServerResource (+1 overload)
Adds a SurrealDB resource to the application model. A container is used for local development. The default image is and the tag is .
addSurrealServer("surreal");const const ns: SurrealDbNamespaceResource
ns = await const surreal: SurrealDbServerResource
surreal.SurrealDbServerResource.addNamespace(name: string, options?: { namespaceName?: string;}): SurrealDbNamespaceResource (+1 overload)
Adds a SurrealDB namespace resource to the application model
addNamespace("ns");const const db: SurrealDbDatabaseResource
db = await const ns: SurrealDbNamespaceResource
ns.SurrealDbNamespaceResource.addDatabase(name: string, options?: { databaseName?: string;}): SurrealDbDatabaseResource (+1 overload)
Adds a SurrealDB database resource to the application model
addDatabase("db");
const const exampleProject: ProjectResource
exampleProject = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addProject(name: string, projectPath: string, options?: { launchProfileOrOptions?: ProjectResourceOptions;}): ProjectResource (+1 overload)
Adds a .NET project resource
addProject("apiservice", "../ExampleProject/ExampleProject.csproj");await const exampleProject: ProjectResource
exampleProject.ProjectResource.withReference(source: EndpointReference | string | uri, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): ProjectResource (+1 overload)
Adds a reference to another resource
withReference(const db: SurrealDbDatabaseResource
db);await const exampleProject: ProjectResource
exampleProject.ProjectResource.waitFor(dependency: IResource | IResourceWithConnectionString, waitBehavior?: WaitBehavior): ProjectResource
Waits for another resource to be ready
waitFor(const db: SurrealDbDatabaseResource
db);
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.build(): DistributedApplication
Builds the distributed application
build().DistributedApplication.run(cancellationToken?: cancellationToken): void
Runs the distributed application
run();-
When Aspire adds a container image to the AppHost, as shown in the preceding example with the
docker.io/surrealdb/surrealdbimage, it creates a new SurrealDB instance on your local machine. -
The SurrealDB resource includes default credentials with a
usernameofrootand a randompasswordgenerated using theCreateDefaultPasswordParametermethod. When the AppHost runs, the password is stored in the AppHost’s secret store under the keysurreal-password. -
Chaining
.AddNamespace("ns")declares a SurrealDB namespace resource, and chaining.AddDatabase("db")declares a database inside that namespace. -
The AppHost reference call configures a connection in the consuming project named after the referenced database resource, such as
dbin the preceding example.
Add Surrealist
Section titled “Add Surrealist”To add Surrealist to the SurrealDB server resource, call the WithSurrealist method:
var builder = DistributedApplication.CreateBuilder(args);
var surreal = builder.AddSurrealServer("surreal") .WithSurrealist();
var db = surreal .AddNamespace("ns") .AddDatabase("db");
builder.AddProject<Projects.ExampleProject>() .WithReference(db) .WaitFor(db);
builder.Build().Run();import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.aspire/modules/aspire.mjs';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
let let surreal: SurrealDbServerResource
surreal = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addSurrealServer(name: string, options?: { userName?: string | ParameterResource; password?: string | ParameterResource; port?: number; path?: string; strictMode?: boolean;}): SurrealDbServerResource (+1 overload)
Adds a SurrealDB resource to the application model. A container is used for local development. The default image is and the tag is .
addSurrealServer("surreal");let surreal: SurrealDbServerResource
surreal = await let surreal: SurrealDbServerResource
surreal.SurrealDbServerResource.withSurrealist(options?: { containerName?: string;}): SurrealDbServerResource (+1 overload)
withSurrealist();
const const ns: SurrealDbNamespaceResource
ns = await let surreal: SurrealDbServerResource
surreal.SurrealDbServerResource.addNamespace(name: string, options?: { namespaceName?: string;}): SurrealDbNamespaceResource (+1 overload)
Adds a SurrealDB namespace resource to the application model
addNamespace("ns");const const db: SurrealDbDatabaseResource
db = await const ns: SurrealDbNamespaceResource
ns.SurrealDbNamespaceResource.addDatabase(name: string, options?: { databaseName?: string;}): SurrealDbDatabaseResource (+1 overload)
Adds a SurrealDB database resource to the application model
addDatabase("db");
const const exampleProject: ProjectResource
exampleProject = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addProject(name: string, projectPath: string, options?: { launchProfileOrOptions?: ProjectResourceOptions;}): ProjectResource (+1 overload)
Adds a .NET project resource
addProject("apiservice", "../ExampleProject/ExampleProject.csproj");await const exampleProject: ProjectResource
exampleProject.ProjectResource.withReference(source: EndpointReference | string | uri, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): ProjectResource (+1 overload)
Adds a reference to another resource
withReference(const db: SurrealDbDatabaseResource
db);await const exampleProject: ProjectResource
exampleProject.ProjectResource.waitFor(dependency: IResource | IResourceWithConnectionString, waitBehavior?: WaitBehavior): ProjectResource
Waits for another resource to be ready
waitFor(const db: SurrealDbDatabaseResource
db);
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.build(): DistributedApplication
Builds the distributed application
build().DistributedApplication.run(cancellationToken?: cancellationToken): void
Runs the distributed application
run();Surrealist is a web-based user interface for interacting with your SurrealDB database visually. It lets you connect to any SurrealDB instance, execute queries, explore your tables, and design your schemas.
Add SurrealDB resource with data bind mount
Section titled “Add SurrealDB resource with data bind mount”To add a data bind mount to the SurrealDB resource, call the WithDataBindMount method:
var builder = DistributedApplication.CreateBuilder(args);
var db = builder.AddSurrealServer("surreal") .WithDataBindMount("./data/surreal/data") .AddNamespace("ns") .AddDatabase("db");
builder.AddProject<Projects.ExampleProject>() .WithReference(db) .WaitFor(db);
builder.Build().Run();import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.aspire/modules/aspire.mjs';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
let let surreal: SurrealDbServerResource
surreal = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addSurrealServer(name: string, options?: { userName?: string | ParameterResource; password?: string | ParameterResource; port?: number; path?: string; strictMode?: boolean;}): SurrealDbServerResource (+1 overload)
Adds a SurrealDB resource to the application model. A container is used for local development. The default image is and the tag is .
addSurrealServer("surreal");let surreal: SurrealDbServerResource
surreal = await let surreal: SurrealDbServerResource
surreal.SurrealDbServerResource.withDataBindMount(source: string): SurrealDbServerResource
Adds a bind mount for the data folder to a SurrealDB resource.
withDataBindMount("./data/surreal/data");
const const ns: SurrealDbNamespaceResource
ns = await let surreal: SurrealDbServerResource
surreal.SurrealDbServerResource.addNamespace(name: string, options?: { namespaceName?: string;}): SurrealDbNamespaceResource (+1 overload)
Adds a SurrealDB namespace resource to the application model
addNamespace("ns");const const db: SurrealDbDatabaseResource
db = await const ns: SurrealDbNamespaceResource
ns.SurrealDbNamespaceResource.addDatabase(name: string, options?: { databaseName?: string;}): SurrealDbDatabaseResource (+1 overload)
Adds a SurrealDB database resource to the application model
addDatabase("db");
const const exampleProject: ProjectResource
exampleProject = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addProject(name: string, projectPath: string, options?: { launchProfileOrOptions?: ProjectResourceOptions;}): ProjectResource (+1 overload)
Adds a .NET project resource
addProject("apiservice", "../ExampleProject/ExampleProject.csproj");await const exampleProject: ProjectResource
exampleProject.ProjectResource.withReference(source: EndpointReference | string | uri, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): ProjectResource (+1 overload)
Adds a reference to another resource
withReference(const db: SurrealDbDatabaseResource
db);await const exampleProject: ProjectResource
exampleProject.ProjectResource.waitFor(dependency: IResource | IResourceWithConnectionString, waitBehavior?: WaitBehavior): ProjectResource
Waits for another resource to be ready
waitFor(const db: SurrealDbDatabaseResource
db);
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.build(): DistributedApplication
Builds the distributed application
build().DistributedApplication.run(cancellationToken?: cancellationToken): void
Runs the distributed application
run();Data bind mounts rely on the host machine’s filesystem to persist SurrealDB data across container restarts. The mount path ./data/surreal/data is resolved relative to the AppHost project directory. For more information on data bind mounts, see Docker docs: Bind mounts.
Add SurrealDB resource with parameters
Section titled “Add SurrealDB resource with parameters”When you want to explicitly provide the password used by the container image, you can provide it as a parameter:
var builder = DistributedApplication.CreateBuilder(args);
var password = builder.AddParameter("password", secret: true);
var db = builder.AddSurrealServer("surreal", password: password) .AddNamespace("ns") .AddDatabase("db");
builder.AddProject<Projects.ExampleProject>() .WithReference(db) .WaitFor(db);
builder.Build().Run();import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.aspire/modules/aspire.mjs';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const password: ParameterResource
password = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addParameter(name: string, options?: { value?: string; publishValueAsDefault?: boolean; secret?: boolean;}): ParameterResource (+1 overload)
Adds a parameter resource
addParameter("password", { secret?: boolean | undefined
secret: true });
const const surreal: SurrealDbServerResource
surreal = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addSurrealServer(name: string, options?: { userName?: string | ParameterResource; password?: string | ParameterResource; port?: number; path?: string; strictMode?: boolean;}): SurrealDbServerResource (+1 overload)
Adds a SurrealDB resource to the application model. A container is used for local development. The default image is and the tag is .
addSurrealServer("surreal", { password?: string | ParameterResource | undefined
password });const const ns: SurrealDbNamespaceResource
ns = await const surreal: SurrealDbServerResource
surreal.SurrealDbServerResource.addNamespace(name: string, options?: { namespaceName?: string;}): SurrealDbNamespaceResource (+1 overload)
Adds a SurrealDB namespace resource to the application model
addNamespace("ns");const const db: SurrealDbDatabaseResource
db = await const ns: SurrealDbNamespaceResource
ns.SurrealDbNamespaceResource.addDatabase(name: string, options?: { databaseName?: string;}): SurrealDbDatabaseResource (+1 overload)
Adds a SurrealDB database resource to the application model
addDatabase("db");
const const exampleProject: ProjectResource
exampleProject = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addProject(name: string, projectPath: string, options?: { launchProfileOrOptions?: ProjectResourceOptions;}): ProjectResource (+1 overload)
Adds a .NET project resource
addProject("apiservice", "../ExampleProject/ExampleProject.csproj");await const exampleProject: ProjectResource
exampleProject.ProjectResource.withReference(source: EndpointReference | string | uri, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): ProjectResource (+1 overload)
Adds a reference to another resource
withReference(const db: SurrealDbDatabaseResource
db);await const exampleProject: ProjectResource
exampleProject.ProjectResource.waitFor(dependency: IResource | IResourceWithConnectionString, waitBehavior?: WaitBehavior): ProjectResource
Waits for another resource to be ready
waitFor(const db: SurrealDbDatabaseResource
db);
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.build(): DistributedApplication
Builds the distributed application
build().DistributedApplication.run(cancellationToken?: cancellationToken): void
Runs the distributed application
run();For more information on providing parameters, see External parameters.
Configure logging
Section titled “Configure logging”To configure the log level for the SurrealDB container, call the WithLogLevel method:
var builder = DistributedApplication.CreateBuilder(args);
var surreal = builder.AddSurrealServer("surreal") .WithLogLevel(Microsoft.Extensions.Logging.LogLevel.Debug);
var db = surreal .AddNamespace("ns") .AddDatabase("db");
builder.AddProject<Projects.ExampleProject>() .WithReference(db) .WaitFor(db);
builder.Build().Run();import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.aspire/modules/aspire.mjs';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
let let surreal: SurrealDbServerResource
surreal = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addSurrealServer(name: string, options?: { userName?: string | ParameterResource; password?: string | ParameterResource; port?: number; path?: string; strictMode?: boolean;}): SurrealDbServerResource (+1 overload)
Adds a SurrealDB resource to the application model. A container is used for local development. The default image is and the tag is .
addSurrealServer("surreal");let surreal: SurrealDbServerResource
surreal = await let surreal: SurrealDbServerResource
surreal.SurrealDbServerResource.withLogLevel(logLevel: string): SurrealDbServerResource
withLogLevel("Debug");
const const ns: SurrealDbNamespaceResource
ns = await let surreal: SurrealDbServerResource
surreal.SurrealDbServerResource.addNamespace(name: string, options?: { namespaceName?: string;}): SurrealDbNamespaceResource (+1 overload)
Adds a SurrealDB namespace resource to the application model
addNamespace("ns");const const db: SurrealDbDatabaseResource
db = await const ns: SurrealDbNamespaceResource
ns.SurrealDbNamespaceResource.addDatabase(name: string, options?: { databaseName?: string;}): SurrealDbDatabaseResource (+1 overload)
Adds a SurrealDB database resource to the application model
addDatabase("db");
const const exampleProject: ProjectResource
exampleProject = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addProject(name: string, projectPath: string, options?: { launchProfileOrOptions?: ProjectResourceOptions;}): ProjectResource (+1 overload)
Adds a .NET project resource
addProject("apiservice", "../ExampleProject/ExampleProject.csproj");await const exampleProject: ProjectResource
exampleProject.ProjectResource.withReference(source: EndpointReference | string | uri, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): ProjectResource (+1 overload)
Adds a reference to another resource
withReference(const db: SurrealDbDatabaseResource
db);await const exampleProject: ProjectResource
exampleProject.ProjectResource.waitFor(dependency: IResource | IResourceWithConnectionString, waitBehavior?: WaitBehavior): ProjectResource
Waits for another resource to be ready
waitFor(const db: SurrealDbDatabaseResource
db);
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.build(): DistributedApplication
Builds the distributed application
build().DistributedApplication.run(cancellationToken?: cancellationToken): void
Runs the distributed application
run();The WithLogLevel method enables verbose logging in the SurrealDB container, which is useful during development and debugging. The polyglot TypeScript AppHost accepts the log level as a string (for example, "Trace", "Debug", "Information", "Warning", "Error", "Critical", or "None").
Health checks
Section titled “Health checks”The SurrealDB hosting integration automatically adds a health check for the SurrealDB namespace and database resources. The health check uses both the /health endpoint and a simple raw query to verify that SurrealDB is running and that a connection can be established. The health check is wired into the /health HTTP endpoint, where all registered health checks must pass before the app is considered ready to accept traffic.