Secret Manager
Secrets are a type of binding that attach sensitive data like API keys, tokens, and passwords to your Worker. Unlike environment variables, secrets are encrypted at rest and in transit, providing secure storage for sensitive configuration data that your AI agents need.
Background
Secrets are available on the env parameter passed to your Worker's fetch event handler, just like environment variables. However, secrets are encrypted and designed specifically for sensitive data that should not be visible in logs or the Cloudflare dashboard after creation.
For non-sensitive configuration data like API endpoints and feature flags, use environment variables instead of secrets.
Add Secrets via Wrangler CLI
To add secrets using Wrangler, use the wrangler secret command. Secrets cannot be defined in the wrangler.json file for security reasons.
Setting Secrets
Environment-Specific Secrets
Set secrets for specific environments using the --env flag:
Wrangler Configuration
You must not setup env variables for secrets as they will conflcit when deploying your sevice!
Basic .dev.vars File
Your .dev.vars file will drive the generation of your env.d.ts file for static access to your secrets
Agent Integration
Access secrets within your Agent class:
MCP Integration
Access secrets in your MCP Server:
Alternative Access via Node.js process.env
For Node.js compatibility, you can also access secrets through process.env by enabling the nodejs_compat_populate_process_env compatibility flag. This works the same way as environment variables.
⚠️ Critical Limitation: Like environment variables, process.env secrets are only available during runtime execution and cannot be accessed at module initialization time.
For complete details on Node.js compatibility, runtime limitations, and configuration examples, see the Node.js process.env section in Environment Variables.
Deploy to Specific Environments
Use the --env flag to deploy to specific environments which will use the specific secrets you have setup in your environment.
Local Development with .dev.vars
For local development, add secrets to your .dev.vars file. This file should be formatted like a dotenv file and contains both environment variables and secrets for local development.
Environment-Specific .dev.vars Files
Create separate .dev.vars files for each environment during local development:
When you run wrangler dev --env staging, the .dev.vars.staging file will be loaded.
Important: Add .dev.vars* files to your .gitignore to prevent accidentally committing secrets:
Code Generation for Type Safety
Secrets, environment variables, and service bindings are automatically generated as TypeScript types. In any agent or MCP project, run:
This will automatically update the env.d.ts file with statically generated types for:
- Environment variables from your
wrangler.jsonconfiguration - Secrets from your
.dev.varsfile (for development) and Wrangler configuration - Service bindings and other Cloudflare bindings
Example generated types:
To ensure proper type generation:
- Add secrets to your
.dev.varsfile for local development - Run
npm run codegento generate TypeScript types - Set actual secrets using
wrangler secret putfor deployed environments
Secrets vs Environment Variables
Use Secrets For:
- API keys and tokens
- Database passwords
- Encryption keys
- Authentication credentials
- Payment processing keys
- Any sensitive data
Use Environment Variables For:
- API endpoints and hostnames
- Feature flags and configuration toggles
- Non-sensitive limits and thresholds
- Environment identifiers
- Public configuration values
For non-sensitive information, use environment variables instead of secrets. Environment variables are visible in the Cloudflare dashboard and logs.
Practical Examples
Multi-Service Authentication
Secure Database Connection
Payment Processing
Environment-Aware Secret Usage
Best Practices
Security Guidelines
Error Handling
Security Features
- Encryption: AES-256 encryption at rest, TLS 1.3 in transit
- Access Control: Secrets are only accessible to your code at runtime
- Audit Trail: Secret creation and deletion are logged
- Zero Visibility: Secret values are never visible after creation
- Environment Isolation: Secrets are isolated per environment
Limitations
- Secret Size: Maximum 25KB per secret value
- Secret Count: Maximum 100 secrets per Worker
- Naming: Secret names must be valid environment variable names
- CLI Only: Secrets can only be set via Wrangler CLI
Related Services
- Environment Variables - Non-sensitive configuration
- Memory Store - Temporary sensitive data storage
- Analytics Storage - Audit logging and monitoring