Cloudflare Hyperdrive
Hyperdrive accelerates and pools connections to an existing database (often a third-party hosted Postgres) so a Worker can reach it quickly. It’s consumed by a Worker via a binding, not over HTTP, so — unlike the other services — there is no .NET client. This integration is hosting-only: it provisions the Hyperdrive configuration at deploy.
The two parts
Section titled “The two parts”- Your dev loop is unchanged. You model your database the normal Aspire way (
builder.AddPostgres("pg"), or an external connection string) and your services connect to it directly. Nothing Cloudflare runs locally. - At deploy, a Hyperdrive config is created pointing at your production database — which is usually not the local dev container. So
PublishAsHyperdrivetakes the production connection string explicitly, typically viaAddDeploymentParameter(a parameter that’s optional in dev but required at deploy).
Provision a Hyperdrive config
Section titled “Provision a Hyperdrive config”The PostgreSQL examples require Aspire.Hosting.PostgreSQL: add the package to the C# AppHost, or add "Aspire.Hosting.PostgreSQL": "13.6.0" to the TypeScript aspire.config.json packages. See the quickstart for the generated TypeScript SDK setup.
The common case — a local Postgres for dev, and the production connection string supplied as a deployment parameter (absent in dev, required at deploy):
builder.AddCloudflareEnvironment();
// Production connection string — required at deploy, optional in local dev.var pgProd = builder.AddDeploymentParameter("pg-production-connection", secret: true);
var pg = builder.AddPostgres("pg"); // normal local dev containerpg.PublishAsHyperdrive("hd", pgProd); // at deploy: a Hyperdrive config pointing at pgProdawait builder.addCloudflareEnvironment();const pgProd = await builder.addDeploymentParameter('pg-production-connection', { secret: true });const pg = await builder.addPostgres('pg');await pg.publishAsHyperdriveWithParameter('hd', pgProd);At aspire deploy, the production connection string is resolved and parsed (both postgres://user:pass@host:port/db URI form and Host=...;Port=...;Database=...;Username=...;Password=... key-value form are supported) and a Hyperdrive configuration is provisioned.
Other production sources:
// From an external connection-string resource:var prod = builder.AddConnectionString("pg-prod");builder.AddPostgres("pg").PublishAsHyperdrive("hd", prod);
// When the resource you call it on already resolves to production (no separate source):builder.AddConnectionString("pg").PublishAsHyperdrive("hd");const prod = await builder.addConnectionString('pg-prod');await builder.addPostgres('pg').publishAsHyperdriveWithConnectionSource('hd', prod);await builder.addConnectionString('pg').publishAsHyperdrive('hd');Options: .WithCachingDisabled(), .AllowDeletion() (permit destroy).
Binding it to a Worker
Section titled “Binding it to a Worker”Workers consume Hyperdrive through a binding. For a hand-authored Worker, associate the config so the provisioned id is surfaced at deploy:
var hd = builder.AddPostgres("pg").PublishAsHyperdrive("hd", pgProd);
builder.AddCloudflareWorker("api", "../worker") .WithHyperdrive(hd, bindingName: "HYPERDRIVE");const hd = await builder.addPostgres('pg').publishAsHyperdriveWithParameter('hd', pgProd);await builder.addCloudflareWorker('api', '../worker') .withHyperdrive(hd, { bindingName: 'HYPERDRIVE' });At deploy the integration logs the binding snippet to add to the Worker’s wrangler.jsonc:
"hyperdrive": [{ "binding": "HYPERDRIVE", "id": "<provisioned-config-id>" }](The integration doesn’t own a hand-authored Worker’s wrangler.jsonc, so it surfaces the id rather than editing the file.)
Credentials
Section titled “Credentials”- API token: needs Hyperdrive: Edit — see API Tokens.
- The production database connection string must point at the real/external database, never the local dev container — supply it as a deployment parameter or external connection-string resource.
