Send email with Astro
An API route in a mostly static site, sending on demand.
You need a verified sending domain and an API key from Settings › API Keys. If neither exists yet, the quickstart covers both in a couple of minutes.
Install
npm i sending-sdkSend
import type { APIRoute } from "astro";
import { Sending } from "sending-sdk";
const sending = new Sending({ apiKey: import.meta.env.SENDING_API_KEY });
export const POST: APIRoute = async ({ request }) => {
const { email, userId } = await request.json();
const res = await sending.emails.send({
from: "Acme <[email protected]>",
to: email,
subject: "Welcome",
html: "<p>You're in.</p>",
idempotencyKey: `welcome-${userId}`,
});
return new Response(JSON.stringify(res), { status: 202 });
};A successful call answers 202 with the message id: accepted and queued, not yet delivered. Delivery, opens, bounces and complaints arrive later, on webhooks or in the dashboard.
What changes in Astro
The route needs output: 'server' or a hybrid config with prerender disabled: a fully static build has nowhere to run this.
The thing that catches people
Only variables prefixed with PUBLIC_ are exposed to the client, but a static build inlines whatever it can reach. Keep the send in an API route rather than in a component's frontmatter.
Next
- All the fields: cc and bcc, reply-to, attachments, templates, scheduling.
- Webhooks: delivery, bounce and complaint events, signed with HMAC.
- Domains: SPF, DKIM and MAIL FROM, and why sending is refused until they are in place.
- MCP: the same operations as tools, when the one writing the code is an agent.