How to send emails with Cloudflare Workers

Send emails from a Cloudflare Worker with the Publiq SDK, reading the API key from Worker secrets.

Set the secret with wrangler secret put PUBLIQ_API_KEY and read from env:

import { Publiq } from 'publiq';

export default {
  async fetch(req: Request, env: { PUBLIQ_API_KEY: string }) {
    const publiq = new Publiq(env.PUBLIQ_API_KEY);
    await publiq.emails.send({
      from: 'you@yourdomain.com',
      to: 'user@example.com',
      templateKey: 'welcome-email',
    });
    return new Response('sent');
  },
};

Best practices

  • Always call Publiq from the server — never expose the API key on the client.
  • Create the client instance once and reuse it across requests.
  • Prefer templateKey over inline HTML to keep content versioned.
  • Handle PubliqError (status/code); the SDK already retries transient errors with backoff.
  • Send from a verified domain — see Domains.

See also

How to send emails with Cloudflare Workers — Publiq Docs