Skip to content

DnsReadWrite

Source: src/Cloudflare/Dns/DnsReadWrite.ts

Binding that lets a Worker perform the full Cloudflare DNS record CRUD surface at runtime.

Creates a least-privilege {@link AccountApiToken} with both the DNS Read and DNS Write permissions, scoped to the single zone passed to bind, and binds its value into the Worker so runtime code can authenticate.

Bind the client in the Worker’s Init phase and provide {@link DnsReadWriteLive}. The zone is fixed by .bind(zone) — the provisioned token only grants access to that zone, so calls take no zoneId. Pass the {@link Zone} resource directly (it’s an Effect), or yield* Zone for a resolved value.

import * as Cloudflare from "alchemy/Cloudflare";
import * as Effect from "effect/Effect";
import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse";
const Zone = Cloudflare.Zone("MyZone", { name: "example.com" });
export class DnsWorker extends Cloudflare.Worker<DnsWorker>()(
"DnsWorker",
{ main: import.meta.filename },
Effect.gen(function* () {
// Init phase — bind the full CRUD client scoped to the zone.
const dns = yield* Cloudflare.DnsReadWrite.bind(Zone);
return {
fetch: Effect.gen(function* () {
const { result } = yield* dns.createDnsRecord({
type: "A",
name: "app.example.com",
content: "192.0.2.1",
ttl: 1,
});
const record = yield* dns.getDnsRecord(result.id);
yield* dns.deleteDnsRecord(result.id);
return yield* HttpServerResponse.json({ id: record.id });
}),
};
}).pipe(Effect.provide(Cloudflare.DnsReadWriteLive)),
) {}