How to send emails with SvelteKit
In SvelteKit, use a `+server.ts` endpoint (or a form action) to send from the server. Read the key via `$env/static/private`.
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 { json } from '@sveltejs/kit';
import { Publiq } from 'publiq';
import { PUBLIQ_API_KEY } from '$env/static/private';
const publiq = new Publiq(PUBLIQ_API_KEY);
export async function POST({ request }) {
const { to } = await request.json();
const email = await publiq.emails.send({
from: 'you@yourdomain.com',
to,
templateKey: 'welcome-email',
});
return json({ id: email.id });
}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
templateKeyover 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.