Skip to content

WriteDns

Source: src/Cloudflare/DNS/WriteDns.ts

Binding that lets a Worker create, update, and delete Cloudflare DNS records at runtime.

Creates a least-privilege AccountApiToken with only the DNS Write permission, scoped to the single zone passed to bind, and binds its value into the Worker so runtime code can authenticate.

Create, update, and delete records from inside a Worker

Bind the client in the Worker’s Init phase and provide WriteDnsBinding. The zone is fixed by WriteDnsBinding(zone) — the provisioned token only grants access to that zone, so calls take no zoneId. Pass the 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.Zone("MyZone", { name: "example.com" });
export class WriteDnsrWorker extends Cloudflare.Worker<WriteDnsrWorker>()(
"WriteDnsrWorker",
{ main: import.meta.url },
Effect.gen(function* () {
// Init phase — bind the write client scoped to the zone.
const dns = yield* Cloudflare.DNS.WriteDns(Zone);
return {
fetch: Effect.gen(function* () {
const { result } = yield* dns.createDnsRecord({
type: "A",
name: "app.example.com",
content: "192.0.2.1",
ttl: 1,
proxied: true,
});
yield* dns.updateDnsRecord(result.id, {
type: "A",
name: "app.example.com",
content: "192.0.2.2",
ttl: 1,
});
yield* dns.deleteDnsRecord(result.id);
return yield* HttpServerResponse.json({ id: result.id });
}),
};
}).pipe(Effect.provide(Cloudflare.DNS.WriteDnsBinding)),
) {}

Apply a batch of changes atomically

yield* dns.batchDnsRecords({
posts: [{ type: "A", name: "a.example.com", content: "192.0.2.1", ttl: 1 }],
deletes: [{ id: oldRecordId }],
});