Skip to content

ReadDns

Source: src/Cloudflare/DNS/ReadDns.ts

Binding that lets a Worker read Cloudflare DNS records at runtime.

Creates a least-privilege AccountApiToken with only the DNS Read permission, 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 ReadDnsBinding. The zone is fixed by ReadDnsBinding(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 ReadDnserWorker extends Cloudflare.Worker<ReadDnserWorker>()(
"ReadDnserWorker",
{ main: import.meta.url },
Effect.gen(function* () {
// Init phase — bind the read client scoped to the zone.
const dns = yield* Cloudflare.DNS.ReadDns(Zone);
return {
fetch: Effect.gen(function* () {
const { result } = yield* dns.listDnsRecords({ type: "A" });
const record = yield* dns.getDnsRecord(result[0].id);
return yield* HttpServerResponse.json({ id: record.id });
}),
};
}).pipe(Effect.provide(Cloudflare.DNS.ReadDnsBinding)),
) {}