How to send emails with Astro

In Astro, use an API endpoint (`src/pages/api`) with SSR enabled. Read the key via `import.meta.env`.

Install the SDK (npm install publiq) and set PUBLIQ_API_KEY in your server environment. The code below runs on the server — the API key never reaches the client.

import type { APIRoute } from 'astro';
import { Publiq } from 'publiq';

const publiq = new Publiq(import.meta.env.PUBLIQ_API_KEY);

export const POST: APIRoute = async ({ request }) => {
  const { to } = await request.json();
  const email = await publiq.emails.send({
    from: 'you@yourdomain.com',
    to,
    templateKey: 'welcome-email',
  });
  return new Response(JSON.stringify({ id: email.id }), {
    headers: { 'Content-Type': 'application/json' },
  });
};

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 Astro — Publiq Docs