Expand description
Access Cloudflare Worker bindings and request context from inside
#[server] functions.
§Binding shortcuts
cf::d1()— D1 database (combines env + binding lookup + error conversion)cf::kv()— Workers KV namespacecf::r2()— R2 bucketcf::durable_object()— Durable Object namespace- [
cf::queue()] — Queue producer (requiresqueuefeature) cf::secret()— encrypted secret (set viawrangler secret put)cf::var()— plaintext environment variable (set in[vars])cf::ai()— Workers AI inferencecf::service()— Service binding (call other Workers)
§Raw access
cf::env()— the full WorkerEnv(for bindings without a shorthand)cf::req()— the rawworker::Request(headers, IP)
§Incoming cookies
cf::cookie()— read a named cookie from the requestcf::cookies()— read all cookies from the request
§Outgoing cookies
cf::set_cookie()— queue an HttpOnly auth cookie (secure defaults)cf::set_cookie_with()— queue a cookie with custom optionscf::clear_cookie()— queue a cookie-clearing header (logout)
Outgoing cookies are queued in thread-local storage and applied to the
response by handle — no &mut Response needed inside server functions.
§Sessions
cf::session()— load the session (async first call, cached after); returnsSessionhandle
Requires .session(SessionConfig::kv("SESSIONS")) on the Handler.
§WebSocket helpers
cf::websocket_upgrade()— create aWebSocketPairand 101 response in one callcf::websocket_pair()— create a rawWebSocketPairfor custom handling
Structs§
- Cookie
Builder - Builder for cookies with custom options.
- Session
- Zero-sized session handle — all operations access thread-local state.
Enums§
- Same
Site SameSiteattribute for cookies.
Functions§
- ai
- Access a Workers AI binding by name.
- clear_
cookie - Queue a cookie-clearing header on the outgoing response (
Max-Age=0). - cookie
- Read a named cookie from the incoming request’s
Cookieheader. - cookies
- Read all cookies from the incoming request’s
Cookieheader. - d1
- Access a D1 database binding by name.
- durable_
object - Access a Durable Object namespace binding by name.
- env
- Access the Cloudflare Worker
Envfrom inside any#[server]function. - kv
- Access a Workers KV namespace binding by name.
- r2
- Access an R2 bucket binding by name.
- req
- Access the raw Cloudflare Worker
worker::Requestfor the current request. - secret
- Access a secret environment variable by name.
- service
- Access a Service binding by name.
- session
- Load the session for the current request.
- set_
cookie - Queue an
HttpOnlyauth cookie on the outgoing response. - set_
cookie_ with - Start building a cookie with custom options.
- var
- Access a plaintext environment variable by name.
- websocket_
pair - Create a raw [
WebSocketPair] without building the upgrade response. - websocket_
upgrade - Create a [
WebSocketPair] and build the 101 Upgrade response in one call.