Skip to content
AnswerWell geo Documentation

Research · 8 min read · Answerwell

Worker Secrets: Wrangler Commands, Bindings, and Secrets Store

How to store API keys and tokens in encrypted Worker secrets, declare required names in Wrangler, and reuse account secrets via Secrets Store.

This page summarizes Cloudflare documentation on secrets: encrypted bindings for API keys, database credentials, and auth tokens on Workers, Pages Functions, and related products. In your code, a secret looks like any other environment variable on env, but its value is hidden in the dashboard and Wrangler after you set it.

Secrets vs environment variables: what to use for API keys

Section titled “Secrets vs environment variables: what to use for API keys”

Secrets are encrypted text bindings attached to a Worker or Pages Function; environment variables in vars are plain text and visible in configuration. Cloudflare is explicit that sensitive data must not live in plaintext variables.

Do not use plaintext environment variables to store sensitive information. Use secrets or Secrets Store bindings instead.

— Cloudflare, Environment variables

Secrets are environment variables. The difference is secret values are not visible within Wrangler or Cloudflare dashboard after you define them.

— Cloudflare, Secrets

At runtime there is no difference: the Worker receives the value through env.SECRET_NAME, import { env } from "cloudflare:workers", or process.env when Node.js compatibility is enabled. Choose per-Worker secrets when one script owns the credential; choose Cloudflare Secrets Store when the same value must be shared across Workers or AI Gateway (Bring Your Own Key).

Mechanism Scope Typical use
Worker secret (wrangler secret put) One Worker (per environment) DB password, single-service API key
vars in Wrangler One Worker, visible in config Non-sensitive URLs and feature flags
Secrets Store binding Account; bound per Worker Shared keys, registry credentials, AI provider keys

Per-Worker secrets: how to add and read them in code

Section titled “Per-Worker secrets: how to add and read them in code”

Per-Worker secrets are created with Wrangler or the dashboard, then read from env in your handler.

wrangler secret put: store credentials before deploy

Section titled “wrangler secret put: store credentials before deploy”

For APIs and databases, Cloudflare recommends Wrangler secrets:

If your API requires authentication, use Wrangler secrets to securely store your credentials.

— Cloudflare, APIs

npx wrangler secret put SECRET_NAME

When prompted, enter the value; you can also pipe input (for example a private key). In code:

const secretValue = env.SECRET_NAME;

Use the value in headers or connection strings (Connect to databases). For mTLS, present a client certificate instead of—or in addition to—a text secret (APIs).

Pages Functions use the same idea via bindings:

Secrets are a type of binding that allow you to attach encrypted text values to your Pages Function. You cannot see secrets after you set them and can only access secrets programmatically on context.env.

— Cloudflare, Bindings

Set them with wrangler pages secret put or in the dashboard under Settings > Variables and Secrets, with Encrypt selected. Secrets must exist before a deployment that uses them.

Rotate database and integration credentials

Section titled “Rotate database and integration credentials”

When credentials change, update the Worker secret, not only the database:

When you rotate or update database credentials, you must update the corresponding secrets in your Worker. Use the wrangler secret put command to update secrets securely or update the secret directly in the Cloudflare dashboard.

— Cloudflare, 3rd Party Integrations

wrangler secret put creates a new Worker version and deploys immediately unless you use gradual deployments with wrangler versions secret put.

Dashboard: Variables and Secrets on Workers

Section titled “Dashboard: Variables and Secrets on Workers”

You can add secrets under Workers & Pages > your Worker > Settings > Variables and Secrets (Secrets). Wrangler will not delete dashboard secrets unless you run wrangler secret delete; dashboard edits to vars may be overwritten on the next deploy unless you set keep_vars = true (Wrangler configuration).

secrets.required in Wrangler: validate names locally and on deploy

Section titled “secrets.required in Wrangler: validate names locally and on deploy”

The secrets configuration property declares which secret names the Worker expects. That list drives local loading, type generation, and deploy checks.

The secrets configuration property lets you declare the secret names your Worker requires in your Wrangler configuration file. Required secrets are validated during local development and deploy, and used as the source of truth for type generation.

— Cloudflare, Declare required secrets in your Wrangler configuration

{
"secrets": {
"required": ["API_KEY", "DB_PASSWORD"]
}
}

When secrets.required is set, wrangler dev and vite dev load only those keys from .dev.vars or .env / process.env; extra keys in those files are ignored. Missing names produce a warning (secrets configuration property).

For a required token pattern, see Call an authenticated API from a sandbox: declare names in wrangler.jsonc, run npx wrangler types, then npx wrangler secret put GITHUB_TOKEN.

wrangler deploy and wrangler versions upload validate that all secrets in secrets.required are configured on the Worker before the operation succeeds. If any required secrets are missing, the command fails with an error listing which secrets need to be set.

