How bindings work
Bindings appear differently depending on your worker syntax:- ES modules: Bindings are properties on the
envobject passed to handlers - Service workers: Bindings are global variables
Defining bindings
Bindings are defined in thebindings list of your worker configuration:
Binding types
Simple data bindings
These bindings provide static data to your worker:A string value.
Binary data as an ArrayBuffer.
JSON data, parsed into a JavaScript object.
WebAssembly module instance. Only supported in service workers syntax.
Cryptographic key binding
A CryptoKey instance for WebCrypto API operations. You can prevent the worker from extracting raw key material by setting Key formats:
extractable = false.raw: Raw key materialhex: Hex-encoded keybase64: Base64-encoded keypkcs8: Private key in PEM-encoded PKCS#8spki: Public key in PEM-encoded SPKIjwk: Key in JSON format
name: Just a name like"AES-GCM"json: Object encoded as JSON
encrypt, decrypt, sign, verify, deriveKey, deriveBits, wrapKey, unwrapKeyService bindings
Binding to a named service (worker, external server, or network).
Durable Object bindings
Binding to a Durable Object namespace. In the common case of a class in the same worker, you can specify just the class name as a string.
A Durable Object class binding without a storage namespace. Used to implement facets.
Storage bindings
KV namespace binding. Requests are converted to HTTP requests targeting the service.
R2 bucket binding for object storage.
R2 admin API binding for bucket management.
Queue binding
Queue binding for message queuing.
Analytics Engine binding
Analytics Engine binding for storing analytics events. Requires
--experimental flag.Hyperdrive binding
Hyperdrive binding for PostgreSQL connection pooling and caching.
Memory cache binding
In-memory cache shared across isolates in the same process.
Advanced bindings
Takes the value from a system environment variable. The binding value is
null if the environment variable isn’t set.Wraps a collection of inner bindings in a common API wrapper. The wrapper is an internal module that exports a function accepting the inner bindings.The wrapper module exports a function:
Binding for dynamically loading workers at runtime. Workers are cached by name.
Enables access to the UnsafeEval API for dynamic code evaluation.
Provides a
connect() method to dynamically connect to any workerd instance’s debug port. For local development and testing only.Parameter bindings
Parameter bindings declare that a worker requires a binding but doesn’t specify its value. Another worker can inherit this worker and fill in the binding.Declares a required or optional parameter.
type: Expected type of the parameter (text, service, durableObjectNamespace, etc.)optional: Iftrue, derived workers need not specify it
Complete example
Next steps
Workers
Learn more about worker configuration
Services
Understand service types