Skip to main content
This is the advanced guide for Cloudflare Workers. If you’re just getting started, check out the Quickstart Guide first.
This guide covers advanced patterns for sending emails from Cloudflare Workers, including deployment strategies, monitoring, rate limiting, and production best practices.

Using Raw Fetch API

For a zero-dependency approach, use the native fetch API:

Configuration

wrangler.toml

Configure your Worker in wrangler.toml:

Environment Variables vs Secrets

Use [vars] for:
  • Non-sensitive configuration (from email, feature flags)
  • Values that can be committed to version control
Use secrets for:
  • API keys, passwords, tokens
  • Any sensitive credentials
Add secrets via Wrangler:

Local Development

Running Locally

Start the local development server:
This starts Wrangler’s local development server at http://localhost:8787.
Wrangler automatically uses your production secrets in local development. If you need different credentials for local testing, use .dev.vars.

Using .dev.vars for Local Secrets

Create a .dev.vars file for local-only secrets:
Important: Add .dev.vars to .gitignore:

Testing Your Worker

Test with cURL:

Deployment

Deploy to Production

This deploys your Worker to Cloudflare’s global edge network. Your Worker will be available at:

Custom Domains

Add a custom domain in the Cloudflare dashboard:
  1. Go to Workers & Pages > your worker
  2. Click “Triggers”
  3. Add a custom domain (e.g., email-api.yourdomain.com)
Or configure in wrangler.toml:

Multiple Environments

Define environments in wrangler.toml:
Deploy to specific environments:

Advanced Patterns

Rate Limiting with KV

Implement rate limiting using Cloudflare KV:
Add KV namespace in 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:
Configure in wrangler.toml:

Monitoring and Debugging

Logging

Workers automatically send logs to the Cloudflare dashboard:
View logs in:
  • Cloudflare dashboard: Workers & Pages > your worker > Logs
  • Or via Wrangler: npx wrangler tail

Real-Time Logs with Wrangler

Stream logs in real-time:
Filter logs:

Analytics

View Worker analytics in the Cloudflare dashboard:
  • Requests per second
  • Success rate
  • P50/P99 latency
  • Error rate
Access metrics via the Workers Analytics API.

Troubleshooting

Workers have near-zero cold starts, but you can optimize further:
  1. Minimize dependencies — Workers have a 1MB size limit
  2. Use native APIs — Workers provide fetch, crypto, and other built-ins
  3. Avoid heavy SDKs — Consider using raw fetch for simpler use cases
If you see “secret not found” errors:
  1. Verify the secret exists: npx wrangler secret list
  2. Re-add the secret: npx wrangler secret put LETTR_API_KEY
  3. Check the environment: Secrets are per-environment
  4. Redeploy: npm run deploy
Workers have a 50ms CPU time limit (free tier). If you exceed it:
  1. Offload work to external APIs (like Lettr)
  2. Use async I/O — don’t block the CPU
  3. Upgrade to paid plan for 30-second limit
If requests fail due to CORS:
  1. Add CORS headers to responses (see CORS Support)
  2. Handle OPTIONS requests for preflight
  3. Check request origin in browser console
If env.LETTR_API_KEY is undefined:
  1. Check wrangler.toml for the binding name
  2. Verify secret exists: npx wrangler secret list
  3. Use correct environment: --env production
  4. Restart dev server: npm run dev
If you’re hitting Lettr’s rate limits:
  1. Implement request queuing with Durable Objects or queues
  2. Add exponential backoff for retries
  3. Consider upgrading your Lettr plan
  4. Use batch sending for multiple recipients

Best Practices

  1. Use secrets for API keys — never commit them to wrangler.toml
  2. Implement rate limiting to prevent abuse
  3. Add CORS headers for public APIs
  4. Validate inputs before sending emails
  5. Log request IDs for tracking and debugging
  6. Use custom domains for production APIs
  7. Set up multiple environments (dev, staging, production)
  8. Monitor Worker analytics for performance insights

Performance Characteristics

Cloudflare Workers offer excellent performance for email APIs:
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