Skip to content
DocsTry Aspire
DocsTry

Set up Valkey in the AppHost

Valkey logo

This article is the reference for the Aspire Valkey Hosting integration. It enumerates the AppHost APIs — with examples for both AppHost.cs and apphost.mts — that you use to model a Valkey resource in your AppHost project.

If you’re new to the Valkey integration, start with the Get started with Valkey integrations guide. For how consuming apps read the connection information this page exposes, see Connect to Valkey.

To start building an Aspire app that uses Valkey, install the 📦 Aspire.Hosting.Valkey NuGet package:

Terminal
aspire add valkey

Learn more about aspire add in the command reference.

This updates your aspire.config.json with the Valkey hosting integration package:

aspire.config.json
{
"packages": {
"Aspire.Hosting.Valkey": "13.5.3"
}
}

Once you’ve installed the hosting integration in your AppHost project, you can add a Valkey resource as shown in the following examples:

apphost.mts
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const cache = await builder.addValkey("cache");
await builder.addNodeApp("api", "./api", "index.js")
.withReference(cache);
// After adding all resources, run the app...
  1. When Aspire adds a container image to the AppHost, as shown in the preceding example with the docker.io/valkey/valkey image, it creates a new Valkey instance on your local machine.

  2. The Valkey resource is configured with a randomly generated password by default. To set an explicit password, see Add Valkey resource with parameters.

  3. The AppHost reference call configures a connection in the consuming project named after the referenced Valkey resource, such as cache in the preceding example.

  • Valkey container imageDocker Hub
    docker.io/valkey/valkey:9.1

    Added by AddValkey()addValkey().

    Source

Tags reflect the latest defaults on the microsoft/aspire main branch, and may be newer than the version pinned by the package you install.

Add a data volume to the Valkey resource as shown in the following examples:

apphost.mts
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 cache: ValkeyResource
cache
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addValkey(name: string, options?: {
port?: number;
password?: string | ParameterResource;
}): ValkeyResource (+1 overload)

Adds a Valkey container to the application model.

addValkey
("cache");
await
const cache: ValkeyResource
cache
.
ValkeyResource.withDataVolume(options?: {
name?: string;
isReadOnly?: boolean;
} | undefined): ValkeyResource (+1 overload)

Adds a named volume for the data folder to a Valkey container resource and enables Valkey persistence.

withDataVolume
({
isReadOnly?: boolean | undefined
isReadOnly
: false });
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource

Adds a node application to the application model. Node should be available on the PATH.

addNodeApp
("api", "./api", "index.js")
.
ExecutableResource.withReference(source: EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): NodeAppResource (+1 overload)

Adds a reference to another resource

withReference
(
const cache: ValkeyResource
cache
);
// After adding all resources, run the app...

The data volume is used to persist Valkey data outside the lifecycle of its container. The data volume is mounted at the /data path in the Valkey container, and when a name parameter isn’t provided, the name is generated at random. Calling WithDataVolume (or withDataVolume) also enables Valkey persistence so the in-memory state survives container restarts. For more information on data volumes and details on why they’re preferred over bind mounts, see Docker docs: Volumes.

Add a data bind mount to the Valkey resource as shown in the following examples:

apphost.mts
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 cache: ValkeyResource
cache
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addValkey(name: string, options?: {
port?: number;
password?: string | ParameterResource;
}): ValkeyResource (+1 overload)

Adds a Valkey container to the application model.

addValkey
("cache");
await
const cache: ValkeyResource
cache
.
ValkeyResource.withDataBindMount(source: string, options?: {
isReadOnly?: boolean;
} | undefined): ValkeyResource (+1 overload)

Adds a bind mount for the data folder to a Valkey container resource and enables Valkey persistence.

withDataBindMount
("/Valkey/Data", {
isReadOnly?: boolean | undefined
isReadOnly
: false });
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource

Adds a node application to the application model. Node should be available on the PATH.

addNodeApp
("api", "./api", "index.js")
.
ExecutableResource.withReference(source: EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): NodeAppResource (+1 overload)

