Send from a form with Server Actions
Use Next.js Server Actions to send emails straight from a form, without creating an API route.
The "use server" action runs on the server when the form is submitted:
'use server';
import { Publiq } from 'publiq';
const publiq = new Publiq(process.env.PUBLIQ_API_KEY);
export async function subscribe(formData: FormData) {
await publiq.emails.send({
from: 'you@yourdomain.com',
to: String(formData.get('email')),
templateKey: 'welcome-email',
});
}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.