# Dev bindings and typed env

A Worker reads its bindings (KV, D1, R2, Queues, Workers AI and the rest) from `event.runtime.env`. Two things make that work before you deploy: `hamur dev` can give every event the bindings of your wrangler config, and `hamur typegen` types them.

## Dev bindings

`cloudflare.devBindings` is `true` by default. With the `cloudflare` preset and `wrangler` installed in the app, `hamur dev` loads it and gives every event `event.runtime.env`, `event.runtime.ctx` and `event.runtime.caches` from wrangler's `getPlatformProxy`. Hamur depends on no wrangler package: it resolves the copy in your app at run time, so install it as a dev dependency of the app.

- The wrangler config is the one the build reads: `wrangler.json`, else `wrangler.jsonc`, else `wrangler.toml`, with the block `cloudflare.env` names. A file that does not parse fails with `Cannot parse wrangler.toml: <message>`.
- Dev is local only. The proxy runs with `remoteBindings: false`, so dev never reaches your Cloudflare account and a Workers AI call from dev does not bill it. A binding that exists only remotely is absent, and dev logs once: `[hamur] Cloudflare bindings AI, DB of the wrangler config are not in event.runtime.env in dev: remote bindings are off and dev never reaches your Cloudflare account`.
- Without `wrangler`, dev starts and logs once `[hamur] Cloudflare bindings are not available in dev; install wrangler in the app to use them`. If the proxy fails, dev logs `wrangler's getPlatformProxy failed under Bun: <message>` and goes on without bindings.
- `cloudflare.devBindings: false` skips all of it. Any value that is not a boolean fails the load.

In a deployed Worker `event.runtime.ctx` is the call's `ExecutionContext` and `event.runtime.caches` is `globalThis.caches`, so code written against dev works deployed. A scheduled, queue, email or tail invocation has no event; its hook gets `context`.

## Typed env

On the `cloudflare` and `cloudflare-pages` presets, `hamur typegen` and `hamur build` read your wrangler config and write `.hamur/types/cloudflare.d.ts`. It augments `HamurRuntimeEnv`, so `event.runtime.env` is typed and always present.

````jsonc [wrangler.jsonc]
{
  "name": "my-app",
  "kv_namespaces": [{ "binding": "CACHE", "id": "<namespace id>" }],
  "vars": { "ENVIRONMENT": "production" },
}
````

With that config, `event.runtime.env.CACHE` is a KV namespace and `event.runtime.env.ENVIRONMENT` is a `string`. A name that is not in the config does not compile.

| Wrangler key                                                                             | Type of the property                                                             |
| ---------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- |
| `kv_namespaces`                                                                          | `HamurKVNamespace`                                                               |
| `d1_databases`                                                                           | `HamurD1Database`                                                                |
| `r2_buckets`                                                                             | `HamurR2Bucket`                                                                  |
| `durable_objects.bindings`, and each class of `defineServer` `exports` not already bound | `HamurDurableObjectNamespace`                                                    |
| `queues.producers`                                                                       | `HamurQueue`                                                                     |
| `services`                                                                               | `HamurFetcher`                                                                   |
| `ai`                                                                                     | `HamurAi`                                                                        |
| `vectorize`                                                                              | `HamurVectorize`                                                                 |
| `hyperdrive`                                                                             | `HamurHyperdrive`                                                                |
| `vars`                                                                                   | the value's type: `string`, `number`, `boolean`, objects and arrays structurally |

The interfaces are written into the file with the surface an app calls, and the file imports no `@cloudflare/workers-types`. The kinds `analytics_engine_datasets`, `dispatch_namespaces`, `mtls_certificates`, `pipelines`, `workflows`, `secrets_store_secrets`, `browser`, `images`, `version_metadata` and `send_email` are typed `unknown`, with one `[hamur]` warning per kind: `the wrangler config binds "browser", which Hamur does not type; its bindings are typed unknown on event.runtime.env`. Narrow them yourself, as the [Browser Rendering recipe](/deploy/recipes/browser-rendering) does. The Workers AI and Vectorize interfaces keep options and results loose (`unknown`), so the [recipes](/deploy/recipes/workers-ai) state the shapes they read.

Without the file, `event.runtime.env` is `Record<string, unknown>` and optional. The handler below states the shape it needs on its own, which is also how a sample can compile without a wrangler config:

````ts [server/cloudflare-env.ts]
import { defineHandler } from "hamur/server"
import type { HamurEvent } from "hamur/server"

interface Env {
  CACHE: {
    get(key: string): Promise<string | null>
    put(key: string, value: string, options?: { expirationTtl?: number }): Promise<void>
  }
  ENVIRONMENT: string
}

const bindings = (event: HamurEvent): Env => event.runtime.env as unknown as Env

export const visits = defineHandler({
  route: "/api/visits",
  method: "GET",
  async handler(_, { event }) {
    const { CACHE, ENVIRONMENT } = bindings(event)
    const count = Number((await CACHE.get("visits")) ?? 0) + 1
    event.waitUntil(CACHE.put("visits", String(count)))
    return { count, environment: ENVIRONMENT }
  },
})
````

## Without an event

`useRuntimeEnv()` of `hamur/server` reads the platform env at module scope and in server plugins, before any request: the `env` of `cloudflare:workers` on `cloudflare`, `Netlify.env.toObject()` on `netlify` and `netlify-edge`, `process.env` elsewhere. It is not `useRuntimeConfig()`, which merges the `HAMUR_*` variables into your config. In `hamur dev` on `cloudflare` it returns the proxy's bindings once the proxy is up, and `process.env` before that. See [The platform env, without an event](/api-reference/server#useruntimeenv) and [`useRuntimeEnv`](/api-reference/server#useruntimeenv).

Bindings that Hamur writes for you (the D1 database of `@hamur/db`, R2 of `@hamur/blob`, KV and R2 storage mounts) and `run_worker_first` are on the [`cloudflare`](/deploy/presets/cloudflare#bound-resources) page.