Adds a reference to another resource

withReference
(
const cache: ValkeyResource
cache
);
// After adding all resources, run the app...

Data bind mounts rely on the host machine’s filesystem to persist Valkey data across container restarts. The data bind mount is mounted at the C:\Valkey\Data on Windows (or /Valkey/Data on Unix) path on the host machine in the Valkey container. As with WithDataVolume, this call also enables persistence. For more information on data bind mounts, see Docker docs: Bind mounts.

To configure Valkey snapshot persistence explicitly, call WithPersistence (or withPersistence) alongside a data volume or bind mount:

apphost.mts
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 cache: ValkeyResource
cache
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addValkey(name: string, options?: {
port?: number;
password?: string | ParameterResource;
}): ValkeyResource (+1 overload)

Adds a Valkey container to the application model.

addValkey
("cache");
await
const cache: ValkeyResource
cache
.
ValkeyResource.withDataVolume(options?: {
name?: string;
isReadOnly?: boolean;
} | undefined): ValkeyResource (+1 overload)

Adds a named volume for the data folder to a Valkey container resource and enables Valkey persistence.

withDataVolume
();
await
const cache: ValkeyResource
cache
.
ValkeyResource.withPersistence(options?: {
interval?: timespan;
keysChangedThreshold?: number;
} | undefined): ValkeyResource (+1 overload)

Configures a Valkey container resource for persistence.

withPersistence
({
interval?: timespan | undefined
interval
: 5 * 60 * 1000,
keysChangedThreshold?: number | undefined
keysChangedThreshold
: 100,
});
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource

Adds a node application to the application model. Node should be available on the PATH.

addNodeApp
("api", "./api", "index.js")
.
ExecutableResource.withReference(source: EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): NodeAppResource (+1 overload)

Adds a reference to another resource

withReference
(
const cache: ValkeyResource
cache
);
// After adding all resources, run the app...

The preceding code adds explicit persistence to the Valkey resource by snapshotting data at the configured interval whenever the configured number of keys changes. The C# AppHost accepts a TimeSpan for interval; the TypeScript AppHost accepts the same value as milliseconds.

When you want to explicitly provide the port and password used by the Valkey container, you can pass them as parameters:

apphost.mts
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 cache: ValkeyResource
cache
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addValkey(name: string, options?: {
port?: number;
password?: string | ParameterResource;
}): ValkeyResource (+1 overload)

Adds a Valkey container to the application model.

addValkey
("cache", {
port?: number | undefined
port
: 6379,
password?: string | ParameterResource | undefined
password
});
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource

Adds a node application to the application model. Node should be available on the PATH.

addNodeApp
("api", "./api", "index.js")
.
ExecutableResource.withReference(source: EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): NodeAppResource (+1 overload)

Adds a reference to another resource

withReference
(
const cache: ValkeyResource
cache
);
// After adding all resources, run the app...

When no password parameter is provided, Aspire generates a strong password automatically using the CreateDefaultPasswordParameter method.

By default, Aspire injects the Valkey connection information using variable names derived from the resource name (for example, CACHE_URI, CACHE_HOST, CACHE_PORT, CACHE_PASSWORD). If your consuming app expects a different set of environment variable names, pass individual connection properties from the AppHost:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
,
type EndpointProperty = "Url" | "Host" | "IPV4Host" | "Port" | "Scheme" | "TargetPort" | "HostAndPort" | "TlsEnabled"
const EndpointProperty: {
readonly Url: "Url";
readonly Host: "Host";
readonly IPV4Host: "IPV4Host";
readonly Port: "Port";
readonly Scheme: "Scheme";
readonly TargetPort: "TargetPort";
readonly HostAndPort: "HostAndPort";
readonly TlsEnabled: "TlsEnabled";
}

Enum Aspire.Hosting.ApplicationModel.EndpointProperty

EndpointProperty
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const cache: ValkeyResource
cache
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addValkey(name: string, options?: {
port?: number;
password?: string | ParameterResource;
}): ValkeyResource (+1 overload)

Adds a Valkey container to the application model.

