Skip to main content

Module cf

Module cf 

Source
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 namespace
  • cf::r2() — R2 bucket
  • cf::durable_object() — Durable Object namespace
  • [cf::queue()] — Queue producer (requires queue feature)
  • cf::secret() — encrypted secret (set via wrangler secret put)
  • cf::var() — plaintext environment variable (set in [vars])

§Raw access

  • cf::env() — the full Worker Env (for bindings without a shorthand)
  • cf::req() — the raw worker::Request (headers, IP)

§Incoming cookies

§Outgoing cookies

Outgoing cookies are queued in thread-local storage and applied to the response by handle — no &mut Response needed inside server functions.

§Sessions

Requires .session(SessionConfig::kv("SESSIONS")) on the Handler.

§WebSocket helpers

Structs§

CookieBuilder
Builder for cookies with custom options.
Session
Zero-sized session handle — all operations access thread-local state.

Enums§

SameSite
SameSite attribute for cookies.

Functions§

clear_cookie
Queue a cookie-clearing header on the outgoing response (Max-Age=0).
cookie
Read a named cookie from the incoming request’s Cookie header.
cookies
Read all cookies from the incoming request’s Cookie header.
d1
Access a D1 database binding by name.
durable_object
Access a Durable Object namespace binding by name.
env
Access the Cloudflare Worker Env from 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::Request for the current request.
secret
Access a secret environment variable by name.
session
Load the session for the current request.
set_cookie
Queue an HttpOnly auth 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.