Set a secret for a specific environment

secrets & env vars

// what it does

Scopes the secret to the named environment defined in your Wrangler config. Reach for this when you deploy the same Worker to staging and production and each needs its own keys — e.g. a prod Hyperdrive/Postgres URL versus a staging one.

// shell

$ wrangler secret put API_KEY --env production

// gotcha

Each environment is a separately-named Worker (e.g. my-worker-production), so a secret set without `--env` is NOT shared with `--env production`. You must set the value once per environment or the binding will be missing at runtime.