addValkey
("cache");
const
const cacheEndpoint: EndpointReference
cacheEndpoint
= await
const cache: ValkeyResource
cache
.
ContainerResource.getEndpoint(name: string): EndpointReference

Gets an endpoint reference

getEndpoint
("tcp");
const
const cacheHost: EndpointReferenceExpression
cacheHost
= await
const cacheEndpoint: EndpointReference
cacheEndpoint
.
EndpointReference.property(property: EndpointProperty): EndpointReferenceExpression

Gets the specified property expression of the endpoint.

property
(
const EndpointProperty: {
readonly Url: "Url";
readonly Host: "Host";
readonly IPV4Host: "IPV4Host";
readonly Port: "Port";
readonly Scheme: "Scheme";
readonly TargetPort: "TargetPort";
readonly HostAndPort: "HostAndPort";
readonly TlsEnabled: "TlsEnabled";
}

Enum Aspire.Hosting.ApplicationModel.EndpointProperty

EndpointProperty
.
type Host: "Host"
Host
);
const
const cachePort: EndpointReferenceExpression
cachePort
= await
const cacheEndpoint: EndpointReference
cacheEndpoint
.
EndpointReference.property(property: EndpointProperty): EndpointReferenceExpression

Gets the specified property expression of the endpoint.

property
(
const EndpointProperty: {
readonly Url: "Url";
readonly Host: "Host";
readonly IPV4Host: "IPV4Host";
readonly Port: "Port";
readonly Scheme: "Scheme";
readonly TargetPort: "TargetPort";
readonly HostAndPort: "HostAndPort";
readonly TlsEnabled: "TlsEnabled";
}

Enum Aspire.Hosting.ApplicationModel.EndpointProperty

EndpointProperty
.
type Port: "Port"
Port
);
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource

Adds a node application to the application model. Node should be available on the PATH.

addNodeApp
("my-app", "./app", "index.js")
.
ExecutableResource.withReference(source: EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): NodeAppResource (+1 overload)

Adds a reference to another resource

withReference
(
const cache: ValkeyResource
cache
)
.
ExecutableResource.withEnvironment(name: string, value: string | IResourceWithConnectionString | IValueProvider): NodeAppResource

Sets an environment variable

withEnvironment
("VALKEY_HOST",
const cacheHost: EndpointReferenceExpression
cacheHost
)
.
ExecutableResource.withEnvironment(name: string, value: string | IResourceWithConnectionString | IValueProvider): NodeAppResource

Sets an environment variable

withEnvironment
("VALKEY_PORT",
const cachePort: EndpointReferenceExpression
cachePort
)
.
ExecutableResource.withEnvironment(name: string, value: string | IResourceWithConnectionString | IValueProvider): NodeAppResource

Sets an environment variable

withEnvironment
("VALKEY_PASSWORD", await
const cache: ValkeyResource
cache
.
ValkeyResource.passwordParameter: () => Promise<ParameterResource>

Gets the parameter that contains the Valkey server password.

passwordParameter
());
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();

To reference an externally managed Valkey instance instead of running one as a container, use AddConnectionString:

apphost.mts
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const cache = await builder.addConnectionString("cache");
await builder.addNodeApp("my-app", "./app", "index.js")
.withReference(cache);
// After adding all resources, run the app...
await builder.build().run();

With AddConnectionString and addConnectionString, Aspire resolves cache from ConnectionStrings:cache (or environment variable ConnectionStrings__cache) in the AppHost configuration. Consuming apps receive that value as a single connection string, not deconstructed Valkey resource-specific connection-property variables such as CACHE_HOST, CACHE_PORT, or CACHE_URI.

For the full reference of Valkey resource connection properties — and how consuming apps in C#, TypeScript, Python, and Go read them — see Connect to Valkey.

The Valkey hosting integration automatically adds a health check for the Valkey resource. The health check verifies that the Valkey instance is running and that a connection can be established to it.

The hosting integration relies on the 📦 AspNetCore.HealthChecks.Redis NuGet package, which works for Valkey because it speaks the Redis serialization protocol.