defineEnv
defineEnv<
S>(schema):Env<S>
Defined in: packages/env/src/define-env.ts:58
Creates a type-safe, runtime-validated environment object for Cloudflare Worker bindings.
Declare your bindings once and get a fully typed environment. Validation runs once
at startup via init(), catching missing or misconfigured bindings before any
request is processed.
Type Parameters
Section titled “Type Parameters”S extends Schema
Parameters
Section titled “Parameters”schema
Section titled “schema”S
A record mapping binding names to their definitions (type, defaults, validation).
Returns
Section titled “Returns”Env<S>
An object with init() and get() methods for initializing and accessing the typed environment.
Example
Section titled “Example”import { defineEnv } from "@cfast/env";
const env = defineEnv({ DB: { type: "d1" }, CACHE: { type: "kv" }, MAILGUN_API_KEY: { type: "secret" }, APP_URL: { type: "var", default: "http://localhost:8787" },});
export default { async fetch(request, rawEnv) { env.init(rawEnv); const { DB, MAILGUN_API_KEY } = env.get(); },};