Skip to content
DocsTry Aspire
DocsTry

Set up Data API Builder in the AppHost

⭐ Community Toolkit Data API Builder logo

This article is the AppHost API reference for the 📦 CommunityToolkit.Aspire.Hosting.Azure.DataApiBuilder package. Start with Get started with Data API Builder for the integration workflow, or see Connect Aspire apps to Data API Builder for consuming-app examples.

Terminal
aspire add CommunityToolkit.Aspire.Hosting.Azure.DataApiBuilder

Or add the package manually:

AppHost.cs
#:package CommunityToolkit.Aspire.Hosting.Azure.DataApiBuilder@*
AppHost.csproj
<PackageReference Include="CommunityToolkit.Aspire.Hosting.Azure.DataApiBuilder" Version="*" />

Learn more about aspire add and aspire restore.

The integration uses dab-config.json by default. The file must exist when the AppHost builds the application model.

AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var database = builder.AddSqlServer("sql")
.AddDatabase("catalog");
var dab = builder.AddDataAPIBuilder("dab")
.WithReference(database);
builder.AddProject<Projects.Web>("web")
.WithReference(dab);
builder.Build().Run();

The integration runs mcr.microsoft.com/azure-databases/data-api-builder:1.6.77, maps the container’s port 5000 to an Aspire-managed HTTP endpoint, enables OTLP export, and checks /health.

Reference each database resource from DAB, then read the Aspire-injected connection string in dab-config.json. For a database resource named catalog, use ConnectionStrings__catalog:

dab-config.json
{
"$schema": "https://github.com/Azure/data-api-builder/releases/download/v1.6.77/dab.draft.schema.json",
"data-source": {
"database-type": "mssql",
"connection-string": "@env('ConnectionStrings__catalog')"
},
"runtime": {
"rest": {
"enabled": true,
"path": "/api"
},
"graphql": {
"enabled": true,
"path": "/graphql"
}
},
"entities": {
"Product": {
"source": "dbo.Products",
"permissions": [
{
"role": "anonymous",
"actions": ["read"]
}
]
}
}
}

The REST route for this entity is /api/Product; GraphQL requests use /graphql.

Pass every configuration path to AddDataAPIBuilder or addDataAPIBuilder. Each file is mounted read-only under /App in the container.

AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var dab = builder.AddDataAPIBuilder(
"dab",
["dab-config.json", "dab-config.admin.json"]);
builder.Build().Run();

The DAB resource is a standard container resource, so you can replace the registry, image, or tag:

AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var dab = builder.AddDataAPIBuilder("dab")
.WithImageRegistry("myregistry.azurecr.io")
.WithImage("custom-dab")
.WithImageTag("1.0.0");
builder.Build().Run();

The resource exports its primary endpoint, host, port, and URI expression for use in custom AppHost expressions:

AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var dab = builder.AddDataAPIBuilder("dab");
var primaryEndpoint = dab.Resource.PrimaryEndpoint;
var host = dab.Resource.Host;
var port = dab.Resource.Port;
var uri = dab.Resource.UriExpression;
builder.Build().Run();

The hosting integration automatically adds an HTTP health check for /health. Aspire uses it to report when the DAB resource is ready.