This article is the reference for the Aspire Apache Kafka Hosting integration. It enumerates the AppHost APIs — with examples for both AppHost.cs and apphost.ts — that you use to model a Kafka resource in your AppHost project.
var builder =DistributedApplication.CreateBuilder(args);
var kafka =builder.AddKafka("kafka");
var exampleProject =builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(kafka);
// After adding all resources, run the app...
TypeScript — apphost.ts
import{createBuilder}from'./.modules/aspire.js';
constbuilder=awaitcreateBuilder();
constkafka=awaitbuilder.addKafka("kafka");
awaitbuilder.addNodeApp("api","./api","index.js")
.withReference(kafka);
// After adding all resources, run the app...
When Aspire adds a container image to the AppHost, as shown in the preceding example with the confluentinc/confluent-local image, it creates a new Kafka server instance on your local machine.
The AppHost reference call configures a connection in the consuming project named after the referenced Kafka resource, such as kafka in the preceding example.
var builder =DistributedApplication.CreateBuilder(args);
var kafka =builder.AddKafka("kafka")
.WithKafkaUI();
var exampleProject =builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(kafka);
// After adding all resources, run the app...
TypeScript — apphost.ts
import{createBuilder}from'./.modules/aspire.js';
constbuilder=awaitcreateBuilder();
constkafka=awaitbuilder.addKafka("kafka");
awaitkafka.withKafkaUI();
awaitbuilder.addNodeApp("api","./api","index.js")
.withReference(kafka);
// After adding all resources, run the app...
The Kafka UI is a free, open-source web UI to monitor and manage Apache Kafka clusters. It’s automatically linked to your Kafka server and appears as a separate resource in the Aspire dashboard.
The data volume is used to persist Kafka broker data outside the lifecycle of its container. For more information on data volumes and details on why they’re preferred over bind mounts, see Docker docs: Volumes.
The Aspire Kafka integration deploys a container from the confluentinc/confluent-local image, which provides a simple Apache Kafka cluster running in KRaft mode with no further configuration required. It’s ideal for local development and testing, but this image is for local experimentation only and isn’t supported by Confluent in production.
To use a full broker topology in non-development environments, switch to an externally managed Kafka cluster via a connection string:
By default, Aspire injects the Kafka connection information using variable names derived from the resource name (for example, KAFKA_HOST, KAFKA_PORT). If your consuming app expects a different set of environment variable names, pass individual connection properties from the AppHost:
var builder =DistributedApplication.CreateBuilder(args);
var kafka =builder.AddConnectionString("kafka");
var exampleProject =builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(kafka);
// After adding all resources, run the app...
The TypeScript AppHost doesn’t currently expose an asExisting(...) API for Kafka. To connect to an existing Kafka instance from a TypeScript AppHost, use builder.addConnectionString(...) with a parameter and reference that connection string from your consuming apps instead.
For the full reference of Kafka connection properties — and how consuming apps in C#, TypeScript, Python, and Go read them — see Connect to Apache Kafka.
The Kafka hosting integration automatically adds a health check for the Kafka server resource. The health check verifies that a Kafka producer with the specified connection name can connect and persist a topic to the Kafka server.