DnsWrite
Source:
src/Cloudflare/Dns/DnsWrite.ts
Binding that lets a Worker create, update, and delete Cloudflare DNS records at runtime.
Creates a least-privilege {@link 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.
Mutating DNS records at runtime
Section titled “Mutating DNS records at runtime”Create, update, and delete records from inside a Worker
Bind the client in the Worker’s Init phase and provide {@link DnsWriteLive}.
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 DnsWriterWorker extends Cloudflare.Worker<DnsWriterWorker>()( "DnsWriterWorker", { main: import.meta.filename }, Effect.gen(function* () { // Init phase — bind the write client scoped to the zone. const dns = yield* Cloudflare.DnsWrite.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, 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.DnsWriteLive)),) {}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 }],});