interface ICloudflareBindings
Since 0.2.0

Typed access to a Cloudflare Worker's platform bindings.

Resolve it from the service registry under CAPABILITIES.CLOUDFLARE.

Every accessor throws CloudflareBindingMissingError for a name the Worker does not carry, rather than returning undefined: a missing binding is a deployment error, and failing with the requested name plus the names that are present says what to fix. Use has when absence is an expected case.

Binding methods may only be called inside a request. The Workers platform prohibits I/O in top-level scope, so holding a binding at registration time is fine while reading through it there is not.

Examples

Example 1

const cf = ctx.services.get<ICloudflareBindings>(CAPABILITIES.CLOUDFLARE);
const value = await cf.kv('SETTINGS').get('theme');
cf.waitUntil(reportUsage(value));

Methods

has(name: string): boolean

Reports whether a binding of that name is present.

names(): readonly string[]

Every binding name the Worker carries, sorted.

vars(): Readonly<Record<string, string>>

The Worker's string variables and secrets.

The same values reach runtime.env when the application passes env to RuntimePlugin; this accessor exists so a consumer holding only the bindings service does not need the runtime as well.

get<T>(name: string): T

A binding of a type this package has no facade for — Hyperdrive, Vectorize, Workers AI, Analytics Engine, and anything Cloudflare ships next.

kv(name: string): IKvNamespace

A KV namespace binding.

r2(name: string): IR2Bucket

An R2 bucket binding.

d1(name: string): ID1Database

A D1 database binding.

queue(name: string): IQueueProducer

A Queues producer binding.

service(name: string): IServiceBinding

A service binding to another Worker.

A Durable Object namespace binding.

waitUntil(promise: Promise<unknown>): void

Keeps the invocation alive until the promise settles, so work can outlive the response.

Cloudflare allows up to 30 seconds after the invocation ends, shared across every call in one request. A rejection is logged rather than left unhandled. Off Cloudflare Workers — with no host injected — the promise simply runs, because no runtime there cuts work off at the response.

Usage

import { type ICloudflareBindings } from "cloudflare-plugin/src/index.ts";