Send email with Next.js
A server-side call to the SendSend API from your Next.js app.
- Create a test API key in the dashboard first: it never delivers real email. Switch to a live key once your domain is verified.
- Add SENDSEND_API_KEY to .env.local (and to your host’s environment variables for production).
- Add this to
app/api/send/route.ts:
export async function POST(req: Request) {
const { to } = await req.json();
const res = await fetch('https://api.sendsend.email/v1/emails', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.SENDSEND_API_KEY}`,
'Content-Type': 'application/json',
'Idempotency-Key': crypto.randomUUID(),
},
body: JSON.stringify({ from: 'Acme <hello@mail.acme.com>', to: [to], subject: 'Hello from Next.js', html: '<p>It works!</p>' }),
});
return Response.json(await res.json(), { status: res.status });
}Call this from a server action or an API route. Never put the key in a client component.
Good to know
- A
202response means the message was queued, not delivered. See message states and webhooks. - Always send an Idempotency-Key, so a retry cannot send twice.
- Check the response status. Errors have one shape: see Errors.
- More options: attachments, scheduling, headers, templates and React Email.