Astro
An API endpoint on a server-rendered route, and the one config line that makes it possible.
Astro is static by default, and a static build has no server to hold a key. Sending needs an adapter and a route that runs on the server.
Install
npm install @emails.sh/sdk
npx astro add node # or: npx astro add vercel / cloudflare / netlify# .env. The key comes from https://emails.sh/dashboard/api-keys.
EMAILSSH_API_KEY=esh_your_key_hereThe endpoint
// src/pages/api/send.ts
import type { APIRoute } from 'astro';
import { Emailssh } from '@emails.sh/sdk';
// This route must run on the server, so opt it out of prerendering.
export const prerender = false;
export const POST: APIRoute = async ({ request }) => {
const { to, subject, html } = await request.json();
const mail = new Emailssh({ apiKey: import.meta.env.EMAILSSH_API_KEY });
try {
const { id } = await mail.send({ from: 'Acme <hello@acme.com>', to: [to], subject, html });
return new Response(JSON.stringify({ id }), {
status: 200,
headers: { 'content-type': 'application/json' }
});
} catch (err) {
console.error('emails.sh refused the send', err);
return new Response(JSON.stringify({ error: 'Could not send that email.' }), { status: 502 });
}
};