DocumentationNext.js recipe
Next.js recipe
Server Actions and Route Handlers — keep API keys off the client.
In Next.js App Router, call MailingCore from server-only code. Never put mc_live_ keys in client bundles.
Server Action
// app/actions/send-email.ts
'use server'
import { MailingCore } from 'mailingcore-js'
MailingCore.init({ apiKey: process.env.MAILINGCORE_API_KEY! })
export async function sendWelcomeEmail(email: string, name: string) {
return MailingCore.send({
to: email,
subject: 'Welcome!',
htmlBody: `<h1>Hello, ${name}!</h1>`,
idempotencyKey: `welcome-${email}`,
})
}
// app/contact/page.tsx
'use client'
import { sendWelcomeEmail } from '@/app/actions/send-email'
export function ContactForm() {
async function handleSubmit(formData: FormData) {
const email = formData.get('email') as string
const name = formData.get('name') as string
await sendWelcomeEmail(email, name)
}
return (
<form action={handleSubmit}>
<input name="name" required />
<input name="email" type="email" required />
<button type="submit">Send</button>
</form>
)
}
Route Handler (alternative)
// app/api/notify/route.ts
import { NextResponse } from 'next/server'
import { MailingCore } from 'mailingcore-js'
MailingCore.init({ apiKey: process.env.MAILINGCORE_API_KEY! })
export async function POST(req: Request) {
const { email, subject, htmlBody } = await req.json()
const result = await MailingCore.send({ to: email, subject, htmlBody })
return NextResponse.json(result)
}
Environment variables
# .env.local (server only — no NEXT_PUBLIC_ prefix)
MAILINGCORE_API_KEY=mc_live_xxxx
Related
- Init and configuration
- Error handling
- Architecture overview — when to use SDK vs REST