Send email with SvelteKit
A form action or a +server endpoint, with the key from $env/static/private.
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 { json } from "@sveltejs/kit";
import { SENDING_API_KEY } from "$env/static/private";
import { Sending } from "sending-sdk";
const sending = new Sending({ apiKey: SENDING_API_KEY });
export async function POST({ 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 json(res);
}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 SvelteKit
$env/static/private is checked at build time: importing it from client code is a build error rather than a leaked key at runtime.
The thing that catches people
On the edge adapters use $env/dynamic/private instead: static values are inlined at build time and a platform secret set afterwards will not appear.
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.