A Cloudflare Worker that receives emails at *@offloadmy.work, stores them in a D1 database, and exposes a REST API for email management.
- ✉️ Receives emails via Cloudflare Email Routing
- 💾 Stores emails in D1 database (from, to, subject, body, headers)
- 🔐 Secure REST API with Bearer token authentication
- 📊 Email statistics (total, unread, starred)
- ⭐ Mark emails as read/starred
- 🗑️ Delete emails
- 🌐 CORS enabled for web clients
All endpoints require Authorization: Bearer YOUR_API_TOKEN header.
List emails with optional filters:
?limit=50- Limit results (default: 50)?offset=0- Pagination offset (default: 0)?unread=true- Only unread emails
Example:
curl -H "Authorization: Bearer YOUR_TOKEN" \
"https://email.offloadmy.work/api/emails?limit=10&unread=true"Get a single email by ID.
Example:
curl -H "Authorization: Bearer YOUR_TOKEN" \
https://email.offloadmy.work/api/emails/abc-123-defUpdate email properties (mark as read, star).
Example:
curl -X PATCH \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{"read": true, "starred": true}' \
https://email.offloadmy.work/api/emails/abc-123-defDelete an email.
Example:
curl -X DELETE \
-H "Authorization: Bearer YOUR_TOKEN" \
https://email.offloadmy.work/api/emails/abc-123-defGet email statistics.
Response:
{
"total": 42,
"unread": 5,
"starred": 3
}- Cloudflare account
- Domain configured in Cloudflare (offloadmy.work)
- Wrangler CLI authenticated
- Clone and install:
cd /Users/netanelgilad/development/offload-email-worker
npm install- Configure your API token:
# Your API token is stored in:
cat ~/.config/offload/email-worker.json- Deploy:
npx wrangler deployAfter deploying, you need to:
- Go to Cloudflare Dashboard
- Select
offloadmy.workdomain - Go to DNS → Records
- Add record:
- Type:
AorAAAA(or CNAME if you have a workers.dev subdomain) - Name:
email - Content: Point to your Cloudflare Worker (usually automatic with routes)
- Proxy: ✅ ON (orange cloud)
- Type:
- Go to Cloudflare Dashboard
- Select
offloadmy.workdomain - Go to Email → Email Routing
- Enable Email Routing if not already enabled
- Add Email Worker route:
- Pattern:
*@offloadmy.work(catch-all) - Action: Send to Worker
- Worker:
offload-email
- Pattern:
See DEPLOY_LOG.md for detailed step-by-step instructions.
CREATE TABLE emails (
id TEXT PRIMARY KEY,
from_address TEXT NOT NULL,
to_address TEXT NOT NULL,
subject TEXT,
body_text TEXT,
body_html TEXT,
headers TEXT, -- JSON string
received_at TEXT NOT NULL,
read INTEGER DEFAULT 0,
starred INTEGER DEFAULT 0
);npx wrangler devnpx wrangler tail offload-email# Local
npx wrangler d1 execute email-store --command "SELECT * FROM emails LIMIT 10"
# Remote
npx wrangler d1 execute email-store --remote --command "SELECT * FROM emails LIMIT 10"- API is protected with Bearer token authentication
- Token is stored in
~/.config/offload/email-worker.json - Never commit the API secret to version control
- Rotate the secret if compromised:
wrangler secret put API_SECRET
- Check Email Routing is enabled in Cloudflare
- Verify worker route is configured correctly
- Check worker logs:
npx wrangler tail offload-email
- Verify Authorization header:
Authorization: Bearer YOUR_TOKEN - Check token in
~/.config/offload/email-worker.json
- Wait for DNS propagation (can take up to 24h, usually <5 min)
- Verify record is created and proxied in Cloudflare DNS
ISC