Send email with Astro
A server-side call to the SendSend API from your Astro 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.
- Add SENDSEND_API_KEY to .env and read it from import.meta.env (server-side endpoints only).
- Add this to
src/pages/api/send.ts:
import type { APIRoute } from 'astro';
export const POST: APIRoute = async ({ request }) => {
const { to } = await request.json();
const res = await fetch('https://api.sendsend.email/v1/emails', {
method: 'POST',
headers: { Authorization: `Bearer ${import.meta.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 Astro', html: '<p>It works!</p>' }),
});
return new Response(await res.text(), { status: res.status, headers: { 'Content-Type': 'application/json' } });
};This needs Astro’s server output (an adapter), because the endpoint runs on the server.
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.