Build Cloudflare Workers that send, read, and manage email through MultiMail's API — with human oversight and zero cold starts.
Cloudflare Workers run at the edge with sub-millisecond cold starts, making them ideal for responsive AI agents. By integrating MultiMail, your Workers gain full email capabilities: sending messages, checking inboxes, replying to threads, and managing contacts — all from edge locations worldwide.
MultiMail itself runs on Cloudflare's infrastructure, which means API calls from your Worker to MultiMail stay within Cloudflare's network for minimal latency. The REST API uses standard fetch, which is native to the Workers runtime with no additional dependencies needed.
Combined with Durable Objects for agent state and Workers AI for inference, you can build complete email-handling agents that process incoming messages, draft responses, and manage approval queues entirely at the edge.
MultiMail runs on Cloudflare. API calls from your Worker stay within Cloudflare's network, avoiding extra network hops and delivering consistently low response times for email operations.
The Workers runtime has fetch built in. Calling the MultiMail API requires no npm packages — just standard HTTP requests. This keeps your Worker bundle small and deploy times fast.
Use Durable Objects to maintain agent state across requests. Track conversation context, pending approvals, and contact history in a Durable Object while using MultiMail for the actual email operations.
Combine MultiMail webhooks with Workers to process incoming emails in real time. When a new email arrives, MultiMail can trigger your Worker to analyze it, draft a response, and queue it for approval.
Use Workers AI for on-device inference to analyze incoming emails, classify intent, and generate responses — then send those responses through MultiMail's API with oversight controls.
interface Env {
MULTIMAIL_API_KEY: string;
}
const MULTIMAIL_API = 'https://api.multimail.dev/v1';
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const url = new URL(request.url);
const headers = {
'Authorization': `Bearer ${env.MULTIMAIL_API_KEY}`,
'Content-Type': 'application/json',
};
if (url.pathname === '/send' && request.method === 'POST') {
const body = await request.json<{
mailbox_id: string;
to: string;
subject: string;
body: string;
}>();
const resp = await fetch(`${MULTIMAIL_API}/send`, {
method: 'POST',
headers,
body: JSON.stringify(body),
});
return new Response(await resp.text(), {
status: resp.status,
headers: { 'Content-Type': 'application/json' },
});
}
if (url.pathname.startsWith('/inbox/')) {
const mailboxId = url.pathname.split('/')[2];
const resp = await fetch(
`${MULTIMAIL_API}/mailboxes/${mailboxId}/inbox?limit=10`,
{ headers }
);
return new Response(await resp.text(), {
headers: { 'Content-Type': 'application/json' },
});
}
return new Response('Not Found', { status: 404 });
},
};A Cloudflare Worker that exposes email send and inbox endpoints using MultiMail's API.
interface Env {
MULTIMAIL_API_KEY: string;
AI: Ai;
}
export default {
async fetch(request: Request, env: Env): Promise<Response> {
if (request.method !== 'POST') {
return new Response('Method not allowed', { status: 405 });
}
const webhook = await request.json<{
event: string;
message_id: string;
from: string;
subject: string;
body: string;
}>();
if (webhook.event !== 'email.received') {
return new Response('OK');
}
"cm">// Use Workers AI to generate a reply
const aiResponse = await env.AI.run('@cf/meta/llama-3.1-8b-instruct', {
messages: [
{
role: 'system',
content: 'Draft a brief, professional reply to this email.',
},
{
role: 'user',
content: `From: ${webhook.from}\nSubject: ${webhook.subject}\n\n${webhook.body}`,
},
],
});
"cm">// Send reply through MultiMail (queued in gated_send mode)
await fetch('https://api.multimail.dev/v1/reply', {
method: 'POST',
headers: {
'Authorization': `Bearer ${env.MULTIMAIL_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
message_id: webhook.message_id,
body: aiResponse.response,
}),
});
return new Response('OK');
},
};Process incoming emails from MultiMail webhooks and generate AI-powered responses.
import { DurableObject } from 'cloudflare:workers';
interface Env {
MULTIMAIL_API_KEY: string;
EMAIL_AGENT: DurableObjectNamespace;
}
export class EmailAgent extends DurableObject {
private headers: Record<string, string>;
constructor(ctx: DurableObjectState, env: Env) {
super(ctx, env);
this.headers = {
'Authorization': `Bearer ${env.MULTIMAIL_API_KEY}`,
'Content-Type': 'application/json',
};
}
async processInbox(mailboxId: string): Promise<object> {
"cm">// Fetch new emails
const resp = await fetch(
`https:"cm">//api.multimail.dev/v1/mailboxes/${mailboxId}/inbox?limit=5`,
{ headers: this.headers }
);
const inbox = await resp.json<{ messages: any[] }>();
"cm">// Track processed messages in Durable Object storage
const processed = (await this.ctx.storage.get<string[]>('processed')) || [];
const newMessages = inbox.messages.filter(
(m: any) => !processed.includes(m.id)
);
"cm">// Mark as processed
const allProcessed = [...processed, ...newMessages.map((m: any) => m.id)];
await this.ctx.storage.put('processed', allProcessed);
return { new_count: newMessages.length, messages: newMessages };
}
async sendReply(messageId: string, body: string): Promise<object> {
const resp = await fetch('https://api.multimail.dev/v1/reply', {
method: 'POST',
headers: this.headers,
body: JSON.stringify({ message_id: messageId, body }),
});
return resp.json();
}
}A stateful email agent using Durable Objects to maintain conversation context across requests.
# wrangler.toml
name = "email-agent"
main = "src/index.ts"
compatibility_date = "2024-12-01"
[durable_objects]
bindings = [
{ name = "EMAIL_AGENT", class_name = "EmailAgent" }
]
[[migrations]]
tag = "v1"
new_classes = ["EmailAgent"]
# Set your API key as a secret:
# npx wrangler secret put MULTIMAIL_API_KEYConfigure your Worker with the MultiMail API key as a secret.
Sign up at multimail.dev, create a mailbox, and generate an API key.
Create a new Cloudflare Worker project using Wrangler.
npm create cloudflare@latest email-agent -- --type worker-typescriptAdd your MultiMail API key as a Worker secret so it is not exposed in source code.
npx wrangler secret put MULTIMAIL_API_KEYWrite fetch handlers that call the MultiMail API. Use the env.MULTIMAIL_API_KEY binding for authentication.
Deploy your Worker and test the email integration. Set up a MultiMail webhook pointing to your Worker URL for incoming email processing.
npx wrangler deployEmail infrastructure built for AI agents. Verifiable identity, graduated oversight, and a 38-tool MCP server. Formally verified in Lean 4.