Add a newsletter signup to your SvelteKit site
One Svelte component collects double-opt-in subscribers and lead metadata on any SvelteKit page, static or server-rendered — pipe new confirmations into your stack with a signed webhook.
1 · Drop in the component
Create a free account, copy your publishable key from the portal, and paste it where you see pk_live_YOUR_KEY below. The key is safe in client-side code.
<script>
let msg = '';
async function subscribe(e) {
e.preventDefault();
const email = e.currentTarget.email.value;
try {
const res = await fetch('https://api.ec.emcognito.com/subscribe', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Publishable-Key': 'pk_live_YOUR_KEY'
},
body: JSON.stringify({
email,
// Optional: provider, spend_band, region, workload, notes
})
});
if (!res.ok) throw new Error();
msg = 'Check your inbox to confirm your subscription.';
} catch {
msg = 'Something went wrong — please try again.';
}
}
</script>
<form on:submit={subscribe}>
<input type="email" name="email" placeholder="you@example.com" required />
<button type="submit">Subscribe</button>
<p role="status" aria-live="polite">{msg}</p>
</form>2 · Subscribers confirm by email
emcognito sends a confirmation email automatically — double opt-in is on by default. The address is recorded as pending and only joins your verified list after the person clicks the link. Bounces and complaints are suppressed for you.
3 · Use your list anywhere
Verified subscribers appear in your owner portal (search, filter, CSV export). Read them server-side with a secret key over GET /v1/subscribers, or get a signed webhook the moment someone confirms — so new subscribers flow straight into your stack. You own the list; emcognito is the collection layer, not an ESP.
FAQ
Does this work with adapter-static?
Yes. Because the publishable key is browser-safe, the form posts straight to emcognito — no server endpoint needed. For a fully server-side flow, a SvelteKit form action works too (see the full docs).
How do I react when someone confirms?
Register a webhook endpoint in the portal. emcognito sends a signed POST on subscriber.confirmed (plus created/deleted/bounced/complained), with retries, so your backend stays in sync.
