Next.js
Send email from a route handler or a server action. Both run on your server, so your API key stays out of the browser.
npm install wakio
Set WAKIO_API_KEY in .env.local and in your host's environment settings. You can create the client at the top of a module: it reads the key when it first sends, so next build works without it.
In a route handler
// app/api/welcome/route.ts
import { Wakio } from "wakio"
import { auth } from "@/auth"
const wakio = new Wakio()
export async function POST() {
const session = await auth()
if (!session?.user?.email) return new Response(null, { status: 401 })
await wakio.emails.send({ to: session.user.email, subject: "Welcome to Acme", html: "<p>Thanks for signing up.</p>" })
return Response.json({ ok: true })
}
The route sends only to the signed-in user's own address. A public route that emails an address from the request would let anyone send mail through your account.
In a server action, with React Email
// app/actions.tsx
"use server"
import { render } from "@react-email/render"
import { Wakio } from "wakio"
import { auth } from "@/auth"
import { WelcomeEmail } from "@/emails/welcome"
const wakio = new Wakio({ render })
export async function sendWelcome() {
const session = await auth()
if (!session?.user?.email) throw new Error("Not signed in")
await wakio.emails.send({
to: session.user.email,
subject: "Welcome to Acme",
react: <WelcomeEmail name={session.user.name ?? "there"} />,
})
}
The package also runs on the Edge runtime.
For the full client, see Node.js.