Send email with Cloudflare Workers
A server-side call to the SendSend API from your Cloudflare Workers app.
- Create a test API key in the dashboard first: it never delivers real email. Switch to a live key once your domain is verified.
- Store the key as a secret: run `wrangler secret put SENDSEND_API_KEY`. Never put it in wrangler.toml.
- Add this to
src/index.ts:
export default {
async fetch(request: Request, env: { SENDSEND_API_KEY: string }): Promise<Response> {
const { to } = await request.json<{ to: string }>();
return fetch('https://api.sendsend.email/v1/emails', {
method: 'POST',
headers: { Authorization: `Bearer ${env.SENDSEND_API_KEY}`, 'Content-Type': 'application/json', 'Idempotency-Key': crypto.randomUUID() },
body: JSON.stringify({ from: 'Acme <hello@mail.acme.com>', to: [to], subject: 'Hello from a Worker', html: '<p>It works!</p>' }),
});
},
};Good to know
- A
202response means the message was queued, not delivered. See message states and webhooks. - Always send an Idempotency-Key, so a retry cannot send twice.
- Check the response status. Errors have one shape: see Errors.
- More options: attachments, scheduling, headers, templates and React Email.