— Cloudflare, Secrets

Per-environment secrets are supported; wrangler types treats secrets that exist only in some environments as optional on the aggregated Env type (Wrangler configuration). Production secrets for a named environment use wrangler secret put KEY --env ENVIRONMENT_NAME (Environments).

Bulk secrets and --secrets-file: CI/CD without one secret per command

Section titled “Bulk secrets and --secrets-file: CI/CD without one secret per command”

Bulk updates create, update, or delete up to 100 secrets per request; omitted keys stay unchanged.

Include a secret with a value to create or update. Set a secret to null to delete. Secrets not included in the request are left unchanged.

— Cloudflare, New Workers bulk secrets API endpoint

CLI equivalent:

npx wrangler secret bulk < secrets.json

Deletion via JSON ("OLD_SECRET": null) needs Wrangler 4.97.0+; .env bulk files cannot delete keys (wrangler secret bulk).

On deploy, pass a file in one step:

npx wrangler deploy --secrets-file .env.production
npx wrangler versions upload --secrets-file secrets.json

Secrets not in the file are preserved from the previous version (Secrets).

Cloudflare Secrets Store: account-level secrets and Worker bindings

Section titled “Cloudflare Secrets Store: account-level secrets and Worker bindings”

Cloudflare Secrets Store holds encrypted account-level secrets reusable across products:

Cloudflare Secrets Store is a secure, centralized location in which account-level secrets are stored and managed. The secrets are securely encrypted and stored across all Cloudflare data centers.

— Cloudflare, Cloudflare Secrets Store

It is compatible with Workers and AI Gateway today (Cloudflare Secrets Store). Open beta limits: one store per account, up to 100 production secrets per account (local secrets without --remote do not count toward that cap) (Manage account secrets). Secrets Store is unavailable on the Cloudflare China Network.

Once stored, values are not readable again via API or dashboard—only bound services can use them (Manage account secrets). String size limit: 65,536 bytes.

Create secrets, scopes, and access control

Section titled “Create secrets, scopes, and access control”

Create a store with:

wrangler secrets-store store create <name> --remote

Create secrets with scope workers or ai_gateway (Secrets Store access control). Binding a secret requires both authorization (role or API token) and a matching scope on the secret.

CI/CD deploys with Secrets Store bindings need an API token with Account Secrets Store Edit; Read-only tokens fail at deploy (Secrets Store access control).

Add secrets_store_secrets in Wrangler (Workers integration):

{
"secrets_store_secrets": [
{
"binding": "API_KEY",
"store_id": "<STORE_ID>",
"secret_name": "api-key"
}
]
}

Unlike plain env.API_KEY, Secrets Store bindings expose an async get():

const APIkey = await env.API_KEY.get();

AI Gateway BYOK uses the same store: manage keys in the dashboard or with ai_gateway scope, then reference the secret name in headers such as cf-aig-authorization instead of plaintext provider keys (changelog: Secrets Store and AI Gateway).

Containers pulling from private registries can store registry credentials in Secrets Store via wrangler containers registries configure (Image Management).

Integrations and previews: dashboard changes and branching

Section titled “Integrations and previews: dashboard changes and branching”

Workers native integrations in the dashboard were removed; new external connections use secrets on the command line:

The Integrations tab in the dashboard has been removed in favor of a more direct, command-line-based approach using Wrangler secrets.

— Cloudflare, Workers native integrations were removed

Existing integrations keep working; update credentials with npx wrangler secret put <SECRET_NAME> (Workers native integrations were removed).

For Worker Previews, secrets are not in Wrangler config files. Shared secrets go on the preview base config; new previews inherit them (Previews configuration):

Goal Command
Secret on base config npx wrangler preview base-config secret put SECRET_NAME
Secret on one preview npx wrangler preview secret put SECRET_NAME --name <preview>

Later base-config changes apply only to new previews; active previews stay unchanged (Previews configuration).

For broader secret-handling guidance, Brave Search API authentication points to the OWASP Cheat Sheet for Secrets Management.

Common mistakes when storing Worker secrets

Section titled “Common mistakes when storing Worker secrets”
  1. In the Cloudflare dashboard under Workers & Pages, open your Worker or Pages project and confirm required names appear under Variables and Secrets as encrypted secrets, not plaintext variables.

  2. If you use secrets.required, run npx wrangler deploy or npx wrangler versions upload from your project and confirm the command succeeds without a missing-secrets error (Declare required secrets).

  3. For Secrets Store bindings, verify the secret’s scope includes workers and your deploy token has Account Secrets Store Edit (Secrets Store access control).

  4. List configured secret names (not values) with npx wrangler secret list and compare them to secrets.required in Wrangler configuration.

  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact statement you are asking about, and we will fix or clarify it.