Send email from SvelteKit with Notify

SvelteKit send email with Notify — private env vars, +page.server.ts form actions or +server.ts routes, fetch or notifycx, never expose the API key to the browser.

Short answer: Put NOTIFY_API_KEY in SvelteKit private env ($env/static/private, $env/dynamic/private, or SvelteKit 3’s $app/env/private) and call POST https://notify.cx/api/email/send with header x-api-key from +page.server.ts actions or +server.ts only — never from +page.svelte. Rehearse with POST /api/email/send/test until domain verification unlocks production from addresses.

Add transactional email to a SvelteKit app the same way you would in any server-first framework: you render HTML, Notify delivers it, then you inspect logs in the dashboard. Pro is $10 for 10,000 emails.

Never import private env or call Notify from client-side Svelte code — SvelteKit blocks $app/env/private and server-only modules in the browser (server-only modules).

Prerequisites

1. Private environment variable

Add to .env (or .env.local):

NOTIFY_API_KEY=your_api_key_here

SvelteKit 2 (widely used today): import from $env/static/private (build-time) or $env/dynamic/private (runtime). Both are private — they cannot be imported in the browser.

SvelteKit 3+: the $env/* modules are deprecated in favor of explicit variables via defineEnvVars and import { NOTIFY_API_KEY } from '$app/env/private'. Use whichever matches your Kit version; the Notify send contract is the same.

// SvelteKit 2 — static private (typical for API keys)
import { NOTIFY_API_KEY } from '$env/static/private';

// SvelteKit 2 — dynamic private (if the value can change without rebuild)
// import { env } from '$env/dynamic/private';
// const key = env.NOTIFY_API_KEY;

Do not prefix the key with PUBLIC_ or import it from $env/static/public.

2. Shared send helper (server-only)

Put helpers under src/lib/server/ so they cannot leak into client bundles (*.server.ts convention).

// src/lib/server/notify.ts
import { NOTIFY_API_KEY } from '$env/static/private';

const NOTIFY_SEND_URL = 'https://notify.cx/api/email/send';

export async function sendWithNotify(opts: {
  to: string;
  from: string;
  subject: string;
  html: string;
}) {
  const res = await fetch(NOTIFY_SEND_URL, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'x-api-key': NOTIFY_API_KEY
    },
    body: JSON.stringify({
      to: opts.to,
      from: opts.from,
      subject: opts.subject,
      html: opts.html
    })
  });

  if (!res.ok) {
    throw new Error(`Notify send failed: ${await res.text()}`);
  }

  return res.json();
}

Optional notifycx SDK (Node adapter / server routes):

npm install notifycx
// src/lib/server/notify-sdk.ts
import Notify from 'notifycx';
import { NOTIFY_API_KEY } from '$env/static/private';

const notify = new Notify({ apiKey: NOTIFY_API_KEY });

export async function sendWithSdk(opts: {
  to: string;
  from: string;
  subject: string;
  html: string;
}) {
  return notify.send(opts);
}

See Official SDK (notifycx). Prefer the html field; message and text are accepted aliases (Sending Emails).

3. Form action (+page.server.ts)

Use a form action when a contact or notify form POSTs from the browser without putting the API key in client JavaScript.

// src/routes/contact/+page.server.ts
import type { Actions } from './$types';
import { fail } from '@sveltejs/kit';
import { sendWithNotify } from '$lib/server/notify';

export const actions = {
  default: async ({ request }) => {
    const data = await request.formData();
    const to = data.get('to');
    const subject = data.get('subject');
    const html = data.get('html');

    if (typeof to !== 'string' || typeof subject !== 'string' || typeof html !== 'string') {
      return fail(400, { message: 'Missing fields' });
    }

    try {
      await sendWithNotify({
        to,
        from: 'Acme <noreply@your-verified-domain.com>',
        subject,
        html
      });
      return { success: true };
    } catch (err) {
      return fail(500, { message: String(err) });
    }
  }
} satisfies Actions;
<!-- src/routes/contact/+page.svelte -->
<form method="POST">
  <input name="to" type="email" required />
  <input name="subject" required />
  <textarea name="html" required />
  <button type="submit">Send</button>
</form>

4. API route (+server.ts)

Expose a JSON endpoint for other services or progressive enhancement.

// src/routes/api/email/+server.ts
import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { sendWithNotify } from '$lib/server/notify';

export const POST: RequestHandler = async ({ request }) => {
  const { to, subject, html } = await request.json();

  if (!to || !subject || !html) {
    return json({ error: 'Missing to, subject, or html' }, { status: 400 });
  }

  try {
    const data = await sendWithNotify({
      to,
      from: 'Acme <noreply@your-verified-domain.com>',
      subject,
      html
    });
    return json(data);
  } catch (err) {
    return json({ error: String(err) }, { status: 500 });
  }
};

Add your own auth (session check, internal API secret) before exposing this route publicly.

5. Production checklist

  1. Verify domain — Domain Verification
  2. Send a real message from your custom from
  3. Optional (Pro+): webhooks for delivery / bounce
  4. Inspect logs

Common flows

Related docs

FAQ

Can I call Notify from +page.svelte or +layout.svelte?

No. Private env and your API key must stay in +page.server.ts, +server.ts, or src/lib/server/**. SvelteKit errors if a server-only import reaches the browser.

$env/static/private or $env/dynamic/private?

Use static for a normal API key set at build/deploy time. Use dynamic when the value can change without rebuilding. On SvelteKit 3+, migrate to $app/env/private.

Bearer token or x-api-key?

Notify uses header x-api-key only — not Authorization: Bearer. See Authentication & API keys.

Why was my send rejected in production?

New accounts are sandbox_only until you verify a domain. Rehearse with POST /api/email/send/test first (Sandbox vs production).

Does Notify work on Vercel / Netlify / Cloudflare?

Yes — any SvelteKit adapter that runs server-side fetch can call Notify. Keep the key in the platform’s secret env, not in public vars.

Next step

  1. Sign up and create an API key on Credentials.
  2. Add NOTIFY_API_KEY to .env and wire src/lib/server/notify.ts.
  3. Verify your domain, then send from a production from address.