This is the advanced guide for Cloudflare Workers. If you’re just getting started, check out the Quickstart Guide first.
Using Raw Fetch API
For a zero-dependency approach, use the native fetch API:Configuration
wrangler.toml
Configure your Worker inwrangler.toml:
Environment Variables vs Secrets
Use[vars] for:
- Non-sensitive configuration (from email, feature flags)
- Values that can be committed to version control
- API keys, passwords, tokens
- Any sensitive credentials
Local Development
Running Locally
Start the local development server:http://localhost:8787.
Using .dev.vars for Local Secrets
Create a.dev.vars file for local-only secrets:
.dev.vars to .gitignore:
Testing Your Worker
Test with cURL:Deployment
Deploy to Production
Custom Domains
Add a custom domain in the Cloudflare dashboard:- Go to Workers & Pages > your worker
- Click “Triggers”
- Add a custom domain (e.g.,
email-api.yourdomain.com)
wrangler.toml:
Multiple Environments
Define environments inwrangler.toml:
Advanced Patterns
Rate Limiting with KV
Implement rate limiting using Cloudflare KV:wrangler.toml:
CORS Support
Add CORS headers for cross-origin requests:Request Validation
Add robust input validation:Template-Based Emails
Use Lettr templates for consistent design:Scheduled Emails with Cron Triggers
Trigger Workers via Cron Triggers for scheduled emails:wrangler.toml:
Monitoring and Debugging
Logging
Workers automatically send logs to the Cloudflare dashboard:- Cloudflare dashboard: Workers & Pages > your worker > Logs
- Or via Wrangler:
npx wrangler tail
Real-Time Logs with Wrangler
Stream logs in real-time:Analytics
View Worker analytics in the Cloudflare dashboard:- Requests per second
- Success rate
- P50/P99 latency
- Error rate
Troubleshooting
Cold start performance
Cold start performance
Workers have near-zero cold starts, but you can optimize further:
- Minimize dependencies — Workers have a 1MB size limit
- Use native APIs — Workers provide fetch, crypto, and other built-ins
- Avoid heavy SDKs — Consider using raw fetch for simpler use cases
Secret not found errors
Secret not found errors
If you see “secret not found” errors:
- Verify the secret exists:
npx wrangler secret list - Re-add the secret:
npx wrangler secret put LETTR_API_KEY - Check the environment: Secrets are per-environment
- Redeploy:
npm run deploy
CPU time exceeded
CPU time exceeded
Workers have a 50ms CPU time limit (free tier). If you exceed it:
- Offload work to external APIs (like Lettr)
- Use async I/O — don’t block the CPU
- Upgrade to paid plan for 30-second limit
CORS issues
CORS issues
If requests fail due to CORS:
- Add CORS headers to responses (see CORS Support)
- Handle OPTIONS requests for preflight
- Check request origin in browser console
Environment variables not loading
Environment variables not loading
If
env.LETTR_API_KEY is undefined:- Check
wrangler.tomlfor the binding name - Verify secret exists:
npx wrangler secret list - Use correct environment:
--env production - Restart dev server:
npm run dev
Rate limiting
Rate limiting
If you’re hitting Lettr’s rate limits:
- Implement request queuing with Durable Objects or queues
- Add exponential backoff for retries
- Consider upgrading your Lettr plan
- Use batch sending for multiple recipients
Best Practices
- Use secrets for API keys — never commit them to
wrangler.toml - Implement rate limiting to prevent abuse
- Add CORS headers for public APIs
- Validate inputs before sending emails
- Log request IDs for tracking and debugging
- Use custom domains for production APIs
- Set up multiple environments (dev, staging, production)
- Monitor Worker analytics for performance insights
Performance Characteristics
Cloudflare Workers offer excellent performance for email APIs:| Metric | Performance |
|---|---|
| Cold start | < 10ms |
| Typical latency | 10-50ms (globally) |
| Concurrent requests | Unlimited |
| CPU time | 50ms (free) / 30s (paid) |
| Memory | 128MB |
| Script size | 1MB (compressed) |
Workers run on Cloudflare’s global network across 300+ cities, so your email API is fast everywhere in the world.
What’s Next
Quickstart Guide
Back to quickstart
AWS Lambda
Deploy on AWS Lambda
Vercel Functions
Deploy on Vercel
Templates
Use Lettr templates